brokk-bifrost-cpp 0.9.1

C++ language knowledge for brokk-bifrost: declarations and macro-sentinel recovery, include-graph visibility, out-of-line member identity reconciliation, and usage-graph resolution
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Overload disambiguation for C++ call sites.
//!
//! The only C++-only module that lived outside `cpp_graph/`: given a candidate
//! set of same-named callables and the argument list at a call site, narrow by
//! parameter type shape. Pure text and AST work over the signatures the
//! declaration walk already emitted.

use crate::declarations::{node_text as cpp_node_text, normalize_cpp_whitespace};
use brokk_bifrost_core::analyzer::CodeUnit;
use tree_sitter::Node;

#[derive(Clone, PartialEq, Eq, Hash)]
pub struct CppArgType {
    pub name: String,
    pub unit: Option<CodeUnit>,
    pub indirection: i32,
    pub pointee_const: bool,
}

pub fn cpp_signature_param_types(signature: &str) -> Option<Vec<String>> {
    let inner = cpp_signature_parameter_text(signature)
        .unwrap_or(signature)
        .trim();
    if inner.is_empty() || inner == "void" {
        return Some(Vec::new());
    }
    Some(
        cpp_split_top_level_commas(inner)
            .map(cpp_parameter_type_text)
            .collect(),
    )
}

pub fn cpp_parameter_type_text(parameter: &str) -> String {
    let mut text = parameter
        .split_once('=')
        .map(|(before, _)| before)
        .unwrap_or(parameter)
        .trim()
        .trim_end_matches(';')
        .trim();
    let pointer_depth = cpp_type_text_pointer_depth(text);
    if let Some((before, last)) = text.rsplit_once(char::is_whitespace)
        && cpp_parameter_name_token(last)
    {
        text = before.trim();
    }
    let pointee_const = pointer_depth > 0 && cpp_type_text_pointee_is_const(text);
    format!(
        "{}{}{}",
        if pointee_const { "const " } else { "" },
        normalize_cpp_type_name(text),
        "*".repeat(pointer_depth as usize)
    )
}

pub fn normalize_cpp_type_name(text: &str) -> String {
    let normalized = normalize_cpp_whitespace(text);
    let base = cpp_type_text_base(&normalized)
        .trim_start_matches("const ")
        .trim();
    strip_tag_type_prefix(base.strip_suffix(" const").unwrap_or(base)).to_string()
}

pub fn cpp_type_text_pointer_depth(text: &str) -> i32 {
    cpp_type_text_shape(text).1
}

fn cpp_type_text_shape(text: &str) -> (usize, i32) {
    let mut depth = 0i32;
    let mut nesting = 0i32;
    let mut base_end = text.len();
    for (offset, ch) in text.char_indices() {
        match ch {
            '<' | '(' | '[' => nesting += 1,
            '>' | ')' | ']' => nesting -= 1,
            '*' if nesting <= 0 => {
                base_end = base_end.min(offset);
                depth += 1;
            }
            '&' if nesting <= 0 => base_end = base_end.min(offset),
            _ => {}
        }
    }
    (base_end, depth)
}

pub fn cpp_literal_arg_type(node: Node<'_>, source: &str) -> Option<CppArgType> {
    let scalar = |name: &str| CppArgType {
        name: name.to_string(),
        unit: None,
        indirection: 0,
        pointee_const: false,
    };
    match node.kind() {
        "number_literal" => {
            let text = cpp_node_text(node, source);
            if cpp_number_literal_is_float(text) {
                Some(scalar("double"))
            } else {
                Some(scalar("int"))
            }
        }
        "true" | "false" => Some(scalar("bool")),
        "char_literal" => Some(scalar("char")),
        "string_literal" => {
            let text = cpp_node_text(node, source).trim_start();
            (text.starts_with('"') || text.starts_with("R\"")).then(|| CppArgType {
                name: "char".to_string(),
                unit: None,
                indirection: 1,
                pointee_const: true,
            })
        }
        "unary_expression" => {
            let operator = node.child_by_field_name("operator")?;
            let inner = node
                .child_by_field_name("argument")
                .or_else(|| node.named_child(0))?;
            matches!(operator.kind(), "+" | "-")
                .then(|| cpp_literal_arg_type(inner, source))
                .flatten()
        }
        _ => None,
    }
}

pub fn cpp_filter_candidates_by_args(
    candidates: Vec<CodeUnit>,
    arg_types: &[Option<CppArgType>],
    resolve_type: &dyn Fn(&str) -> Option<CodeUnit>,
    assignable: &dyn Fn(&CodeUnit, &CodeUnit) -> bool,
) -> Vec<CodeUnit> {
    if candidates.len() <= 1 || arg_types.iter().any(Option::is_none) {
        return candidates;
    }

    let filtered: Vec<_> = candidates
        .iter()
        .filter(|candidate| {
            cpp_signature_param_types(candidate.signature().unwrap_or_default()).is_some_and(
                |params| {
                    params.len() == arg_types.len()
                        && params.iter().zip(arg_types.iter()).all(|(param, arg)| {
                            cpp_param_matches_arg(param, arg, resolve_type, assignable)
                        })
                },
            )
        })
        .cloned()
        .collect();
    if filtered.is_empty() {
        candidates
    } else {
        filtered
    }
}

