aft-parse 0.67.1

Syntax parsing, imports and call resolution for Agent Foundational Toolkit
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use std::collections::BTreeSet;
use std::path::Path;

use super::super::{dependency, typescript, Resolution, Resolver, Target};
use super::extract::join;
use super::{csharp_projects::ProjectId, relative_module, Declaration, Scope, StaticFacts};

pub(in crate::callgraph_store) const STATIC_IMPORT_PREFIX: &str = "@static-import:";
// An import can resolve to a project module without resolving to a class.
const MISSING_STATIC_EXPORT: u32 = u32::MAX;

struct Type<'a> {
    file: String,
    facts: &'a StaticFacts,
    declaration: &'a Declaration,
}

impl<'a> Resolver<'a> {
    fn declares_namespace(&self, language: &str, namespace: &str) -> bool {
        let key = StaticFacts::namespace_key(language, namespace);
        self.depend(key.clone());
        let declares = |parse: &super::super::ParseBlob| {
            parse.language == language
                && parse.dispatch.static_facts.as_ref().is_some_and(|facts| {
                    facts
                        .namespaces
                        .binary_search_by(|name| name.as_str().cmp(namespace))
                        .is_ok()
                })
        };
        match self.source {
            Some(source) => source
                .files_with_static_type(&key)
                .iter()
                .any(|path| self.lookup(path).is_some_and(declares)),
            None => self.files.values().copied().any(declares),
        }
    }

    fn absent_namespace(&self, language: &str, type_name: &str) -> bool {
        let Some((namespace, _)) = type_name.rsplit_once('.') else {
            return false;
        };
        !self.declares_namespace(language, namespace)
    }
    fn csharp_project(&self, file: &str) -> Option<ProjectId> {
        self.csharp_projects?.owner(file, |key| self.depend(key))
    }

