cpp_code_analysis 0.1.3

The analysis layer of cpp_ls: the preprocessor, symbol scopes, type inference and the semantic checks.
Documentation
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#![allow(dead_code)]
//! **Function template argument deduction** — what `T` is, given the call.
//!
//! ```cpp
//! template <typename T> void f(T value);
//! f(1);                     // T is int, and every question about this call is a question about `f(int)`
//! ```
//!
//! A template is not a function until it is instantiated, and the call is what instantiates it. The resolver in
//! [`crate::sema::overload`] ranks **spellings**, so a template candidate has to arrive at it already substituted —
//! that is what this module produces: the template arguments, in the order the template declares them, which
//! [`crate::sema::types::substituted_spelling`] turns into the parameter types the call is really facing.
//!
//! # The subset, and why it is small on purpose
//!
//! ```text
//! deduced     `T`                 the parameter is exactly the parameter
//!             `const T&` / `T&&`  the same type seen through a reference and a cv-qualifier
//!             `T*` / `T&` / `T[]` a shape the argument has and the parameter names
//! deduced     `T` twice           if both agree, once; if they disagree, not at all — the call does not compile
//! refused     anything else       `std::vector<T>`, `T::value_type`, a pack. Each is a real rule of the language and
//!                                 each needs a unifier that this module does not have; refusing gives the caller a
//!                                 candidate it can rank as `Unranked`, which is the quiet direction, instead of a
//!                                 substitution that is wrong for some calls and right for others
//! ```
//!
//! # Why a failure is `None` and not a partial answer
//!
//! A substitution is used to *rank* a call and, in signature help, to *show* a reader what the callee's parameters are.
//! A half-deduced template would show `f(int, T)` for a call whose second parameter is `double`, which is worse than
//! showing `f(T, T)` — the reader of the second can see that the analysis did not deduce, and the reader of the first
//! cannot.

use std::collections::HashMap;

use crate::sema::symbol::Known;

/// **The template arguments a call fixes**, in the order the template declares them.
///
/// `None` when any declared parameter stays unbound, when two deductions of one parameter disagree, or when the call
/// gives too few arguments to reach every parameter that mentions one.
pub(crate) fn deduce(
    names: &[String],
    parameters: &[String],
    arguments: &[Known<String>],
) -> Option<Vec<String>> {
    deduce_seeded(names, parameters, arguments, &[], HashMap::new())
}

/// **The template arguments a call fixes, given what the call wrote** — [`deduce`] with the explicit `<…>` list already
/// bound.
///
/// The explicit list is the half a call states outright: in `std::make_unique<Resource>(1)` the `_Tp` in
/// `__unique_ptr_t<_Tp>` is `Resource` before any argument is looked at, and what is left to deduce is the pack. Read
/// as an argument-only question, every parameter of that candidate stayed unbound — `_Tp` because nothing wrote it,
/// `_Args` because the pack's element is not a parameter the old loop could split — the substitution was refused, and
/// the candidate ranked `Unranked`. That is what made `make_unique` ambiguous between its three visible overloads and
/// left `auto unique = std::make_unique<Resource>(1);` with no type.
///
/// A name the call wrote but the declaration does not have is ignored: the two are paired by position, and a longer
/// written list is a call this module cannot read rather than a deduction to guess at.
pub(crate) fn deduce_with_written(
    names: &[String],
    parameters: &[String],
    arguments: &[Known<String>],
    written: &[String],
) -> Option<Vec<String>> {
    let mut bound: HashMap<String, String> = HashMap::new();
    let mut fixed: Vec<String> = Vec::new();
    for (name, value) in names.iter().zip(written) {
        bound.insert(name.clone(), value.clone());
        fixed.push(name.clone());
    }
    deduce_seeded(names, parameters, arguments, &fixed, bound)
}

