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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
//! Method-call resolution split in two: what each file's sites and
//! declarations mean, worked out in file order, and the matching of sites
//! to declarations, which then reads only the records written here.
//!
//! [`Resolver::project_type`] is the only rule that reads a file's imports,
//! fields, return types, type and import targets, or its directory, and
//! Python's method resolution order is the only rule that follows imports
//! across files. Both are answered here once per file, with the dependency
//! keys they recorded. A resolver over a source with
//! [`DispatchFiles::projections`] replays these answers and reads each file
//! only for its language, Go package, types and methods (the method rows
//! and the type graph), so it may run in any order: every other rule runs
//! the same code on the same facts and records the same keys.
//!
//! What pass 4 asks [`Resolver::project_type`] of each file `F`:
//! - each site's receiver, from the calling file;
//! - each method's owner and trait, from `F`;
//! - each base of every type `F` declares, from `F`;
//! - each base of a method's owner type, again from the method's file `F`
//!   (the unknown-receiver rule projects them from there, even when the
//!   owner type is declared elsewhere);
//! - for a Rust file with typed sites, the last segment of a trait or owner
//!   name, from the calling file. Those names are not known in file order,
//!   so every name such a file can resolve is projected, and any other name
//!   gets [`Recorded::NotProjectable`], whose answer and keys are fixed (see
//!   [`Resolver::replay_projection`]).

use std::collections::{BTreeMap, BTreeSet};

use super::{dependency, DispatchFacts, MethodHint, ParseBlob, Resolver, SiteHint, TypeHint};

/// The answer for one type lookup in a file. Static import keys carry the
/// class declaration's position in the static facts rather than its nominal type.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectedType {
    #[cfg(any(test, feature = "test-support"))]
    /// The file declaring the type and the type's position among that
    /// file's types, or `None` when the name names no project type.
    pub found: Option<(String, u32)>,
    #[cfg(not(any(test, feature = "test-support")))]
    /// The file declaring the type and the type's position among that
    /// file's types, or `None` when the name names no project type.
    found: Option<(String, u32)>,
    #[cfg(any(test, feature = "test-support"))]
    /// The keys the lookup recorded.
    pub dependencies: Box<[String]>,
    #[cfg(not(any(test, feature = "test-support")))]
    /// The keys the lookup recorded.
    dependencies: Box<[String]>,
}

/// The method resolution order a Python `super()` call reads for its owner
/// type, with the keys computing it recorded. The owner's own projection is
/// recorded separately, as a [`ProjectedType`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ProjectedMro {
    #[cfg(any(test, feature = "test-support"))]
    pub mro: Option<Vec<(String, String)>>,
    #[cfg(not(any(test, feature = "test-support")))]
    mro: Option<Vec<(String, String)>>,
    #[cfg(any(test, feature = "test-support"))]
    pub dependencies: Box<[String]>,
    #[cfg(not(any(test, feature = "test-support")))]
    dependencies: Box<[String]>,
}

/// One method-call site in `bound_dispatch_sites()` order, with what the
/// pass-4 driver reads about it from its file besides the site itself.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SiteRecord {
    pub site: SiteHint,
    /// Whether the file's reference with the site's ordinal is a Rust
    /// associated-function call. The resolver applies it to Rust files only.
    pub associated: bool,
    /// Whether pass 2 already linked the site's reference with an ordinary
    /// edge that Rust spells as an associated or `self.` call: such a Rust
    /// site with no project receiver is left out of the file's sites.
    pub keeps_ordinary_edge: bool,
}