    fn csharp_project_types(&self, project: ProjectId, name: &str) -> Vec<Type<'a>> {
        let Some(visible) = self
            .csharp_projects
            .and_then(|projects| projects.reachable(project, |key| self.depend(key)))
        else {
            return Vec::new();
        };
        self.static_types("csharp", name)
            .into_iter()
            .filter(|ty| {
                self.csharp_project(&ty.file)
                    .is_some_and(|owner| visible.contains(&owner))
            })
            .collect()
    }
    fn static_types(&self, language: &str, name: &str) -> Vec<Type<'a>> {
        let key = StaticFacts::type_key(language, name);
        // A miss matters too: adding this declaration can turn a formerly
        // unknown receiver into a static call without changing its source file.
        self.depend(StaticFacts::dependency(language, name));
        let files: Vec<_> = match self.source {
            Some(source) => source.files_with_static_type(&key),
            None => self
                .files
                .iter()
                .filter(|(_, parse)| {
                    parse.language == language
                        && parse
                            .dispatch
                            .static_facts
                            .as_ref()
                            .is_some_and(|facts| facts.types.iter().any(|ty| ty.name == name))
                })
                .map(|(path, _)| path.clone())
                .collect(),
        };
        files
            .into_iter()
            .flat_map(|file| {
                let facts = self
                    .lookup(&file)
                    .filter(|parse| parse.language == language)
                    .and_then(|parse| parse.dispatch.static_facts.as_deref());
                facts.into_iter().flat_map(move |facts| {
                    let file = file.clone();
                    facts
                        .types
                        .iter()
                        .filter(move |ty| ty.name == name)
                        .map(move |declaration| Type {
                            file: file.clone(),
                            facts,
                            declaration,
                        })
                })
            })
            .collect()
    }

    fn ts_static_types(&self, file: &str, name: &str) -> Vec<Type<'a>> {
        let Some((path, ty)) = self.project_type(file, name) else {
            return Vec::new();
        };
        self.depend(dependency::file(&path));
        let Some(facts) = self
            .lookup(&path)
            .and_then(|parse| parse.dispatch.static_facts.as_deref())
        else {
            return Vec::new();
        };
        facts
            .types
            .iter()
            .filter(|declaration| declaration.name == ty.name)
            .map(|declaration| Type {
                file: path.clone(),
                facts,
                declaration,
            })
            .collect()
    }

    pub(in super::super) fn static_import_projection(
        &self,
        file: &str,
        key: &str,
    ) -> Option<(String, u32)> {
        let (path, symbol) = self.type_target(file, key)?;
        self.depend(dependency::file(&path));
        let index = self
            .lookup(&path)
            .and_then(|parse| parse.dispatch.static_facts.as_deref())
            .and_then(|facts| {
                let mut types = facts
                    .types
                    .iter()
                    .enumerate()
                    .filter(|(_, ty)| ty.name == symbol);
                let (index, _) = types.next()?;
                types.next().is_none().then_some(index as u32)
            })
            .unwrap_or(MISSING_STATIC_EXPORT);
        Some((path, index))
    }

    fn ts_import_type(&self, file: &str, name: &str, module: &str) -> Option<Vec<Type<'a>>> {
        // A host can supply a bare module at runtime without any file for
        // import resolution to find. Only relative imports can prove a class.
        if !relative_module(module) {
            return None;
        }
        let key = format!("{STATIC_IMPORT_PREFIX}{name}");
        let (path, index) = match self.projections() {
            Some(projections) => self.replay_static_import(projections, file, &key),
            None => self.static_import_projection(file, &key),
        }?;
        let facts = self.lookup(&path)?.dispatch.static_facts.as_deref()?;
        let declaration = facts.types.get(index as usize)?;
        Some(vec![Type {
            file: path,
            facts,
            declaration,
        }])
    }

    fn csharp_type(&self, file: &str, scope: &Scope, owner: &str, name: &str) -> Vec<Type<'a>> {
        let Some(project) = self.csharp_project(file) else {
            return Vec::new();
        };
        if let Some(name) = name.strip_prefix("global::") {
            return self.csharp_project_types(project, name);
        }
        if let Some((_, target)) = scope.aliases.iter().find(|(alias, _)| alias == name) {
            if target.is_empty() {
                return Vec::new();
            }
            return self.csharp_project_types(project, target.trim_start_matches("global::"));
        }
        let mut prefix = owner;
        while !prefix.is_empty() {
            let found = self.csharp_project_types(project, &join(prefix, name));
            if !found.is_empty() {
                return found;
            }
            prefix = prefix.rsplit_once('.').map_or("", |(outer, _)| outer);
        }
        // A global name in another C# compilation is not visible just because
        // its source is present. A written qualifier can name it explicitly.
        let mut found = self.csharp_project_types(project, name);
        if !name.contains('.') {
            found.retain(|candidate| candidate.file == file);
        }
        if !found.is_empty() {
            return found;
        }
        let mut seen = BTreeSet::new();
        for namespace in &scope.imports {
            // A static using lets a caller name nested types of the imported type.
            let prefix = namespace
                .trim_start_matches("static:")
                .trim_start_matches("global::");
            for candidate in self.csharp_project_types(project, &join(prefix, name)) {
                if seen.insert((candidate.file.clone(), candidate.declaration.name.as_str())) {
                    found.push(candidate);
                }
            }
        }
        found
    }

    fn java_type(&self, file: &str, scope: &Scope, name: &str) -> Option<Vec<Type<'a>>> {
        if let Some((_, target)) = scope
            .aliases
            .iter()
            .find(|(alias, target)| alias == name || target == name)
        {
            let found = unambiguous(self.static_types("java", target))?;
            return (!found.is_empty() || self.absent_namespace("java", target)).then_some(found);
        }
        let package_prefix = format!("{}.", scope.namespace);
        let mut found = self.static_types(
            "java",
            &if name.starts_with(&package_prefix) {
                name.to_string()
            } else {
                join(&scope.namespace, name)
            },
        );
        found.retain(|candidate| java_package_visible(file, &candidate.file, &scope.namespace));
        if !found.is_empty() {
            return unambiguous(found);
        }
        let core = name.strip_prefix("java.lang.").unwrap_or(name);
        if java_lang_class(core) {
            let found = unambiguous(self.static_types("java", &join("java.lang", core)))?;
            return (!found.is_empty() || !self.declares_namespace("java", "java.lang"))
                .then_some(found);
        }
        None
    }

    fn csharp_closed(
        &self,
        owner: &Type<'a>,
        name: &str,
        seen: &mut BTreeSet<(String, String)>,
    ) -> bool {
        let ty = owner.declaration;
        if ty
            .values
            .binary_search_by(|value| value.as_str().cmp(name))
            .is_ok()
        {
            return false;
        }
        let key = (owner.file.clone(), ty.name.clone());
        if !seen.insert(key.clone()) {
            return false;
        }
        let closed = ty.bases.iter().all(|base| {
            // A base expression is outside the declaring type's body, so it
            // can see the enclosing type and namespace, not its own members.
            let prefix = ty.name.rsplit_once('.').map_or("", |(prefix, _)| prefix);
            let bases = self.csharp_type(
                &owner.file,
                &owner.facts.scopes[ty.scope as usize],
                prefix,
                base,
            );
            bases.len() == 1
                && bases
                    .iter()
                    .all(|base| self.csharp_closed(base, name, seen))
        });
        seen.remove(&key);
        closed
    }

    fn java_clear_receiver_scope(&self, owner: Type<'a>, name: &str) -> bool {
        let mut seen = BTreeSet::new();
        let mut pending = vec![owner];
        while let Some(ty) = pending.pop() {
            if !seen.insert((ty.file.clone(), ty.declaration.name.as_str())) {
                continue;
            }
            if ty
                .declaration
                .values
                .binary_search_by(|value| value.as_str().cmp(name))
                .is_ok()
            {
                return false;
            }
            let scope = &ty.facts.scopes[ty.declaration.scope as usize];
            for base in &ty.declaration.bases {
                // An unreadable superclass could contribute a field with
                // this name. It cannot establish absence of shadowing.
                let Some(bases) = self.java_type(&ty.file, scope, base) else {
                    return false;
                };
                if bases.is_empty() {
                    return false;
                }
                pending.extend(bases);
            }
        }
        true
    }

    pub(in super::super) fn static_call(
        &self,
        file: &str,
        language: &str,
        facts: &'a StaticFacts,
        receiver: u32,
        member: &str,
    ) -> Option<Resolution> {
        let receiver = &facts.receivers[receiver as usize];
        let scope = &facts.scopes[receiver.scope as usize];
        let mut pending = match language {
            "java" => {
                let types = self.java_type(file, scope, &receiver.name)?;
                if let Some(owner) = receiver.owner {
                    let root = receiver.name.split('.').next()?;
                    if !self.java_clear_receiver_scope(
                        Type {
                            file: file.to_string(),
                            facts,
                            declaration: &facts.types[owner as usize],
                        },
                        root,
                    ) {
                        return None;
                    }
                }
                types
            }
            "csharp" => {
                let owner = &facts.types[receiver.owner? as usize];
                let project = self.csharp_project(file)?;
                self.csharp_projects?
                    .reachable(project, |key| self.depend(key))?;
                // An empty alias target means its declaration is conditional,
                // ambiguous, or unreadable. It can still hide a visible type.
                if scope.aliases.iter().any(|(alias, target)| {
                    target.is_empty()
                        && (alias == &receiver.name
                            || receiver
                                .name
                                .strip_prefix(alias)
                                .is_some_and(|rest| rest.starts_with('.')))
                }) {
                    return None;
                }
                let types =
                    unambiguous(self.csharp_type(file, scope, &owner.name, &receiver.name))?;
                if types.is_empty() {
                    let (_, target) = scope
                        .aliases
                        .iter()
                        .find(|(alias, _)| alias == &receiver.name)?;
                    // An alias referring to a declaration in another project
                    // is not proof that the caller references that project.
                    if !self
                        .static_types("csharp", target.trim_start_matches("global::"))
                        .is_empty()
                        || !self.absent_namespace("csharp", target.trim_start_matches("global::"))
                    {
                        return None;
                    }
                }
                let name = receiver
                    .name
                    .trim_start_matches("global::")
                    .split(['.', '`'])
                    .next()?;
                if !receiver.name.starts_with("global::")
                    && scope
                        .imports
                        .iter()
                        .filter_map(|import| import.strip_prefix("static:"))
                        .any(|import| {
                            let providers = self.csharp_type(file, scope, &owner.name, import);
                            providers.len() != 1
                                || providers
                                    .iter()
                                    .any(|ty| !self.csharp_closed(ty, name, &mut BTreeSet::new()))
                        })
                {
                    return None;
                }
                // Check all parts of partial types, as well as containing
                // types whose members are visible inside the nested type.
                let mut qualified_owner = owner.name.as_str();
                loop {
                    let parts = self.csharp_project_types(project, qualified_owner);
                    if parts.is_empty()
                        || parts
                            .iter()
                            .any(|part| !self.csharp_closed(part, name, &mut BTreeSet::new()))
                    {
                        return None;
                    }
                    let Some((outer, _)) = qualified_owner.rsplit_once('.') else {
                        break;
                    };
                    if outer == scope.namespace || outer.is_empty() {
                        break;
                    }
                    qualified_owner = outer;
                }
                types
            }
            _ if typescript::supported(language) => match receiver.owner {
                Some(index) => vec![Type {
                    file: file.to_string(),
                    facts,
                    declaration: &facts.types[index as usize],
                }],
                None => self.ts_import_type(file, &receiver.name, receiver.module.as_deref()?)?,
            },
            _ => return None,
        };
        if pending.is_empty() {
            return Some(Resolution {
                external: 1,
                ..Resolution::default()
            });
        }
        let mut answer = Resolution::default();
        let mut seen = BTreeSet::new();
        while let Some(ty) = pending.pop() {
            if !seen.insert((ty.file.clone(), ty.declaration.name.clone())) {
                continue;
            }
            for (name, symbol) in &ty.declaration.methods {
                if name == member {
                    answer.protected.insert((ty.file.clone(), symbol.clone()));
                    answer.targets.insert(Target {
                        file: ty.file.clone(),
                        symbol: symbol.clone(),
                        provenance: "exact",
                    });
                }
            }
            let scope = &ty.facts.scopes[ty.declaration.scope as usize];
            for base in &ty.declaration.bases {
                pending.extend(match language {
                    "java" => self.java_type(&ty.file, scope, base).unwrap_or_default(),
                    "csharp" => unambiguous(
                        self.csharp_type(
                            &ty.file,
                            scope,
                            ty.declaration
                                .name
                                .rsplit_once('.')
                                .map_or("", |(prefix, _)| prefix),
                            base,
                        ),
                    )
                    .unwrap_or_default(),
                    _ => self.ts_static_types(&ty.file, base),
                });
            }
        }
        (!answer.targets.is_empty()).then_some(answer)
    }
}

