1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//! **What an `auto` stands for** — the type a declaration did not write.
//!
//! A declaration whose type is a placeholder is the one kind whose type is not in the file at all: `auto x = f();`
//! says `x` is whatever `f` returns, and nothing in that line spells it. So this module answers a question the
//! reader of a single declaration cannot, and it answers it **only from what the file itself says** — an
//! initializer's own expression, typed by the engine [`type_of_expression`] already provides.
//!
//! # What it deduces, and what it refuses
//!
//! Measured over MSVC's headers, `auto` declarations divide by what follows the name:
//!
//! ```text
//! 111 a call `auto x = f(a, b);` the callee's return type
//! 83 a plain expression `auto x = y;` the name's own type
//! 63 a cast `auto x = static_cast<T>(v);`
//! 18 no initializer `auto f() { … }` the return statements, not the declaration
//! 8 a braced initializer `auto x{…};`
//! ```
//!
//! The first three are one question — *what is this expression's type* — and they are what this module answers.
//! The last two are **not**, and they are refused rather than approximated:
//!
//! * `auto f() { … }` has no initializer at all; its type is what the `return` statements agree on, which is a
//! second walk over the function's body and a different question about agreement. Answering it with the first
//! `return` would be wrong whenever the others differ, and a signature is not the place to be wrong.
//! * `auto x{…};` deduces `initializer_list<T>` or `T` depending on the braces, which is a rule about the
//! *initializer* rather than about the expression inside it.
//!
//! # The refusal is part of the contract
//!
//! [`Known::Unknown`] is the answer whenever the initializer's type is not one this can read, and it carries
//! **why** — the name is not declared here, the expression is inside a macro this analysis did not expand, the
//! type depends on a template argument. A consumer that shows `auto` for those is showing what the file says; a
//! consumer that invented a type would be showing something the file does not.
use Path;
use ;
use crateDeclFact;
use crateScopeTree;
use crateProjectIndex;
use crate;
/// **The type an `auto` declaration's initializer has**, or why it cannot be told.
///
/// `Known::No` is the answer for a declaration whose type is **not** a placeholder: the question does not apply
/// to it, which is not the same as an answer that could not be found. A caller asking about every declaration in
/// a file — which is what a consumer walking a summary does — gets `No` for most of them, and that is the
/// distinction [`Known`] exists to keep.
/// The `Initializer` node written inside `declaration`, if there is one.
///
/// A declaration's initializer is a node of its own — `= expr`, `(expr)` or `{…}` — so finding it is a search of
/// the declaration's own subtree rather than a scan for an `=` token: the `=` of a default template argument, of
/// a `requires` clause or of an operator name is not this declaration's initializer, and a node cannot be
/// confused with one.
/// **The expression an initializer holds** — its last child that is a node rather than a token.
///
/// `= f(x)` holds the `=` and the call; `{1, 2}` holds the braces and the list. The last node is the payload in
/// both, and a braced list comes back as itself, which the module note says is refused rather than read.