hugr_llvm/emit/
args.rs

1use hugr_core::{HugrView, Node, ops::OpType};
2use inkwell::values::BasicValueEnum;
3
4use crate::utils::fat::FatNode;
5
6use super::func::RowPromise;
7
8/// A type used whenever emission is delegated to a function, for example in
9/// [`crate::custom::extension_op::ExtensionOpMap::emit_extension_op`].
10pub struct EmitOpArgs<'c, 'hugr, OT, H> {
11    /// The [`HugrView`] and [`hugr_core::Node`] we are emitting
12    pub node: FatNode<'hugr, OT, H>,
13    /// The values that should be used for all Value input ports of the node
14    pub inputs: Vec<BasicValueEnum<'c>>,
15    /// The results of the node should be put here
16    pub outputs: RowPromise<'c>,
17}
18
19impl<'hugr, OT, H> EmitOpArgs<'_, 'hugr, OT, H> {
20    /// Get the internal [`FatNode`]
21    #[must_use]
22    pub fn node(&self) -> FatNode<'hugr, OT, H> {
23        self.node
24    }
25}
26
27impl<'c, 'hugr, H: HugrView<Node = Node>> EmitOpArgs<'c, 'hugr, OpType, H> {
28    /// Attempt to specialise the internal [`FatNode`].
29    pub fn try_into_ot<OT>(self) -> Result<EmitOpArgs<'c, 'hugr, OT, H>, Self>
30    where
31        for<'a> &'a OpType: TryInto<&'a OT>,
32    {
33        let EmitOpArgs {
34            node,
35            inputs,
36            outputs,
37        } = self;
38        match node.try_into_ot() {
39            Some(new_node) => Ok(EmitOpArgs {
40                node: new_node,
41                inputs,
42                outputs,
43            }),
44            None => Err(EmitOpArgs {
45                node,
46                inputs,
47                outputs,
48            }),
49        }
50    }
51
52    /// Specialise the internal [`FatNode`].
53    ///
54    /// Panics if `OT` is not the [`HugrView::get_optype`] of the internal
55    /// [`hugr_core::Node`].
56    pub fn into_ot<OTInto: PartialEq + 'c>(self, ot: &OTInto) -> EmitOpArgs<'c, 'hugr, OTInto, H>
57    where
58        for<'a> &'a OpType: TryInto<&'a OTInto>,
59    {
60        let EmitOpArgs {
61            node,
62            inputs,
63            outputs,
64        } = self;
65        EmitOpArgs {
66            node: node.into_ot(ot),
67            inputs,
68            outputs,
69        }
70    }
71}