/// Everything pass 4 reads about one file once receivers are projected.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileRecord {
    #[cfg(any(test, feature = "test-support"))]
    /// The file's language, Go package, types and methods; everything else
    /// is left empty.
    pub facts: ParseBlob,
    #[cfg(not(any(test, feature = "test-support")))]
    /// The file's language, Go package, types and methods; everything else
    /// is left empty.
    facts: ParseBlob,
    #[cfg(any(test, feature = "test-support"))]
    /// Every name pass 4 asks [`Resolver::project_type`] of this file, with
    /// its answer.
    pub projections: BTreeMap<String, ProjectedType>,
    #[cfg(not(any(test, feature = "test-support")))]
    /// Every name pass 4 asks [`Resolver::project_type`] of this file, with
    /// its answer.
    projections: BTreeMap<String, ProjectedType>,
    #[cfg(any(test, feature = "test-support"))]
    /// For each owner of a Python `super()` site of this file, the owner
    /// type's resolution order.
    pub mro: BTreeMap<String, ProjectedMro>,
    #[cfg(not(any(test, feature = "test-support")))]
    /// For each owner of a Python `super()` site of this file, the owner
    /// type's resolution order.
    mro: BTreeMap<String, ProjectedMro>,
    #[cfg(any(test, feature = "test-support"))]
    /// Set for a Rust file whose every resolvable name is in
    /// `projections`, so any other name has the fixed miss answer.
    pub complete_names: bool,
    #[cfg(not(any(test, feature = "test-support")))]
    /// Set for a Rust file whose every resolvable name is in
    /// `projections`, so any other name has the fixed miss answer.
    complete_names: bool,
    #[cfg(any(test, feature = "test-support"))]
    /// How many of `projections` are there only because `complete_names`
    /// is set, for measurement.
    pub resolvable_name_projections: u32,
    #[cfg(not(any(test, feature = "test-support")))]
    /// How many of `projections` are there only because `complete_names`
    /// is set, for measurement.
    resolvable_name_projections: u32,
    #[cfg(any(test, feature = "test-support"))]
    pub sites: Vec<SiteRecord>,
    #[cfg(not(any(test, feature = "test-support")))]
    sites: Vec<SiteRecord>,
    #[cfg(any(test, feature = "test-support"))]
    /// Sorted ordinals of the sites whose `associated` is set.
    pub associated: Vec<u32>,
    #[cfg(not(any(test, feature = "test-support")))]
    /// Sorted ordinals of the sites whose `associated` is set.
    associated: Vec<u32>,
}

/// A record looked up by name.
pub enum Recorded<'r, T> {
    Found(&'r T),
    /// A name of a Rust file the file cannot resolve to a type.
    NotProjectable,
    /// No record: the records are incomplete, which fails the build.
    Missing,
}

/// Recorded projections, as a source serving them hands them to the
/// resolver.
pub trait Projections {
    fn projected_type(&self, file: &str, name: &str) -> Recorded<'_, ProjectedType>;
    fn projected_mro(&self, file: &str, owner: &str) -> Recorded<'_, ProjectedMro>;
    /// Notes a record the resolver needed and did not find; the pass fails.
    fn missing(&self, what: String);
}

impl FileRecord {
    pub fn facts(&self) -> &ParseBlob {
        &self.facts
    }

    pub fn sites(&self) -> &[SiteRecord] {
        &self.sites
    }

    /// How many projected answers and resolution orders the record keeps,
    /// how many of those answer a Rust file's resolvable names only, and
    /// the heap they hold.
    pub fn projection_census(&self, seen: &mut aft_store::heap::Seen) -> (usize, usize, usize) {
        use aft_store::heap::HeapBytes as _;
        (
            self.projections.len() + self.mro.len(),
            self.resolvable_name_projections as usize,
            self.projections.heap_bytes(seen) + self.mro.heap_bytes(seen),
        )
    }

    pub fn associated(&self, ordinal: u32) -> bool {
        self.associated.binary_search(&ordinal).is_ok()
    }

