Skip to main content

fic/tast/
view.rs

1//! What one program's ids name: the accessors a backend reads the typed
2//! environment and the typed bodies through, beside the Ccfg's graphs.
3
4use crate::base::{
5    BodyVarId, ChanId, ChannelEnd, Endpoint, IdVec, Mapping, MappingId, PdId, ProcId, StructId,
6    VarRef,
7};
8
9use super::{Body, BodyVar, Channel, Field, LocalVars, Struct, Tast, Ty, TyEnv};
10
11/// One program: its declarations in the typed environment, and its bodies.
12#[derive(Debug, Clone, Copy)]
13pub struct ProgramView<'a> {
14    /// The typed environment.
15    pub tenv: &'a TyEnv,
16    /// The program's protection domain, its id in the project.
17    pub pd: PdId,
18    body: &'a Body,
19}
20
21impl<'a> ProgramView<'a> {
22    /// Program `pd`: its data in `tast.tenv`, its code in `tast.bodies`.
23    #[must_use]
24    pub fn new(tast: &'a Tast, pd: PdId) -> Self {
25        Self {
26            tenv: &tast.tenv,
27            pd,
28            body: &tast.bodies[pd],
29        }
30    }
31
32    /// The program's body.
33    #[must_use]
34    pub fn body(&self) -> &'a Body {
35        self.body
36    }
37
38    /// The program's locals, typed.
39    #[must_use]
40    pub fn locals(&self) -> &'a LocalVars {
41        &self.tenv.locals[self.pd]
42    }
43
44    /// The name `var` denotes in the body of `process`.
45    #[must_use]
46    pub fn var(&self, process: ProcId, var: VarRef) -> &'a str {
47        self.var_in(&self.body.processes[process].body_vars, var)
48    }
49
50    /// The name `var` denotes in a body whose variables are `body_vars`.
51    #[must_use]
52    pub fn var_in(&self, body_vars: &'a IdVec<BodyVarId, BodyVar>, var: VarRef) -> &'a str {
53        match var {
54            VarRef::Body(id) => &body_vars[id].name,
55            VarRef::Local(id) => &self.locals()[id].name,
56            VarRef::Const(id) => &self.tenv.consts[id].name,
57        }
58    }
59
60    /// A channel.
61    #[must_use]
62    pub fn chan(&self, chan: ChanId) -> &'a Channel {
63        &self.tenv.chans[chan]
64    }
65
66    /// The program's end of each channel it is an endpoint of, in channel
67    /// order.
68    pub fn ends(&self) -> impl Iterator<Item = (ChanId, Endpoint, ChannelEnd)> + 'a {
69        let pd = self.pd;
70        self.tenv
71            .chans
72            .iter_enumerated()
73            .filter_map(move |(id, ch)| ch.end(pd).map(|(endpoint, end)| (id, endpoint, end)))
74    }
75
76    /// Its IRQ's Microkit channel id, if it has an IRQ.
77    #[must_use]
78    pub fn irq_id(&self) -> Option<u32> {
79        self.tenv.irq_ids[self.pd]
80    }
81
82    /// Every mapping in this program's protection domain, with its id, in
83    /// declaration order: the buffer at each of its channel ends, in
84    /// channel order, then its own.
85    pub fn mappings(&self) -> impl Iterator<Item = (MappingId, &'a Mapping)> {
86        let pd = self.pd;
87        self.tenv
88            .mappings
89            .iter_enumerated()
90            .filter(move |(_, m)| m.pd == pd)
91    }
92
93    /// A struct.
94    #[must_use]
95    pub fn struct_of(&self, id: StructId) -> &'a Struct {
96        &self.tenv.structs[id]
97    }
98
99    /// Field `index` of a value of struct type `base`.
100    ///
101    /// # Panics
102    ///
103    /// If `base` is not a struct type: `tast` types `e.f` only on one.
104    #[must_use]
105    pub fn field(&self, base: &Ty, index: u32) -> &'a Field {
106        let Ty::Struct(s) = base else {
107            panic!("a field of a non-struct {base:?}");
108        };
109        &self.struct_of(*s).fields[index as usize]
110    }
111}