diff --git a/src/builder/build_traits.rs b/src/builder/build_traits.rs index a4deabbe2..734995a49 100644 --- a/src/builder/build_traits.rs +++ b/src/builder/build_traits.rs @@ -438,9 +438,8 @@ pub trait Dataflow: Container { let hugr = self.hugr(); let def_op = hugr.get_optype(function.node()); let signature = match def_op { - OpType::Def(ops::Def { signature }) | OpType::Declare(ops::Declare { signature }) => { - signature.clone() - } + OpType::Def(ops::Def { signature, .. }) + | OpType::Declare(ops::Declare { signature, .. }) => signature.clone(), _ => { return Err(BuildError::UnexpectedType { node: function.node(), diff --git a/src/builder/dataflow.rs b/src/builder/dataflow.rs index 729e66728..9623a19c8 100644 --- a/src/builder/dataflow.rs +++ b/src/builder/dataflow.rs @@ -128,10 +128,13 @@ impl FunctionBuilder { /// # Errors /// /// Error in adding DFG child nodes. - pub fn new(_name: impl Into, signature: Signature) -> Result { + pub fn new(name: impl Into, signature: Signature) -> Result { let inputs = signature.input.clone(); let outputs = signature.output.clone(); - let op = ops::Def { signature }; + let op = ops::Def { + signature, + name: name.into(), + }; let base = HugrMut::new(op); let root = base.hugr().root(); diff --git a/src/builder/module_builder.rs b/src/builder/module_builder.rs index 7a426e432..abf5c8a0b 100644 --- a/src/builder/module_builder.rs +++ b/src/builder/module_builder.rs @@ -73,18 +73,24 @@ impl ModuleBuilder { f_id: &FuncID, ) -> Result, BuildError> { let f_node = f_id.node(); - let (inputs, outputs) = - if let OpType::Declare(ops::Declare { signature }) = self.hugr().get_optype(f_node) { - (signature.input.clone(), signature.output.clone()) - } else { - return Err(BuildError::UnexpectedType { - node: f_node, - op_desc: "OpType::Declare", - }); - }; + let (inputs, outputs, name) = if let OpType::Declare(ops::Declare { signature, name }) = + self.hugr().get_optype(f_node) + { + ( + signature.input.clone(), + signature.output.clone(), + name.clone(), + ) + } else { + return Err(BuildError::UnexpectedType { + node: f_node, + op_desc: "OpType::Declare", + }); + }; self.base().replace_op( f_node, ops::Def { + name, signature: Signature::new_df(inputs.clone(), outputs.clone()), }, ); @@ -117,11 +123,14 @@ impl ModuleBuilder { /// [`OpType::Declare`] node. pub fn declare( &mut self, - _name: impl Into, + name: impl Into, signature: Signature, ) -> Result, BuildError> { - // TODO add name and param names to metadata - let declare_n = self.add_child_op(ops::Declare { signature })?; + // TODO add param names to metadata + let declare_n = self.add_child_op(ops::Declare { + signature, + name: name.into(), + })?; Ok(declare_n.into()) } diff --git a/src/hugr/hugrmut.rs b/src/hugr/hugrmut.rs index df1ef52c2..1c3e376ce 100644 --- a/src/hugr/hugrmut.rs +++ b/src/hugr/hugrmut.rs @@ -285,6 +285,7 @@ mod test { .add_op_with_parent( module, ops::Def { + name: "main".into(), signature: Signature::new_df(type_row![NAT], type_row![NAT, NAT]), }, ) diff --git a/src/hugr/validate.rs b/src/hugr/validate.rs index ca51479d1..45420534b 100644 --- a/src/hugr/validate.rs +++ b/src/hugr/validate.rs @@ -775,6 +775,7 @@ mod test { /// Returns the hugr and the node index of the definition. fn make_simple_hugr(copies: usize) -> (HugrMut, Node) { let def_op: OpType = ops::Def { + name: "main".into(), signature: Signature::new_df(type_row![B], vec![B; copies]), } .into(); @@ -871,6 +872,7 @@ mod test { #[test] fn invalid_root() { let declare_op: OpType = ops::Declare { + name: "main".into(), signature: Default::default(), } .into(); @@ -943,7 +945,13 @@ mod test { // Add a definition without children let def_sig = Signature::new_df(type_row![B], type_row![B, B]); let new_def = b - .add_op_with_parent(root, ops::Def { signature: def_sig }) + .add_op_with_parent( + root, + ops::Def { + signature: def_sig, + name: "main".into(), + }, + ) .unwrap(); assert_matches!( b.hugr().validate(), diff --git a/src/ops/module.rs b/src/ops/module.rs index 0776f3b2c..676b8befa 100644 --- a/src/ops/module.rs +++ b/src/ops/module.rs @@ -27,6 +27,8 @@ impl OpTrait for Module { /// Children nodes are the body of the definition. #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Def { + /// Name of function + pub name: String, /// Signature of the function pub signature: Signature, } @@ -51,6 +53,8 @@ impl OpTrait for Def { /// External function declaration, linked at runtime. #[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Declare { + /// Name of function + pub name: String, /// Signature of the function pub signature: Signature, }