    pub fn projected_type(&self, name: &str) -> Recorded<'_, ProjectedType> {
        match self.projections.get(name) {
            Some(found) => Recorded::Found(found),
            None if self.complete_names && !name.starts_with('@') => Recorded::NotProjectable,
            None => Recorded::Missing,
        }
    }

    pub fn projected_mro(&self, owner: &str) -> Recorded<'_, ProjectedMro> {
        self.mro
            .get(owner)
            .map_or(Recorded::Missing, Recorded::Found)
    }

    /// The names of the methods the file declares, in name order.
    pub fn method_names(&self) -> BTreeSet<&str> {
        self.facts
            .dispatch
            .methods
            .iter()
            .map(|method| method.name.as_str())
            .collect()
    }

    /// For a Go file, the names of the methods its interfaces require.
    pub fn go_required(&self) -> BTreeSet<&str> {
        if self.facts.language != "go" {
            return BTreeSet::new();
        }
        let dispatch = &self.facts.dispatch;
        dispatch
            .methods
            .iter()
            .filter(|method| {
                dispatch
                    .types
                    .iter()
                    .any(|ty| ty.interface && ty.name == method.owner)
            })
            .map(|method| method.name.as_str())
            .collect()
    }
}

test_support_visibility! {
/// The first byte of every scratch-encoded record (see
/// [`FileRecord::encode_scratch`]). The encoding lives only in one build's
/// scratch folder and is read back by the same process, so it needs no
/// version history; the byte only catches bytes that are not a record.
const SCRATCH_FORMAT: u8 = 1;
}

/// Appends the scratch encoding's parts: counts and lengths as
/// little-endian `u32`, flags as one byte, text as its length and bytes.
struct ScratchWriter<'o>(&'o mut Vec<u8>);

impl ScratchWriter<'_> {
    fn byte(&mut self, value: u8) {
        self.0.push(value);
    }

    fn flag(&mut self, value: bool) {
        self.0.push(u8::from(value));
    }

    fn number(&mut self, value: u32) {
        self.0.extend(value.to_le_bytes());
    }

    fn count(&mut self, count: usize) -> Result<(), String> {
        let count = u32::try_from(count)
            .map_err(|_| "a pass-4 record part has 4 billion or more items".to_string())?;
        self.number(count);
        Ok(())
    }

    fn text(&mut self, text: &str) -> Result<(), String> {
        self.count(text.len())?;
        self.0.extend_from_slice(text.as_bytes());
        Ok(())
    }

    fn optional(&mut self, text: Option<&str>) -> Result<(), String> {
        match text {
            None => self.byte(0),
            Some(text) => {
                self.byte(1);
                self.text(text)?;
            }
        }
        Ok(())
    }

    fn texts(&mut self, texts: &[String]) -> Result<(), String> {
        self.count(texts.len())?;
        for text in texts {
            self.text(text)?;
        }
        Ok(())
    }
}

/// Reads what [`ScratchWriter`] wrote, failing on anything it could not
/// have written.
struct ScratchReader<'b> {
    bytes: &'b [u8],
    at: usize,
}

impl<'b> ScratchReader<'b> {
    fn take(&mut self, len: usize) -> Result<&'b [u8], String> {
        let end = self
            .at
            .checked_add(len)
            .filter(|end| *end <= self.bytes.len())
            .ok_or_else(|| "a truncated pass-4 record".to_string())?;
        let taken = &self.bytes[self.at..end];
        self.at = end;
        Ok(taken)
    }

    fn byte(&mut self) -> Result<u8, String> {
        Ok(self.take(1)?[0])
    }

    fn flag(&mut self) -> Result<bool, String> {
        match self.byte()? {
            0 => Ok(false),
            1 => Ok(true),
            other => Err(format!("a pass-4 record flag reads {other}")),
        }
    }

    fn number(&mut self) -> Result<u32, String> {
        let bytes = self.take(4)?;
        Ok(u32::from_le_bytes(bytes.try_into().expect("four bytes")))
    }

    /// A count of items, each at least one byte long, so a damaged count
    /// cannot ask for more memory than the record's bytes.
    fn count(&mut self) -> Result<usize, String> {
        let count = self.number()? as usize;
        if count > self.bytes.len() - self.at {
            return Err(format!("a pass-4 record counts {count} items past its end"));
        }
        Ok(count)
    }

    fn text(&mut self) -> Result<String, String> {
        let len = self.number()? as usize;
        String::from_utf8(self.take(len)?.to_vec())
            .map_err(|_| "a pass-4 record holds text that is not UTF-8".to_string())
    }

    fn optional(&mut self) -> Result<Option<String>, String> {
        match self.byte()? {
            0 => Ok(None),
            1 => self.text().map(Some),
            other => Err(format!("a pass-4 record option reads {other}")),
        }
    }

    fn texts(&mut self) -> Result<Vec<String>, String> {
        let count = self.count()?;
        (0..count).map(|_| self.text()).collect()
    }
}

