use alloc::vec::Vec;
use bytecode_lang::{FuncId, GlobalId, Module};
use hir_lang::{ItemId, NodeRef};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Compiled {
pub(crate) module: Module,
pub(crate) entry: FuncId,
pub(crate) start: Option<FuncId>,
pub(crate) result: Option<GlobalId>,
pub(crate) functions: Vec<(ItemId, FuncId)>,
pub(crate) origins: Vec<Vec<NodeRef>>,
}
impl Compiled {
#[must_use]
pub fn module(&self) -> &Module {
&self.module
}
#[must_use]
pub fn into_module(self) -> Module {
self.module
}
#[must_use]
pub fn entry(&self) -> FuncId {
self.entry
}
#[must_use]
pub fn start(&self) -> Option<FuncId> {
self.start
}
#[must_use]
pub fn result_global(&self) -> Option<GlobalId> {
self.result
}
#[must_use]
pub fn function(&self, item: ItemId) -> Option<FuncId> {
self.functions
.iter()
.find(|(i, _)| *i == item)
.map(|&(_, f)| f)
}
#[must_use]
pub fn origin(&self, func: FuncId, pc: u32) -> Option<NodeRef> {
self.origins
.get(func.index())
.and_then(|o| o.get(pc as usize))
.copied()
}
#[must_use]
pub fn origins(&self, func: FuncId) -> &[NodeRef] {
self.origins.get(func.index()).map_or(&[], Vec::as_slice)
}
}