/// The deduction itself, with a set of names already fixed.
fn deduce_seeded(
    names: &[String],
    parameters: &[String],
    arguments: &[Known<String>],
    // **The names the call wrote**, which are fixed rather than deduced: `f<long>(1)` states `T`, and the argument's
    // `int` converts to it instead of disagreeing with it. They are a list of their own rather than "whatever is already
    // bound", because a name bound by an *earlier argument* must still agree with the next one.
    fixed: &[String],
    mut bound: HashMap<String, String>,
) -> Option<Vec<String>> {
    let is_fixed = |name: &str| fixed.iter().any(|written| written == name);
    if names.is_empty() {
        return None;
    }

    for (parameter, argument) in parameters.iter().zip(arguments) {
        let Known::Yes(argument) = argument else {
            // Nothing is known about this argument, so this parameter cannot bind anything. A later parameter may still
            // bind it — `f(T, const T&)` called with an unknown and an `int` is a deduction this can make.
            continue;
        };

        let parameter = the_shape_of(parameter);
        let argument = the_shape_of(argument);

        // **The parameter is exactly a template parameter.** The whole of the commonest case.
        //
        // **A written template argument is not deduced.** `f<long>(1)` fixes `T = long`, and the argument's `int` does
        // not disagree with it — it converts to it. Deducting anyway made a written argument and an argument type that
        // are not identical a refusal, which is the opposite of what the call means.
        if names.iter().any(|name| name == &parameter) {
            if is_fixed(&parameter) {
                continue;
            }
            if !bind(&mut bound, &parameter, &argument) {
                return None;
            }
            continue;
        }

        // **The parameter names a template parameter inside a shape**: `T*`, `T&`, `T[]`. The argument has to have the
        // same shape before and after the name, and the part where the name was is its argument.
        if let Some((before, name, after)) = around_a_name(&parameter, names) {
            // A name the call already wrote is fixed — see the note above.
            if is_fixed(&name) {
                continue;
            }
            let Some(rest) = argument.strip_prefix(&before) else {
                continue;
            };
            let Some(rest) = rest.strip_suffix(&after) else {
                continue;
            };
            if rest.is_empty() {
                continue;
            }
            if !bind(&mut bound, &name, rest) {
                return None;
            }
            continue;
        }

        // Anything else mentions no Template parameter in a way this can split, and binds nothing.
    }

    // Every declared parameter must have a value: a substitution missing one is not an instantiation.
    let mut deduced = Vec::with_capacity(names.len());
    for name in names {
        deduced.push(bound.get(name.as_str())?.clone());
    }
    Some(deduced)
}

/// Bind one name, and refuse a second deduction that disagrees — `f(T, T)` called with `int` and `long` does not
/// compile, and the analysis should not pretend it does.
fn bind(bound: &mut HashMap<String, String>, name: &str, argument: &str) -> bool {
    match bound.get(name) {
        Some(known) => known == argument,
        None => {
            bound.insert(name.to_string(), argument.to_string());
            true
        }
    }
}

/// **`T*` as `("", "T", "*")`** — the text before and after the first template parameter the spelling names.
fn around_a_name(parameter: &str, names: &[String]) -> Option<(String, String, String)> {
    for name in names {
        let mut from = 0usize;
        while let Some(at) = parameter[from..].find(name.as_str()) {
            let start = from + at;
            let end = start + name.len();
            let before = &parameter[..start];
            let after = &parameter[end..];
            // Only a **whole word**: `T` must not match inside `Type`.
            let whole_start = start == 0
                || !parameter[..start]
                    .chars()
                    .next_back()
                    .is_some_and(|character| character.is_alphanumeric() || character == '_');
            let whole_end = end == parameter.len()
                || !parameter[end..]
                    .chars()
                    .next()
                    .is_some_and(|character| character.is_alphanumeric() || character == '_');
            if whole_start && whole_end && !(before.is_empty() && after.is_empty()) {
                return Some((
                    before.trim().to_string(),
                    name.clone(),
                    after.trim().to_string(),
                ));
            }
            from = end;
        }
    }
    None
}

/// A spelling without the reference and cv-qualifiers a call does not choose on — the same normalisation the resolver
/// ranks with, kept here so that a deduction and a rank agree about what two types are.
fn the_shape_of(spelling: &str) -> String {
    let mut text = spelling.trim().to_string();
    loop {
        let trimmed = text.trim();
        if let Some(before) = trimmed
            .strip_suffix("&&")
            .or_else(|| trimmed.strip_suffix('&'))
        {
            text = before.trim_end().to_string();
        } else {
            break;
        }
    }
    loop {
        let trimmed = text.trim_start();
        if let Some(after) = trimmed
            .strip_prefix("const ")
            .or_else(|| trimmed.strip_prefix("volatile "))
        {
            text = after.trim_start().to_string();
        } else {
            break;
        }
    }
    text.split_whitespace().collect::<Vec<_>>().join(" ")
}

/// **The template parameters a declaration owns**, read from the text before it.
///
/// A heuristic, and stated as one — the same one `signature.rs` grew for the same reason:
/// `DeclFact` has no template header, so the nearest `template` before the declaration's name is
/// the only evidence there is.
pub fn template_parameters_before(source: &str, name_offset: usize) -> Vec<String> {
    let before = &source[..name_offset.min(source.len())];
    let Some(at) = before.rfind("template") else {
        return Vec::new();
    };
    let header = &before[at..];
    let Some(open) = header.find('<') else {
        return Vec::new();
    };
    let Some(close) = header[open..].find('>') else {
        return Vec::new();
    };
    if header[open + close..].contains(';') {
        return Vec::new();
    }
    let inside = &header[open + 1..open + close];

    let mut names = Vec::new();
    let mut depth = 0i32;
    let mut part = String::new();
    for character in inside.chars() {
        match character {
            '<' | '(' | '[' => depth += 1,
            '>' | ')' | ']' => depth -= 1,
            ',' if depth == 0 => {
                if let Some(name) = the_name_in(&part) {
                    names.push(name);
                }
                part.clear();
                continue;
            }
            _ => {}
        }
        part.push(character);
    }
    if let Some(name) = the_name_in(&part) {
        names.push(name);
    }
    names
}