impl ProjectedType {
    fn encode_scratch(&self, out: &mut ScratchWriter<'_>) -> Result<(), String> {
        let Self {
            found,
            dependencies,
        } = self;
        match found {
            None => out.byte(0),
            Some((file, index)) => {
                out.byte(1);
                out.text(file)?;
                out.number(*index);
            }
        }
        out.texts(dependencies)
    }

    fn decode_scratch(input: &mut ScratchReader<'_>) -> Result<Self, String> {
        let found = match input.byte()? {
            0 => None,
            1 => Some((input.text()?, input.number()?)),
            other => return Err(format!("a projected type reads {other}")),
        };
        Ok(Self {
            found,
            dependencies: input.texts()?.into_boxed_slice(),
        })
    }
}

impl ProjectedMro {
    fn encode_scratch(&self, out: &mut ScratchWriter<'_>) -> Result<(), String> {
        let Self { mro, dependencies } = self;
        match mro {
            None => out.byte(0),
            Some(order) => {
                out.byte(1);
                out.count(order.len())?;
                for (file, name) in order {
                    out.text(file)?;
                    out.text(name)?;
                }
            }
        }
        out.texts(dependencies)
    }

    fn decode_scratch(input: &mut ScratchReader<'_>) -> Result<Self, String> {
        let mro = match input.byte()? {
            0 => None,
            1 => {
                let count = input.count()?;
                Some(
                    (0..count)
                        .map(|_| Ok((input.text()?, input.text()?)))
                        .collect::<Result<Vec<_>, String>>()?,
                )
            }
            other => return Err(format!("a resolution order reads {other}")),
        };
        Ok(Self {
            mro,
            dependencies: input.texts()?.into_boxed_slice(),
        })
    }
}

impl FileRecord {
    /// Appends the record's scratch encoding to `out`: what a cold build
    /// writes to its scratch folder, and [`Self::decode_scratch`] reads back
    /// equal. Never published.
    ///
    /// # Errors
    ///
    /// When a part has 4 billion or more items or bytes.
    pub fn encode_scratch(&self, out: &mut Vec<u8>) -> Result<(), String> {
        // Named in full, so a field added to the record must be added here.
        let Self {
            facts,
            projections,
            mro,
            complete_names,
            resolvable_name_projections,
            sites,
            associated,
        } = self;
        debug_assert!(
            *facts == declarations(facts),
            "a record keeps only the facts `declarations` keeps"
        );
        let mut out = ScratchWriter(out);
        out.byte(SCRATCH_FORMAT);
        out.text(&facts.language)?;
        out.optional(facts.dispatch.go_package.as_deref())?;
        out.count(facts.dispatch.types.len())?;
        for TypeHint {
            name,
            bases,
            interface,
            closed,
        } in &facts.dispatch.types
        {
            out.text(name)?;
            out.texts(bases)?;
            out.flag(*interface);
            out.flag(*closed);
        }
        out.count(facts.dispatch.methods.len())?;
        for MethodHint {
            owner,
            trait_name,
            name,
            symbol,
            has_body,
            shape,
            private,
        } in &facts.dispatch.methods
        {
            out.text(owner)?;
            out.optional(trait_name.as_deref())?;
            out.text(name)?;
            out.text(symbol)?;
            out.flag(*has_body);
            out.text(shape)?;
            out.flag(*private);
        }
        out.count(projections.len())?;
        for (name, projected) in projections {
            out.text(name)?;
            projected.encode_scratch(&mut out)?;
        }
        out.count(mro.len())?;
        for (owner, order) in mro {
            out.text(owner)?;
            order.encode_scratch(&mut out)?;
        }
        out.flag(*complete_names);
        out.number(*resolvable_name_projections);
        out.count(sites.len())?;
        for SiteRecord {
            site,
            associated,
            keeps_ordinary_edge,
        } in sites
        {
            let SiteHint {
                ordinal,
                caller,
                line,
                member,
                receiver,
                dynamic,
            } = site;
            out.number(*ordinal);
            out.optional(caller.as_deref())?;
            out.number(*line);
            out.optional(member.as_deref())?;
            out.optional(receiver.as_deref())?;
            out.flag(*dynamic);
            out.flag(*associated);
            out.flag(*keeps_ordinary_edge);
        }
        out.count(associated.len())?;
        for ordinal in associated {
            out.number(*ordinal);
        }
        if let Some(static_facts) = &facts.dispatch.static_facts {
            out.0.extend(static_facts.encode());
        }
        Ok(())
    }

