Skip to content

Commit

Permalink
Store function name in Def and Declare nodes (#144)
Browse files Browse the repository at this point in the history
Closes #136
  • Loading branch information
ss2165 authored Jun 9, 2023
1 parent 504229e commit 0fefde4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
7 changes: 5 additions & 2 deletions src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,13 @@ impl FunctionBuilder<HugrMut> {
/// # Errors
///
/// Error in adding DFG child nodes.
pub fn new(_name: impl Into<String>, signature: Signature) -> Result<Self, BuildError> {
pub fn new(name: impl Into<String>, signature: Signature) -> Result<Self, BuildError> {
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();
Expand Down
33 changes: 21 additions & 12 deletions src/builder/module_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,24 @@ impl<T: HugrMutRef> ModuleBuilder<T> {
f_id: &FuncID<false>,
) -> Result<FunctionBuilder<&mut HugrMut>, 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()),
},
);
Expand Down Expand Up @@ -117,11 +123,14 @@ impl<T: HugrMutRef> ModuleBuilder<T> {
/// [`OpType::Declare`] node.
pub fn declare(
&mut self,
_name: impl Into<String>,
name: impl Into<String>,
signature: Signature,
) -> Result<FuncID<false>, 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())
}
Expand Down
1 change: 1 addition & 0 deletions src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
},
)
Expand Down
10 changes: 9 additions & 1 deletion src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -871,6 +872,7 @@ mod test {
#[test]
fn invalid_root() {
let declare_op: OpType = ops::Declare {
name: "main".into(),
signature: Default::default(),
}
.into();
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions src/ops/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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,
}
Expand Down

0 comments on commit 0fefde4

Please sign in to comment.