1
use super::SymbolTable;
2
use crate::ast::Expr;
3

            
4
impl SymbolTable {
5
    /// Records the parameter `WasmType`s for a closure signature id (see
6
    /// [`Self::closure_params`]).
7
178862
    pub fn record_closure_params(
8
178862
        &mut self,
9
178862
        sig: crate::ast::ClosureSigId,
10
178862
        params: &[crate::ast::WasmType],
11
178862
    ) {
12
178862
        self.closure_params.insert(sig.0, params.to_vec());
13
178862
    }
14

            
15
    /// The recorded parameter `WasmType`s for a closure signature id, or `None`
16
    /// when the closure wasn't recorded.
17
52919
    pub fn closure_params(&self, sig: crate::ast::ClosureSigId) -> Option<&[crate::ast::WasmType]> {
18
52919
        self.closure_params.get(&sig.0).map(Vec::as_slice)
19
52919
    }
20

            
21
    /// The recorded first parameter `WasmType` for a closure signature id, or
22
    /// `None` when the closure takes no parameters or wasn't recorded.
23
2
    pub fn closure_param(&self, sig: crate::ast::ClosureSigId) -> Option<crate::ast::WasmType> {
24
2
        self.closure_params(sig)?.first().copied()
25
2
    }
26

            
27
    /// Records a value-position lambda's source for the closure value in `place`.
28
119673
    pub fn record_closure_body(
29
119673
        &mut self,
30
119673
        place: crate::ast::Place,
31
119673
        params: crate::ast::LambdaParams,
32
119673
        body: Expr,
33
119673
    ) {
34
119673
        self.closure_bodies.insert(place, (params, body));
35
119673
    }
36

            
37
    /// The source of the closure value held in `place`, if one was recorded.
38
    #[must_use]
39
656671
    pub fn closure_body(
40
656671
        &self,
41
656671
        place: crate::ast::Place,
42
656671
    ) -> Option<&(crate::ast::LambdaParams, Expr)> {
43
656671
        self.closure_bodies.get(&place)
44
656671
    }
45

            
46
    /// Drops every source recorded under a local index: a function body or a
47
    /// reset local pool reuses the indices.
48
2453298
    pub(crate) fn forget_local_closure_bodies(&mut self) {
49
2453298
        self.closure_bodies
50
100544
            .retain(|place, _| !matches!(place, crate::ast::Place::Local(_)));
51
2453298
    }
52

            
53
    /// Starts a compilation unit with no closure records. Local and global
54
    /// places and closure signature ids all restart with each unit's context,
55
    /// so a record outliving its unit (a failed one included) would be read
56
    /// under a reused key.
57
2352272
    pub(crate) fn forget_unit_closures(&mut self) {
58
2352272
        self.closure_bodies.clear();
59
2352272
        self.closure_results.clear();
60
2352272
        self.closure_params.clear();
61
2352272
    }
62

            
63
    /// Drops the recorded source of `place`: a `setf` overwrote the closure, so
64
    /// the source no longer describes the value now there.
65
4699
    pub fn forget_closure_body(&mut self, place: crate::ast::Place) {
66
4699
        self.closure_bodies.remove(&place);
67
4699
    }
68
}