    /// The record [`Self::encode_scratch`] encoded as `bytes`.
    ///
    /// # Errors
    ///
    /// When `bytes` is not exactly one record's scratch encoding.
    pub fn decode_scratch(bytes: &[u8]) -> Result<Self, String> {
        let mut input = ScratchReader { bytes, at: 0 };
        let format = input.byte()?;
        if format != SCRATCH_FORMAT {
            return Err(format!("not a pass-4 record (it starts with {format})"));
        }
        let language = input.text()?;
        let go_package = input.optional()?;
        let count = input.count()?;
        let mut types = Vec::with_capacity(count);
        for _ in 0..count {
            types.push(TypeHint {
                name: input.text()?,
                bases: input.texts()?,
                interface: input.flag()?,
                closed: input.flag()?,
            });
        }
        let count = input.count()?;
        let mut methods = Vec::with_capacity(count);
        for _ in 0..count {
            methods.push(MethodHint {
                owner: input.text()?,
                trait_name: input.optional()?,
                name: input.text()?,
                symbol: input.text()?,
                has_body: input.flag()?,
                shape: input.text()?,
                private: input.flag()?,
            });
        }
        let mut projections = BTreeMap::new();
        for _ in 0..input.count()? {
            let name = input.text()?;
            projections.insert(name, ProjectedType::decode_scratch(&mut input)?);
        }
        let mut mro = BTreeMap::new();
        for _ in 0..input.count()? {
            let owner = input.text()?;
            mro.insert(owner, ProjectedMro::decode_scratch(&mut input)?);
        }
        let complete_names = input.flag()?;
        let resolvable_name_projections = input.number()?;
        let count = input.count()?;
        let mut sites = Vec::with_capacity(count);
        for _ in 0..count {
            sites.push(SiteRecord {
                site: SiteHint {
                    ordinal: input.number()?,
                    caller: input.optional()?,
                    line: input.number()?,
                    member: input.optional()?,
                    receiver: input.optional()?,
                    dynamic: input.flag()?,
                },
                associated: input.flag()?,
                keeps_ordinary_edge: input.flag()?,
            });
        }
        let count = input.count()?;
        let associated = (0..count)
            .map(|_| input.number())
            .collect::<Result<Vec<_>, _>>()?;
        let mut facts = declarations_of(language, go_package, types, methods);
        if input.at != bytes.len() {
            facts.dispatch.static_facts = Some(Box::new(
                super::StaticFacts::decode(&bytes[input.at..])
                    .map_err(|error| error.to_string())?,
            ));
        }
        Ok(Self {
            facts,
            projections,
            mro,
            complete_names,
            resolvable_name_projections,
            sites,
            associated,
        })
    }
}

/// `parse` reduced to what the resolver reads once projections are
/// recorded: language, Go package, types and methods.
fn declarations(parse: &ParseBlob) -> ParseBlob {
    let mut facts = declarations_of(
        parse.language.clone(),
        parse.dispatch.go_package.clone(),
        parse.dispatch.types.clone(),
        parse.dispatch.methods.clone(),
    );
    facts.dispatch.static_facts = parse.dispatch.static_facts.clone();
    facts
}

