hugr_llvm/emit/
args.rs

1use hugr_core::{ops::OpType, HugrView, Node};
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    pub fn node(&self) -> FatNode<'hugr, OT, H> {
22        self.node
23    }
24}
25
26impl<'c, 'hugr, H: HugrView<Node = Node>> EmitOpArgs<'c, 'hugr, OpType, H> {
27    /// Attempt to specialise the internal [FatNode].
28    pub fn try_into_ot<OT>(self) -> Result<EmitOpArgs<'c, 'hugr, OT, H>, Self>
29    where
30        for<'a> &'a OpType: TryInto<&'a OT>,
31    {
32        let EmitOpArgs {
33            node,
34            inputs,
35            outputs,
36        } = self;
37        match node.try_into_ot() {
38            Some(new_node) => Ok(EmitOpArgs {
39                node: new_node,
40                inputs,
41                outputs,
42            }),
43            None => Err(EmitOpArgs {
44                node,
45                inputs,
46                outputs,
47            }),
48        }
49    }
50
51    /// Specialise the internal [FatNode].
52    ///
53    /// Panics if `OT` is not the [HugrView::get_optype] of the internal
54    /// [hugr_core::Node].
55    pub fn into_ot<OTInto: PartialEq + 'c>(self, ot: &OTInto) -> EmitOpArgs<'c, 'hugr, OTInto, H>
56    where
57        for<'a> &'a OpType: TryInto<&'a OTInto>,
58    {
59        let EmitOpArgs {
60            node,
61            inputs,
62            outputs,
63        } = self;
64        EmitOpArgs {
65            node: node.into_ot(ot),
66            inputs,
67            outputs,
68        }
69    }
70}