fn cpp_param_matches_arg(
    param: &str,
    arg: &Option<CppArgType>,
    resolve_type: &dyn Fn(&str) -> Option<CodeUnit>,
    assignable: &dyn Fn(&CodeUnit, &CodeUnit) -> bool,
) -> bool {
    let Some(arg) = arg else {
        return false;
    };
    if cpp_type_text_pointer_depth(param) != arg.indirection {
        return false;
    }
    if arg.pointee_const && !cpp_type_text_pointee_is_const(param) {
        return false;
    }
    let param_name = normalize_cpp_type_name(param);
    match (resolve_type(&param_name), arg.unit.as_ref()) {
        (Some(param_unit), Some(arg_unit)) => assignable(arg_unit, &param_unit),
        _ => param_name == arg.name,
    }
}

fn cpp_type_text_pointee_is_const(text: &str) -> bool {
    let normalized = normalize_cpp_whitespace(text);
    let base = cpp_type_text_base(&normalized).trim();
    base.starts_with("const ") || base.ends_with(" const")
}

fn cpp_type_text_base(text: &str) -> &str {
    text[..cpp_type_text_shape(text).0].trim()
}

pub fn cpp_split_top_level_commas(value: &str) -> impl Iterator<Item = &str> {
    struct TopLevelCommaSplit<'a> {
        value: &'a str,
        start: usize,
        angle: usize,
        paren: usize,
        brace: usize,
        bracket: usize,
    }

    impl<'a> Iterator for TopLevelCommaSplit<'a> {
        type Item = &'a str;

        fn next(&mut self) -> Option<Self::Item> {
            if self.start > self.value.len() {
                return None;
            }
            for (offset, ch) in self.value[self.start..].char_indices() {
                let absolute = self.start + offset;
                match ch {
                    '<' => self.angle += 1,
                    '>' => self.angle = self.angle.saturating_sub(1),
                    '(' => self.paren += 1,
                    ')' => self.paren = self.paren.saturating_sub(1),
                    '{' => self.brace += 1,
                    '}' => self.brace = self.brace.saturating_sub(1),
                    '[' => self.bracket += 1,
                    ']' => self.bracket = self.bracket.saturating_sub(1),
                    ',' if self.angle == 0
                        && self.paren == 0
                        && self.brace == 0
                        && self.bracket == 0 =>
                    {
                        let item = self.value[self.start..absolute].trim();
                        self.start = absolute + ch.len_utf8();
                        return Some(item);
                    }
                    _ => {}
                }
            }
            let item = self.value[self.start..].trim();
            self.start = self.value.len() + 1;
            Some(item)
        }
    }

    TopLevelCommaSplit {
        value,
        start: 0,
        angle: 0,
        paren: 0,
        brace: 0,
        bracket: 0,
    }
    .filter(|item| !item.is_empty())
}

/// The byte offsets of a signature's outermost parameter-list parentheses.
fn cpp_signature_parameter_span(signature: &str) -> Option<(usize, usize)> {
    let open = signature.find('(')?;
    let mut depth = 0i32;
    for (offset, ch) in signature[open..].char_indices() {
        match ch {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth == 0 {
                    return Some((open, open + offset));
                }
            }
            _ => {}
        }
    }
    None
}

fn cpp_signature_parameter_text(signature: &str) -> Option<&str> {
    let (open, close) = cpp_signature_parameter_span(signature)?;
    Some(signature[open + 1..close].trim())
}

/// What a signature carries after its parameter list: the trailing
/// cv-/ref-qualifiers and `noexcept` that the signature identity records
/// (#1827).
///
/// Two member declarations with the same parameter types but different
/// trailing qualifiers are distinct declarations, so a caller deciding whether
/// one declaration hides another has to compare this alongside the parameter
/// types.
pub fn cpp_signature_trailing_qualifiers(signature: &str) -> &str {
    match cpp_signature_parameter_span(signature) {
        Some((_, close)) => signature[close + 1..].trim(),
        None => "",
    }
}

fn cpp_parameter_name_token(token: &str) -> bool {
    let token = token.trim_start_matches('*').trim_start_matches('&').trim();
    token
        .chars()
        .next()
        .is_some_and(|ch| ch == '_' || ch.is_ascii_lowercase())
        && token
            .chars()
            .all(|ch| ch == '_' || ch.is_ascii_alphanumeric())
}

fn strip_tag_type_prefix(value: &str) -> &str {
    let value = value.trim_start_matches("const ");
    value
        .strip_prefix("struct ")
        .or_else(|| value.strip_prefix("class "))
        .or_else(|| value.strip_prefix("enum "))
        .unwrap_or(value)
        .trim()
}