test_support_visibility! {
/// The facts a [`FileRecord`] keeps, from their parts: every other field
/// of the parse is left empty.
fn declarations_of(
    language: String,
    go_package: Option<String>,
    types: Vec<TypeHint>,
    methods: Vec<MethodHint>,
) -> ParseBlob {
    ParseBlob {
        entry_point_symbols: Vec::new(),
        dispatch: DispatchFacts {
            go_package,
            types,
            methods,
            ..DispatchFacts::default()
        },
        extractor_version: String::new(),
        language,
        ast_nodes: Vec::new(),
        symbols: Vec::new(),
        default_export_symbol: None,
        exported_symbols: Vec::new(),
        callable_symbols: Vec::new(),
        imports: Vec::new(),
        python_bindings: Vec::new(),
        refs: Vec::new(),
    }
}
}

/// Every name a Rust file's [`Resolver::project_type`] can answer other than
/// with the fixed miss: the types it declares, its receiver type targets,
/// and the names its imports bind (read as `File::imports_naming` reads
/// them).
fn rust_resolvable_names(
    parse: &ParseBlob,
    type_target_names: &mut dyn Iterator<Item = &str>,
) -> BTreeSet<String> {
    let mut names = parse
        .dispatch
        .types
        .iter()
        .map(|ty| ty.name.clone())
        .collect::<BTreeSet<_>>();
    names.extend(type_target_names.map(str::to_string));
    for import in &parse.imports {
        names.extend(
            import
                .names
                .iter()
                .filter_map(|name| name.split_whitespace().last())
                .map(str::to_string),
        );
        names.extend(import.default_import.clone());
    }
    names
}