fn unambiguous(types: Vec<Type<'_>>) -> Option<Vec<Type<'_>>> {
    (types.len() <= 1).then_some(types)
}

fn java_package_visible(caller: &str, declaration: &str, package: &str) -> bool {
    let caller = Path::new(caller).parent();
    let declared = Path::new(declaration).parent();
    if caller == declared {
        return true;
    }
    fn module<'a>(mut directory: &'a Path, package: &str) -> Option<&'a Path> {
        for part in package.rsplit('.').filter(|part| !part.is_empty()) {
            if directory.file_name()? != part {
                return None;
            }
            directory = directory.parent()?;
        }
        if directory.file_name()? != "java" {
            return None;
        }
        let src = directory.parent()?.parent()?;
        (src.file_name()? == "src").then(|| src.parent()).flatten()
    }
    caller
        .and_then(|path| module(path, package))
        .is_some_and(|root| declared.and_then(|path| module(path, package)) == Some(root))
}

fn java_lang_class(name: &str) -> bool {
    matches!(
        name,
        "Boolean"
            | "Byte"
            | "Character"
            | "Class"
            | "ClassLoader"
            | "ClassValue"
            | "Compiler"
            | "Double"
            | "Enum"
            | "Float"
            | "InheritableThreadLocal"
            | "Integer"
            | "Long"
            | "Math"
            | "Module"
            | "ModuleLayer"
            | "Number"
            | "Object"
            | "Package"
            | "Process"
            | "ProcessBuilder"
            | "ProcessHandle"
            | "Record"
            | "Runtime"
            | "RuntimePermission"
            | "SecurityManager"
            | "Short"
            | "StackTraceElement"
            | "StackWalker"
            | "StrictMath"
            | "String"
            | "StringBuffer"
            | "StringBuilder"
            | "System"
            | "Thread"
            | "ThreadGroup"
            | "ThreadLocal"
            | "Throwable"
            | "Void"
    )
}