/// The last identifier of one `template` parameter, which is its name: `typename T` → `T`, `int N` → `N`.
fn the_name_in(parameter: &str) -> Option<String> {
    let without_default = parameter.split('=').next().unwrap_or(parameter);
    let identifier = without_default
        .split(|character: char| !(character.is_alphanumeric() || character == '_'))
        .rfind(|word| !word.is_empty())?;
    Some(identifier.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn yes(spelling: &str) -> Known<String> {
        Known::Yes(spelling.to_string())
    }

    fn names(values: &[&str]) -> Vec<String> {
        values.iter().map(|value| (*value).to_string()).collect()
    }

    fn parameters(values: &[&str]) -> Vec<String> {
        values.iter().map(|value| (*value).to_string()).collect()
    }

    #[test]
    fn the_parameter_that_is_just_the_parameter_is_the_commonest_case() {
        assert_eq!(
            deduce(&names(&["T"]), &parameters(&["T"]), &[yes("int")]),
            Some(names(&["int"]))
        );
    }

    #[test]
    fn a_reference_and_a_cv_qualifier_are_the_same_type_seen_two_ways() {
        assert_eq!(
            deduce(
                &names(&["T"]),
                &parameters(&["const T&"]),
                &[yes("std::string")]
            ),
            Some(names(&["std::string"]))
        );
    }

    #[test]
    fn a_shape_is_carried_by_the_argument_and_the_name_is_what_is_inside_it() {
        assert_eq!(
            deduce(&names(&["T"]), &parameters(&["T*"]), &[yes("int*")]),
            Some(names(&["int"]))
        );
    }

    #[test]
    fn a_name_deduced_twice_has_to_agree() {
        assert_eq!(
            deduce(
                &names(&["T"]),
                &parameters(&["T", "T"]),
                &[yes("int"), yes("int")]
            ),
            Some(names(&["int"])),
            "two deductions that agree are one deduction"
        );
        assert_eq!(
            deduce(
                &names(&["T"]),
                &parameters(&["T", "T"]),
                &[yes("int"), yes("long")]
            ),
            None,
            "and two that disagree do not compile, so this analysis should not pretend they do"
        );
    }

    #[test]
    fn an_argument_nothing_is_known_about_binds_nothing_and_a_later_one_can_still_bind_it() {
        assert_eq!(
            deduce(
                &names(&["T"]),
                &parameters(&["T", "const T&"]),
                &[
                    Known::Unknown(crate::sema::symbol::UnknownReason::DependentName),
                    yes("int")
                ],
            ),
            Some(names(&["int"])),
            "the second parameter still fixes T"
        );
    }

    #[test]
    fn a_name_that_stays_unbound_is_a_refusal_and_not_a_guess() {
        assert_eq!(
            deduce(&names(&["T"]), &parameters(&["T*"]), &[yes("int")]),
            None,
            "`int` is not a `T*`"
        );
        assert_eq!(
            deduce(&names(&["T"]), &parameters(&["int"]), &[yes("int")]),
            None,
            "a parameter that names no template parameter deduces nothing"
        );
    }

    #[test]
    fn a_single_level_template_id_is_deduced_by_what_surrounds_the_name() {
        // The split `around_a_name` makes is "everything before the name" and "everything after it", so a one-level
        // template-id is a shape like any other: `vector<T>` against `vector<int>` leaves `int` where `T` was. The
        // first version of this test asserted a refusal here, which was wrong about what this can do.
        assert_eq!(
            deduce(
                &names(&["T"]),
                &parameters(&["std::vector<T>"]),
                &[yes("std::vector<int>")]
            ),
            Some(names(&["int"]))
        );
    }

    /// **The explicit `<…>` list is a deduction, not a decoration.** `std::make_unique<Resource>(1)` states `_Tp` and
    /// leaves the pack; without the seed every name stayed unbound, the substitution was refused, and the candidate
    /// ranked `Unranked` — which made the three visible `make_unique` overloads tie.
    #[test]
    fn a_written_template_argument_is_bound_before_the_arguments_are_read() {
        assert_eq!(
            deduce_with_written(
                &names(&["_Tp", "_Args"]),
                &parameters(&["_Args&&"]),
                &[yes("int")],
                &names(&["Resource"]),
            ),
            Some(names(&["Resource", "int"])),
            "`_Tp` comes from the call's own `<Resource>`; `_Args` from the argument"
        );

        // A written name the declaration does not have is ignored rather than guessed at.
        assert_eq!(
            deduce_with_written(
                &names(&["T"]),
                &parameters(&["T"]),
                &[yes("int")],
                &names(&["long", "extra"]),
            ),
            Some(names(&["long"])),
            "the first written argument is the first template parameter's"
        );
    }

    #[test]
    fn a_shape_the_argument_does_not_have_is_refused_rather_than_approximated() {
        assert_eq!(
            deduce(
                &names(&["T"]),
                &parameters(&["std::vector<T>"]),
                &[yes("std::map<int, int>")]
            ),
            None,
            "`std::map<int, int>` does not begin with `std::vector<`, so nothing binds and the answer is no"
        );
    }
}