impl<'a> Resolver<'a> {
    /// The source's recorded projections, when it serves them.
    pub(super) fn projections(&self) -> Option<&'a (dyn Projections + 'a)> {
        self.source.and_then(|source| source.projections())
    }

    /// [`Resolver::project_type`] answered from `projections`, recording the
    /// keys the recorded lookup recorded.
    pub(super) fn replay_projection(
        &self,
        projections: &'a (dyn Projections + 'a),
        file: &str,
        name: &str,
    ) -> Option<(String, &'a TypeHint)> {
        match projections.projected_type(file, name) {
            Recorded::Found(projected) => {
                for key in projected.dependencies.iter() {
                    self.depend(key.clone());
                }
                let (type_file, index) = projected.found.as_ref()?;
                let ty = self
                    .lookup(type_file)
                    .and_then(|parse| parse.dispatch.types.get(usize::try_from(*index).ok()?));
                if ty.is_none() {
                    projections.missing(format!(
                        "type {index} of {type_file}, which {name} names in {file}"
                    ));
                }
                Some((type_file.clone(), ty?))
            }
            // A Rust file's lookup of a name it neither declares, targets
            // nor imports reads its facts, the name's target and the
            // name's local declaration and imports, and finds nothing.
            Recorded::NotProjectable => {
                self.depend(dependency::file(file));
                self.depend(dependency::type_name(file, name));
                None
            }
            Recorded::Missing => {
                projections.missing(format!("the projection of {name} in {file}"));
                None
            }
        }
    }

    pub(super) fn replay_static_import(
        &self,
        projections: &'a (dyn Projections + 'a),
        file: &str,
        key: &str,
    ) -> Option<(String, u32)> {
        if let Recorded::Found(projected) = projections.projected_type(file, key) {
            for dependency in &projected.dependencies {
                self.depend(dependency.clone());
            }
            projected.found.clone()
        } else {
            projections.missing(format!("the static import projection of {key} in {file}"));
            None
        }
    }

    /// `owner`'s resolution order as `file`'s Python `super()` call reads
    /// it, from `projections`, recording the keys computing it recorded.
    pub(super) fn replay_mro(
        &self,
        projections: &'a (dyn Projections + 'a),
        file: &str,
        owner: &str,
    ) -> Option<Vec<(String, String)>> {
        match projections.projected_mro(file, owner) {
            Recorded::Found(projected) => {
                for key in projected.dependencies.iter() {
                    self.depend(key.clone());
                }
                projected.mro.clone()
            }
            Recorded::NotProjectable | Recorded::Missing => {
                projections.missing(format!("the resolution order of {owner} in {file}"));
                None
            }
        }
    }

    /// [`Resolver::project_type`] for `name` in `file` with the keys it
    /// recorded, leaving the keys recorded so far as they were.
    fn record_projection(&self, file: &str, name: &str) -> ProjectedType {
        let outer = self.take_dependencies();
        let found = if name.starts_with(super::STATIC_IMPORT_PREFIX) {
            self.static_import_projection(file, name)
        } else {
            self.project_type(file, name).and_then(|(type_file, ty)| {
                let index = self
                    .lookup(&type_file)?
                    .dispatch
                    .types
                    .iter()
                    .position(|candidate| std::ptr::eq(candidate, ty))?;
                Some((type_file, u32::try_from(index).ok()?))
            })
        };
        let dependencies = self.take_dependencies();
        *self.dependencies.borrow_mut() = outer;
        ProjectedType {
            found,
            dependencies: dependencies.into_iter().collect(),
        }
    }

    /// The resolution order `python_super` reads for `owner` in `file`,
    /// with the keys computing it recorded. `None` when `owner` names no
    /// project type, so `python_super` reads no order.
    fn record_mro(&self, file: &str, owner: &str) -> Option<ProjectedMro> {
        let outer = self.take_dependencies();
        let projected = self.project_type(file, owner);
        // The owner's projection is recorded on its own.
        self.take_dependencies();
        let mro = projected.map(|(owner_file, ty)| {
            let mro = self.python_mro(&owner_file, ty, &mut BTreeSet::new(), &mut BTreeMap::new());
            ProjectedMro {
                mro,
                dependencies: self.take_dependencies().into_iter().collect(),
            }
        });
        *self.dependencies.borrow_mut() = outer;
        mro
    }

    /// The pass-4 record of `path`, a file this resolver reads from files:
    /// its declarations, every projection pass 4 asks of it, and `sites`
    /// (its `bound_dispatch_sites()`), each with whether its ordinal is an
    /// associated-function call and whether it keeps an ordinary edge.
    /// `None` when `path` has no facts.
    pub fn project_file(
        &self,
        path: &str,
        sites: Vec<SiteHint>,
        type_target_names: &mut dyn Iterator<Item = &str>,
        associated: &dyn Fn(u32) -> bool,
        keeps_ordinary_edge: &dyn Fn(u32) -> bool,
    ) -> Option<FileRecord> {
        let parse = self.lookup(path)?;
        let mut projections = BTreeMap::new();
        let inserted = std::cell::Cell::new(0u32);
        let mut project = |name: &str| -> Option<(String, u32)> {
            if let Some(found) = projections.get(name) {
                let found: &ProjectedType = found;
                return found.found.clone();
            }
            let projected = self.record_projection(path, name);
            let found = projected.found.clone();
            projections.insert(name.to_string(), projected);
            inserted.set(inserted.get() + 1);
            found
        };
        for ty in &parse.dispatch.types {
            for base in &ty.bases {
                project(base);
            }
        }
        for method in &parse.dispatch.methods {
            if let Some((owner_file, index)) = project(&method.owner) {
                let owner = self
                    .lookup(&owner_file)
                    .and_then(|owner| owner.dispatch.types.get(index as usize));
                for base in owner.into_iter().flat_map(|owner| &owner.bases) {
                    project(base);
                }
            }
            if let Some(trait_name) = &method.trait_name {
                project(trait_name);
            }
        }
        let language = parse.language.as_str();
        if super::typescript::supported(language) {
            if let Some(static_facts) = &parse.dispatch.static_facts {
                for name in static_facts.projection_names() {
                    project(name);
                }
                for (name, _) in static_facts.import_receivers() {
                    project(&format!("{}{name}", super::STATIC_IMPORT_PREFIX));
                }
            }
        }
        let mut mro = BTreeMap::new();
        let mut typed_rust = false;
        let mut records = Vec::with_capacity(sites.len());
        let mut associated_ordinals = Vec::new();
        for site in sites {
            let record = SiteRecord {
                associated: associated(site.ordinal),
                keeps_ordinary_edge: keeps_ordinary_edge(site.ordinal),
                site,
            };
            if record.associated {
                associated_ordinals.push(record.site.ordinal);
            }
            let site = &record.site;
            // The projections `Resolver::resolve` makes from the calling
            // file, on the paths that reach one.
            let typed = !site.dynamic
                && site.member.is_some()
                && !(language == "go" && parse.dispatch.go_package.is_none())
                && language != "cpp";
            match site.receiver.as_deref() {
                Some(receiver)
                    if typed
                        && !(language == "rust"
                            && receiver.starts_with(super::TEMPLATE_SELF_RECEIVER)) =>
                {
                    match receiver.strip_prefix("@super:") {
                        Some(owner) if language == "python" => {
                            project(owner);
                            if !mro.contains_key(owner) {
                                if let Some(order) = self.record_mro(path, owner) {
                                    mro.insert(owner.to_string(), order);
                                }
                            }
                        }
                        _ => {
                            let projection = super::receiver_projection(
                                language,
                                receiver,
                                site.member.as_deref().expect("typed site has a member"),
                            );
                            project(&projection);
                            typed_rust |= language == "rust";
                        }
                    }
                }
                _ => {}
            }
            records.push(record);
        }
        let mut complete_names = false;
        let before = inserted.get();
        if typed_rust {
            for name in rust_resolvable_names(parse, type_target_names) {
                project(&name);
            }
            complete_names = true;
        }
        let resolvable_name_projections = inserted.get() - before;
        associated_ordinals.sort_unstable();
        associated_ordinals.dedup();
        Some(FileRecord {
            facts: declarations(parse),
            projections,
            mro,
            complete_names,
            resolvable_name_projections,
            sites: records,
            associated: associated_ordinals,
        })
    }
}