fn cpp_number_literal_is_float(text: &str) -> bool {
    let text = text.trim();
    text.contains('.') || text.contains('e') || text.contains('E') || text.ends_with(['f', 'F'])
}

#[cfg(test)]
mod tests {
    use super::*;
    use brokk_bifrost_core::analyzer::ProjectFile;
    use brokk_bifrost_core::analyzer::model::CodeUnitType;

    fn test_file() -> ProjectFile {
        ProjectFile::new(std::env::temp_dir(), "test.cpp")
    }

    fn function(name: &str, signature: &str) -> CodeUnit {
        CodeUnit::with_signature(
            test_file(),
            CodeUnitType::Function,
            "ns",
            name,
            Some(signature.to_string()),
            false,
        )
    }

    fn class(name: &str) -> CodeUnit {
        CodeUnit::new(test_file(), CodeUnitType::Class, "ns", name)
    }

    #[test]
    fn cpp_filter_candidates_matches_named_unindexed_types() {
        let candidates = vec![
            function("format", "std::string format(const std::string& value)"),
            function("format", "std::string format(int value)"),
        ];
        let filtered = cpp_filter_candidates_by_args(
            candidates,
            &[Some(CppArgType {
                name: "std::string".to_string(),
                unit: None,
                indirection: 0,
                pointee_const: false,
            })],
            &|_| None,
            &|_, _| false,
        );
        assert_eq!(1, filtered.len());
        assert!(filtered[0].signature().unwrap().contains("std::string&"));
    }

    #[test]
    fn cpp_filter_candidates_matches_assignable_units() {
        let arg = class("Arg");
        let param = class("Param");
        let filtered = cpp_filter_candidates_by_args(
            vec![function("take", "void take(Param value)")],
            &[Some(CppArgType {
                name: "Arg".to_string(),
                unit: Some(arg.clone()),
                indirection: 0,
                pointee_const: false,
            })],
            &|name| (name == "Param").then(|| param.clone()),
            &|from, to| from == &arg && to == &param,
        );
        assert_eq!(1, filtered.len());
    }

    #[test]
    fn cpp_filter_candidates_rejects_pointer_depth_mismatch() {
        let candidates = vec![
            function("take", "void take(int* value)"),
            function("take", "void take(int value)"),
        ];
        let filtered = cpp_filter_candidates_by_args(
            candidates,
            &[Some(CppArgType {
                name: "int".to_string(),
                unit: None,
                indirection: 0,
                pointee_const: false,
            })],
            &|_| None,
            &|_, _| false,
        );
        assert_eq!(1, filtered.len());
        assert_eq!("void take(int value)", filtered[0].signature().unwrap());
    }

    #[test]
    fn cpp_filter_candidates_uses_const_string_literal_pointer_evidence() {
        let literal = Some(CppArgType {
            name: "char".to_string(),
            unit: None,
            indirection: 1,
            pointee_const: true,
        });
        let direct = cpp_filter_candidates_by_args(
            vec![
                function("select", "int select(int value)"),
                function("select", "int select(const char* value)"),
            ],
            std::slice::from_ref(&literal),
            &|_| None,
            &|_, _| false,
        );
        assert_eq!(1, direct.len());
        assert_eq!(
            "int select(const char* value)",
            direct[0].signature().unwrap()
        );

        for candidates in [
            vec![
                function("select", "int select(int value)"),
                function("select", "int select(char* value)"),
            ],
            vec![
                function("format", "int format(int value)"),
                function("format", "int format(std::string value)"),
            ],
        ] {
            let filtered = cpp_filter_candidates_by_args(
                candidates.clone(),
                std::slice::from_ref(&literal),
                &|_| None,
                &|_, _| false,
            );
            assert_eq!(
                candidates, filtered,
                "unmodeled or invalid conversions must remain conservative"
            );
        }
    }

    #[test]
    fn cpp_parameter_type_keeps_pointer_const_distinct_from_pointee_const() {
        assert_eq!("char*", cpp_parameter_type_text("char * const value"));
        assert_eq!(
            "const char*",
            cpp_parameter_type_text("const char * const value")
        );
        assert_eq!("char", normalize_cpp_type_name("char * const"));
    }

    #[test]
    fn cpp_filter_candidates_keeps_all_for_unknown_arguments() {
        let candidates = vec![
            function("format", "void format(std::string value)"),
            function("format", "void format(int value)"),
        ];
        let filtered =
            cpp_filter_candidates_by_args(candidates.clone(), &[None], &|_| None, &|_, _| false);
        assert_eq!(candidates, filtered);
    }

    #[test]
    fn cpp_filter_candidates_keeps_all_when_no_candidate_matches() {
        let candidates = vec![
            function("format", "void format(std::string value)"),
            function("format", "void format(int value)"),
        ];
        let filtered = cpp_filter_candidates_by_args(
            candidates.clone(),
            &[Some(CppArgType {
                name: "double".to_string(),
                unit: None,
                indirection: 0,
                pointee_const: false,
            })],
            &|_| None,
            &|_, _| false,
        );
        assert_eq!(candidates, filtered);
    }
}