1use 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#[derive(Debug, Clone, Copy)]
13pub struct ProgramView<'a> {
14 pub tenv: &'a TyEnv,
16 pub pd: PdId,
18 body: &'a Body,
19}
20
21impl<'a> ProgramView<'a> {
22 #[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 #[must_use]
34 pub fn body(&self) -> &'a Body {
35 self.body
36 }
37
38 #[must_use]
40 pub fn locals(&self) -> &'a LocalVars {
41 &self.tenv.locals[self.pd]
42 }
43
44 #[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 #[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 #[must_use]
62 pub fn chan(&self, chan: ChanId) -> &'a Channel {
63 &self.tenv.chans[chan]
64 }
65
66 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 #[must_use]
78 pub fn irq_id(&self) -> Option<u32> {
79 self.tenv.irq_ids[self.pd]
80 }
81
82 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 #[must_use]
95 pub fn struct_of(&self, id: StructId) -> &'a Struct {
96 &self.tenv.structs[id]
97 }
98
99 #[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}