@@ -8,11 +8,10 @@ use crate::hugr::ValidationError;
8
8
use crate :: hugr:: internal:: HugrMutInternals ;
9
9
use crate :: hugr:: views:: HugrView ;
10
10
use crate :: ops;
11
- use crate :: types:: { PolyFuncType , Type , TypeBound } ;
12
-
13
11
use crate :: ops:: handle:: { AliasID , FuncID , NodeHandle } ;
14
-
12
+ use crate :: types :: { PolyFuncType , Type , TypeBound } ;
15
13
use crate :: { Hugr , Node } ;
14
+
16
15
use smol_str:: SmolStr ;
17
16
18
17
/// Builder for a HUGR module.
@@ -51,7 +50,7 @@ impl HugrBuilder for ModuleBuilder<Hugr> {
51
50
}
52
51
53
52
impl < T : AsMut < Hugr > + AsRef < Hugr > > ModuleBuilder < T > {
54
- /// Replace a [`ops::FuncDecl`] with [`ops::FuncDefn`] and return a builder for
53
+ /// Replace a [`ops::FuncDecl`] with public [`ops::FuncDefn`] and return a builder for
55
54
/// the defining graph.
56
55
///
57
56
/// # Errors
@@ -63,19 +62,50 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
63
62
f_id : & FuncID < false > ,
64
63
) -> Result < FunctionBuilder < & mut Hugr > , BuildError > {
65
64
let f_node = f_id. node ( ) ;
66
- let decl =
67
- self . hugr ( )
68
- . get_optype ( f_node)
69
- . as_func_decl ( )
70
- . ok_or ( BuildError :: UnexpectedType {
71
- node : f_node,
72
- op_desc : "crate::ops::OpType::FuncDecl" ,
73
- } ) ?;
74
- let name = decl. func_name ( ) . clone ( ) ;
75
- let sig = decl. signature ( ) . clone ( ) ;
76
- let body = sig. body ( ) . clone ( ) ;
77
- self . hugr_mut ( )
78
- . replace_op ( f_node, ops:: FuncDefn :: new ( name, sig) ) ;
65
+ let opty = self . hugr_mut ( ) . optype_mut ( f_node) ;
66
+ let ops:: OpType :: FuncDecl ( decl) = opty else {
67
+ return Err ( BuildError :: UnexpectedType {
68
+ node : f_node,
69
+ op_desc : "crate::ops::OpType::FuncDecl" ,
70
+ } ) ;
71
+ } ;
72
+
73
+ let body = decl. signature ( ) . body ( ) . clone ( ) ;
74
+ // TODO look for `name_hint` metadata on the FuncDecl and copy to FuncDefn
75
+ * opty = ops:: FuncDefn :: new_link_name (
76
+ format ! ( "From Decl {}" , decl. link_name( ) ) ,
77
+ decl. signature ( ) . clone ( ) ,
78
+ decl. link_name ( ) . clone ( ) ,
79
+ )
80
+ . into ( ) ;
81
+
82
+ let db = DFGBuilder :: create_with_io ( self . hugr_mut ( ) , f_node, body) ?;
83
+ Ok ( FunctionBuilder :: from_dfg_builder ( db) )
84
+ }
85
+
86
+ /// Add a [`ops::FuncDefn`] node, with both `name` and `link_name` explicitly specified.
87
+ /// Returns a builder to define the function body graph.
88
+ ///
89
+ /// # Errors
90
+ ///
91
+ /// This function will return an error if there is an error in adding the
92
+ /// [`ops::FuncDefn`] node.
93
+ pub fn define_function_link_name (
94
+ & mut self ,
95
+ name : impl Into < String > ,
96
+ signature : impl Into < PolyFuncType > ,
97
+ link_name : impl Into < Option < String > > ,
98
+ ) -> Result < FunctionBuilder < & mut Hugr > , BuildError > {
99
+ let signature: PolyFuncType = signature. into ( ) ;
100
+ let body = signature. body ( ) . clone ( ) ;
101
+ let f_node = self . add_child_node ( ops:: FuncDefn :: new_link_name ( name, signature, link_name) ) ;
102
+
103
+ // Add the extensions used by the function types.
104
+ self . use_extensions (
105
+ body. used_extensions ( ) . unwrap_or_else ( |e| {
106
+ panic ! ( "Build-time signatures should have valid extensions. {e}" )
107
+ } ) ,
108
+ ) ;
79
109
80
110
let db = DFGBuilder :: create_with_io ( self . hugr_mut ( ) , f_node, body) ?;
81
111
Ok ( FunctionBuilder :: from_dfg_builder ( db) )
@@ -106,31 +136,36 @@ impl<T: AsMut<Hugr> + AsRef<Hugr>> ModuleBuilder<T> {
106
136
Ok ( declare_n. into ( ) )
107
137
}
108
138
109
- /// Add a [`ops::FuncDefn`] node and returns a builder to define the function
139
+ /// Adds a private [`ops::FuncDefn`] node and returns a builder to define the function
110
140
/// body graph.
111
141
///
112
142
/// # Errors
113
143
///
114
144
/// This function will return an error if there is an error in adding the
115
145
/// [`ops::FuncDefn`] node.
146
+ // ALAN TODO? deprecate and rename to define_private_func(tion)? (Rather long...)
116
147
pub fn define_function (
117
148
& mut self ,
118
149
name : impl Into < String > ,
119
150
signature : impl Into < PolyFuncType > ,
120
151
) -> Result < FunctionBuilder < & mut Hugr > , BuildError > {
121
- let signature: PolyFuncType = signature. into ( ) ;
122
- let body = signature. body ( ) . clone ( ) ;
123
- let f_node = self . add_child_node ( ops:: FuncDefn :: new ( name, signature) ) ;
124
-
125
- // Add the extensions used by the function types.
126
- self . use_extensions (
127
- body. used_extensions ( ) . unwrap_or_else ( |e| {
128
- panic ! ( "Build-time signatures should have valid extensions. {e}" )
129
- } ) ,
130
- ) ;
152
+ self . define_function_link_name ( name, signature, None )
153
+ }
131
154
132
- let db = DFGBuilder :: create_with_io ( self . hugr_mut ( ) , f_node, body) ?;
133
- Ok ( FunctionBuilder :: from_dfg_builder ( db) )
155
+ /// Adds a public [`ops::FuncDefn`] node, with `link_name` the same as `name`,
156
+ /// and returns a builder to define the function body graph.
157
+ ///
158
+ /// # Errors
159
+ ///
160
+ /// This function will return an error if there is an error in adding the
161
+ /// [`ops::FuncDefn`] node.
162
+ pub fn define_function_pub (
163
+ & mut self ,
164
+ name : impl Into < String > ,
165
+ signature : impl Into < PolyFuncType > ,
166
+ ) -> Result < FunctionBuilder < & mut Hugr > , BuildError > {
167
+ let name = name. into ( ) ;
168
+ self . define_function_link_name ( name. clone ( ) , signature, name)
134
169
}
135
170
136
171
/// Add a [`crate::ops::OpType::AliasDefn`] node and return a handle to the Alias.
@@ -218,10 +253,7 @@ mod test {
218
253
219
254
let f_build = module_builder. define_function (
220
255
"main" ,
221
- Signature :: new (
222
- vec ! [ qubit_state_type. get_alias_type( ) ] ,
223
- vec ! [ qubit_state_type. get_alias_type( ) ] ,
224
- ) ,
256
+ Signature :: new_endo ( qubit_state_type. get_alias_type ( ) ) ,
225
257
) ?;
226
258
n_identity ( f_build) ?;
227
259
module_builder. finish_hugr ( )
0 commit comments