aft_store::heap_bytes_of_fields!(ProjectedType {
    found,
    dependencies
});
aft_store::heap_bytes_of_fields!(ProjectedMro { mro, dependencies });
aft_store::heap_bytes_of_fields!(SiteRecord {
    site,
    associated,
    keeps_ordinary_edge,
});
aft_store::heap_bytes_of_fields!(FileRecord {
    facts,
    projections,
    mro,
    complete_names,
    resolvable_name_projections,
    sites,
    associated,
});

#[cfg(any(test, feature = "test-support"))]
impl FileRecord {
    /// A record whose parts all depend on `index`, declaring methods named
    /// `m{index % 3}` and `shared`, for tests of where records are kept.
    pub fn sample(index: u32) -> Self {
        let method = |name: String| MethodHint {
            owner: format!("Type{index}"),
            trait_name: None,
            name,
            symbol: format!("Type{index}::method"),
            has_body: index % 2 == 0,
            shape: "x".repeat(index as usize % 40),
            private: false,
        };
        FileRecord {
            facts: declarations_of(
                "typescript".to_string(),
                None,
                vec![TypeHint {
                    name: format!("Type{index}"),
                    bases: vec![format!("Base{}", index / 2)],
                    interface: false,
                    closed: false,
                }],
                vec![
                    method(format!("m{}", index % 3)),
                    method("shared".to_string()),
                ],
            ),
            projections: BTreeMap::from([(
                format!("Base{}", index / 2),
                ProjectedType {
                    found: (index % 2 == 0).then(|| (format!("src/{index}.ts"), index)),
                    dependencies: vec![format!("t:src/{index}.ts\tBase")].into_boxed_slice(),
                },
            )]),
            mro: BTreeMap::new(),
            complete_names: false,
            resolvable_name_projections: 0,
            sites: (0..index % 5)
                .map(|ordinal| SiteRecord {
                    site: SiteHint {
                        ordinal,
                        caller: Some(format!("caller{index}")),
                        line: ordinal + index,
                        member: Some("shared".to_string()),
                        receiver: None,
                        dynamic: false,
                    },
                    associated: false,
                    keeps_ordinary_edge: false,
                })
                .collect(),
            associated: Vec::new(),
        }
    }
}