Skip to content

Commit 0280eb2

Browse files
authored
feat!: Hide FuncDefn/cl fields, add accessors and ::new(...) (#2213)
Accessors ::func_name, ::func_name_mut, ::signature, ::signature_mut. Constructor ::new takes name + sig using `impl Into`. BREAKING CHANGE: construction, destruction, and field access should use new methods
1 parent 19e3216 commit 0280eb2

File tree

21 files changed

+175
-151
lines changed

21 files changed

+175
-151
lines changed

hugr-core/src/builder/build_traits.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ pub trait Container {
9696
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
9797
let signature: PolyFuncType = signature.into();
9898
let body = signature.body().clone();
99-
let f_node = self.add_child_node(ops::FuncDefn {
100-
name: name.into(),
101-
signature,
102-
});
99+
let f_node = self.add_child_node(ops::FuncDefn::new(name, signature));
103100

104101
// Add the extensions used by the function types.
105102
self.use_extensions(
@@ -401,8 +398,8 @@ pub trait Dataflow: Container {
401398
let func_node = fid.node();
402399
let func_op = self.hugr().get_optype(func_node);
403400
let func_sig = match func_op {
404-
OpType::FuncDefn(ops::FuncDefn { signature, .. })
405-
| OpType::FuncDecl(ops::FuncDecl { signature, .. }) => signature.clone(),
401+
OpType::FuncDefn(fd) => fd.signature().clone(),
402+
OpType::FuncDecl(fd) => fd.signature().clone(),
406403
_ => {
407404
return Err(BuildError::UnexpectedType {
408405
node: func_node,
@@ -617,8 +614,8 @@ pub trait Dataflow: Container {
617614
let hugr = self.hugr();
618615
let def_op = hugr.get_optype(function.node());
619616
let type_scheme = match def_op {
620-
OpType::FuncDefn(ops::FuncDefn { signature, .. })
621-
| OpType::FuncDecl(ops::FuncDecl { signature, .. }) => signature.clone(),
617+
OpType::FuncDefn(fd) => fd.signature().clone(),
618+
OpType::FuncDecl(fd) => fd.signature().clone(),
622619
_ => {
623620
return Err(BuildError::UnexpectedType {
624621
node: function.node(),

hugr-core/src/builder/dataflow.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,7 @@ impl FunctionBuilder<Hugr> {
162162
) -> Result<Self, BuildError> {
163163
let signature: PolyFuncType = signature.into();
164164
let body = signature.body().clone();
165-
let op = ops::FuncDefn {
166-
signature,
167-
name: name.into(),
168-
};
165+
let op = ops::FuncDefn::new(name, signature);
169166

170167
let base = Hugr::new_with_entrypoint(op).expect("FuncDefn entrypoint should be valid");
171168
let root = base.entrypoint();
@@ -187,7 +184,7 @@ impl FunctionBuilder<Hugr> {
187184
});
188185

189186
// Update the inner input node
190-
let types = new_optype.signature.body().input.clone();
187+
let types = new_optype.signature().body().input.clone();
191188
self.hugr_mut().replace_op(inp_node, Input { types });
192189
let mut new_port = self.hugr_mut().add_ports(inp_node, Direction::Outgoing, 1);
193190
let new_port = new_port.next().unwrap();
@@ -222,7 +219,7 @@ impl FunctionBuilder<Hugr> {
222219
});
223220

224221
// Update the inner input node
225-
let types = new_optype.signature.body().output.clone();
222+
let types = new_optype.signature().body().output.clone();
226223
self.hugr_mut().replace_op(out_node, Output { types });
227224
let mut new_port = self.hugr_mut().add_ports(out_node, Direction::Incoming, 1);
228225
let new_port = new_port.next().unwrap();
@@ -253,22 +250,12 @@ impl FunctionBuilder<Hugr> {
253250
/// Returns a reference to the new optype.
254251
fn update_fn_signature(&mut self, f: impl FnOnce(Signature) -> Signature) -> &ops::FuncDefn {
255252
let parent = self.container_node();
256-
let old_optype = self
257-
.hugr()
258-
.get_optype(parent)
259-
.as_func_defn()
260-
.expect("FunctionBuilder node must be a FuncDefn");
261-
let signature = old_optype.inner_signature().into_owned();
262-
let name = old_optype.name.clone();
263-
self.hugr_mut().replace_op(
264-
parent,
265-
ops::FuncDefn {
266-
signature: f(signature).into(),
267-
name,
268-
},
269-
);
270253

271-
self.hugr().get_optype(parent).as_func_defn().unwrap()
254+
let ops::OpType::FuncDefn(fd) = self.hugr_mut().optype_mut(parent) else {
255+
panic!("FunctionBuilder node must be a FuncDefn")
256+
};
257+
*fd.signature_mut() = f(fd.inner_signature().into_owned()).into();
258+
&*fd
272259
}
273260
}
274261

@@ -531,8 +518,8 @@ pub(crate) mod test {
531518
let mut module_builder = ModuleBuilder::new();
532519

533520
let (dfg_node, f_node) = {
534-
let mut f_build = module_builder
535-
.define_function("main", Signature::new(vec![bool_t()], vec![bool_t()]))?;
521+
let mut f_build =
522+
module_builder.define_function("main", Signature::new_endo(bool_t()))?;
536523

537524
let [i1] = f_build.input_wires_arr();
538525
let dfg = f_build.add_hugr_with_wires(dfg_hugr, [i1])?;

hugr-core/src/builder/module.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,19 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
6969
f_id: &FuncID<false>,
7070
) -> Result<FunctionBuilder<&mut Hugr>, BuildError> {
7171
let f_node = f_id.node();
72-
let ops::FuncDecl { signature, name } = self
73-
.hugr()
74-
.get_optype(f_node)
75-
.as_func_decl()
76-
.ok_or(BuildError::UnexpectedType {
77-
node: f_node,
78-
op_desc: "crate::ops::OpType::FuncDecl",
79-
})?
80-
.clone();
81-
let body = signature.body().clone();
72+
let decl =
73+
self.hugr()
74+
.get_optype(f_node)
75+
.as_func_decl()
76+
.ok_or(BuildError::UnexpectedType {
77+
node: f_node,
78+
op_desc: "crate::ops::OpType::FuncDecl",
79+
})?;
80+
let name = decl.func_name().clone();
81+
let sig = decl.signature().clone();
82+
let body = sig.body().clone();
8283
self.hugr_mut()
83-
.replace_op(f_node, ops::FuncDefn { name, signature });
84+
.replace_op(f_node, ops::FuncDefn::new(name, sig));
8485

8586
let db = DFGBuilder::create_with_io(self.hugr_mut(), f_node, body)?;
8687
Ok(FunctionBuilder::from_dfg_builder(db))
@@ -99,10 +100,7 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
99100
) -> Result<FuncID<false>, BuildError> {
100101
let body = signature.body().clone();
101102
// TODO add param names to metadata
102-
let declare_n = self.add_child_node(ops::FuncDecl {
103-
signature,
104-
name: name.into(),
105-
});
103+
let declare_n = self.add_child_node(ops::FuncDecl::new(name, signature));
106104

107105
// Add the extensions used by the function types.
108106
self.use_extensions(

hugr-core/src/export.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ impl<'a> Context<'a> {
226226
/// one of those operations.
227227
fn get_func_name(&self, func_node: Node) -> Option<&'a str> {
228228
match self.hugr.get_optype(func_node) {
229-
OpType::FuncDecl(func_decl) => Some(&func_decl.name),
230-
OpType::FuncDefn(func_defn) => Some(&func_defn.name),
229+
OpType::FuncDecl(func_decl) => Some(func_decl.func_name()),
230+
OpType::FuncDefn(func_defn) => Some(func_defn.func_name()),
231231
_ => None,
232232
}
233233
}
@@ -260,8 +260,8 @@ impl<'a> Context<'a> {
260260

261261
// We record the name of the symbol defined by the node, if any.
262262
let symbol = match optype {
263-
OpType::FuncDefn(func_defn) => Some(func_defn.name.as_str()),
264-
OpType::FuncDecl(func_decl) => Some(func_decl.name.as_str()),
263+
OpType::FuncDefn(func_defn) => Some(func_defn.func_name().as_str()),
264+
OpType::FuncDecl(func_decl) => Some(func_decl.func_name().as_str()),
265265
OpType::AliasDecl(alias_decl) => Some(alias_decl.name.as_str()),
266266
OpType::AliasDefn(alias_defn) => Some(alias_defn.name.as_str()),
267267
_ => None,
@@ -331,7 +331,7 @@ impl<'a> Context<'a> {
331331

332332
OpType::FuncDefn(func) => self.with_local_scope(node_id, |this| {
333333
let name = this.get_func_name(node).unwrap();
334-
let symbol = this.export_poly_func_type(name, &func.signature);
334+
let symbol = this.export_poly_func_type(name, func.signature());
335335
regions = this.bump.alloc_slice_copy(&[this.export_dfg(
336336
node,
337337
model::ScopeClosure::Closed,
@@ -342,7 +342,7 @@ impl<'a> Context<'a> {
342342

343343
OpType::FuncDecl(func) => self.with_local_scope(node_id, |this| {
344344
let name = this.get_func_name(node).unwrap();
345-
let symbol = this.export_poly_func_type(name, &func.signature);
345+
let symbol = this.export_poly_func_type(name, func.signature());
346346
table::Operation::DeclareFunc(symbol)
347347
}),
348348

hugr-core/src/extension/resolution/types.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ pub(crate) fn collect_op_types_extensions(
4242
}
4343
collect_signature_exts(&ext.signature(), &mut used, &mut missing);
4444
}
45-
OpType::FuncDefn(f) => collect_signature_exts(f.signature.body(), &mut used, &mut missing),
46-
OpType::FuncDecl(f) => collect_signature_exts(f.signature.body(), &mut used, &mut missing),
45+
OpType::FuncDefn(f) => {
46+
collect_signature_exts(f.signature().body(), &mut used, &mut missing)
47+
}
48+
OpType::FuncDecl(f) => {
49+
collect_signature_exts(f.signature().body(), &mut used, &mut missing)
50+
}
4751
OpType::Const(c) => collect_value_exts(&c.value, &mut used, &mut missing),
4852
OpType::Input(inp) => collect_type_row_exts(&inp.types, &mut used, &mut missing),
4953
OpType::Output(out) => collect_type_row_exts(&out.types, &mut used, &mut missing),

hugr-core/src/extension/resolution/types_mut.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,20 @@ pub fn resolve_op_types_extensions(
3535
resolve_signature_exts(node, ext.signature_mut(), extensions, used_extensions)?;
3636
}
3737
OpType::FuncDefn(f) => {
38-
resolve_signature_exts(node, f.signature.body_mut(), extensions, used_extensions)?;
38+
resolve_signature_exts(
39+
node,
40+
f.signature_mut().body_mut(),
41+
extensions,
42+
used_extensions,
43+
)?;
3944
}
4045
OpType::FuncDecl(f) => {
41-
resolve_signature_exts(node, f.signature.body_mut(), extensions, used_extensions)?;
46+
resolve_signature_exts(
47+
node,
48+
f.signature_mut().body_mut(),
49+
extensions,
50+
used_extensions,
51+
)?;
4252
}
4353
OpType::Const(c) => resolve_value_exts(node, &mut c.value, extensions, used_extensions)?,
4454
OpType::Input(inp) => {

hugr-core/src/hugr.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,7 @@ fn make_module_hugr(root_op: OpType, nodes: usize, ports: usize) -> Option<Hugr>
443443
let dataflow_inputs = signature.input_count();
444444
let dataflow_outputs = signature.output_count();
445445

446-
let func = hugr.add_node_with_parent(
447-
module,
448-
ops::FuncDefn {
449-
name: "main".into(),
450-
signature: signature.clone().into(),
451-
},
452-
);
446+
let func = hugr.add_node_with_parent(module, ops::FuncDefn::new("main", signature.clone()));
453447
let inp = hugr.add_node_with_parent(
454448
func,
455449
ops::Input {

hugr-core/src/hugr/hugrmut.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,10 @@ mod test {
630630
// Start a main function with two nat inputs.
631631
let f: Node = hugr.add_node_with_parent(
632632
module,
633-
ops::FuncDefn {
634-
name: "main".into(),
635-
signature: Signature::new(vec![usize_t()], vec![usize_t(), usize_t()]).into(),
636-
},
633+
ops::FuncDefn::new(
634+
"main",
635+
Signature::new(usize_t(), vec![usize_t(), usize_t()]),
636+
),
637637
);
638638

639639
{
@@ -676,13 +676,8 @@ mod test {
676676
hugr.use_extension(PRELUDE.to_owned());
677677
let root = hugr.entrypoint();
678678
let [foo, bar] = ["foo", "bar"].map(|name| {
679-
let fd = hugr.add_node_with_parent(
680-
root,
681-
FuncDefn {
682-
name: name.to_string(),
683-
signature: Signature::new_endo(usize_t()).into(),
684-
},
685-
);
679+
let fd = hugr
680+
.add_node_with_parent(root, FuncDefn::new(name, Signature::new_endo(usize_t())));
686681
let inp = hugr.add_node_with_parent(fd, Input::new(usize_t()));
687682
let out = hugr.add_node_with_parent(fd, Output::new(usize_t()));
688683
hugr.connect(inp, 0, out, 0);

hugr-core/src/hugr/serialize/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -506,8 +506,8 @@ fn roundtrip_polyfunctype_varlen(#[case] poly_func_type: PolyFuncTypeRV) {
506506

507507
#[rstest]
508508
#[case(ops::Module::new())]
509-
#[case(ops::FuncDefn { name: "polyfunc1".into(), signature: polyfunctype1()})]
510-
#[case(ops::FuncDecl { name: "polyfunc2".into(), signature: polyfunctype1()})]
509+
#[case(ops::FuncDefn::new("polyfunc1", polyfunctype1()))]
510+
#[case(ops::FuncDecl::new("polyfunc2", polyfunctype1()))]
511511
#[case(ops::AliasDefn { name: "aliasdefn".into(), definition: Type::new_unit_sum(4)})]
512512
#[case(ops::AliasDecl { name: "aliasdecl".into(), bound: TypeBound::Any})]
513513
#[case(ops::Const::new(Value::false_val()))]

hugr-core/src/hugr/validate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::ops::custom::{ExtensionOp, OpaqueOpError};
1616
use crate::ops::validate::{
1717
ChildrenEdgeData, ChildrenValidationError, EdgeValidationError, OpValidityFlags,
1818
};
19-
use crate::ops::{FuncDefn, NamedOp, OpName, OpTag, OpTrait, OpType, ValidateOp};
19+
use crate::ops::{NamedOp, OpName, OpTag, OpTrait, OpType, ValidateOp};
2020
use crate::types::EdgeKind;
2121
use crate::types::type_param::TypeParam;
2222
use crate::{Direction, Port};
@@ -580,8 +580,8 @@ impl<'a, H: HugrView> ValidationContext<'a, H> {
580580

581581
// For FuncDefn's, only the type variables declared by the FuncDefn can be referred to by nodes
582582
// inside the function. (The same would be true for FuncDecl's, but they have no child nodes.)
583-
let var_decls = if let OpType::FuncDefn(FuncDefn { signature, .. }) = op_type {
584-
signature.params()
583+
let var_decls = if let OpType::FuncDefn(fd) = op_type {
584+
fd.signature().params()
585585
} else {
586586
var_decls
587587
};

0 commit comments

Comments
 (0)