Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions hugr-core/src/builder/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,31 @@ impl FunctionBuilder<Hugr> {
}
}

impl<B: AsMut<Hugr> + AsRef<Hugr>> FunctionBuilder<B> {
/// Initialize a new function definition on the root module of an existing HUGR.
///
/// The HUGR's entrypoint will **not** be modified.
///
/// # Errors
///
/// Error in adding DFG child nodes.
pub fn with_hugr(
mut hugr: B,
name: impl Into<String>,
signature: impl Into<PolyFuncType>,
) -> Result<Self, BuildError> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this useful without returning the Node of the inserted FuncDefn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The builder itself carries the node, all the DFGBuilder methods run from there. The node itself can be retrieved with container_node if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see 👍

let signature: PolyFuncType = signature.into();
let body = signature.body().clone();
let op = ops::FuncDefn::new(name, signature);

let module = hugr.as_ref().module_root();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove the AsRef<Hugr> requirement if you used as_mut here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we could drop it but we use the AsMut<Hugr> + AsRef<Hugr> bound through most of the builder traits, so I think it's better to keep it consistent here.

let func = hugr.as_mut().add_node_with_parent(module, op);

let db = DFGBuilder::create_with_io(hugr, func, body)?;
Ok(Self::from_dfg_builder(db))
}
}

impl<B: AsMut<Hugr> + AsRef<Hugr>, T> Container for DFGWrapper<B, T> {
#[inline]
fn container_node(&self) -> Node {
Expand Down
21 changes: 21 additions & 0 deletions hugr-core/src/builder/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ impl HugrBuilder for ModuleBuilder<Hugr> {
}

impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
/// Continue building a module from an existing hugr.
#[must_use]
pub fn with_hugr(hugr: T) -> Self {
ModuleBuilder(hugr)
}

/// Replace a [`ops::FuncDecl`] with [`ops::FuncDefn`] and return a builder for
/// the defining graph.
///
Expand Down Expand Up @@ -229,4 +235,19 @@ mod test {
assert_matches!(build_result, Ok(_));
Ok(())
}

#[test]
fn builder_from_existing() -> Result<(), BuildError> {
let hugr = Hugr::new();

let fn_builder = FunctionBuilder::with_hugr(hugr, "main", Signature::new_endo(vec![]))?;
let mut hugr = fn_builder.finish_hugr()?;

let mut module_builder = ModuleBuilder::with_hugr(&mut hugr);
module_builder.declare("other", Signature::new_endo(vec![]).into())?;

hugr.validate()?;

Ok(())
}
}
Loading