Skip to content

Commit 9a1b615

Browse files
authored
feat: export mangle name function (#2152)
for use in tket2/other downstream compilation tools
1 parent 731081f commit 9a1b615

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

hugr-passes/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub mod untuple;
2525
)]
2626
#[allow(deprecated)]
2727
pub use monomorphize::remove_polyfuncs;
28-
pub use monomorphize::{monomorphize, MonomorphizePass};
28+
pub use monomorphize::{mangle_name, monomorphize, MonomorphizePass};
2929
pub mod replace_types;
3030
pub use replace_types::ReplaceTypes;
3131
pub mod nest_cfgs;

hugr-passes/src/monomorphize.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,19 +306,20 @@ fn write_type_arg_str(arg: &TypeArg, f: &mut std::fmt::Formatter<'_>) -> std::fm
306306
}
307307
}
308308

309-
/// We do our best to generate unique names. Our strategy is to pick out '$' as
309+
/// Produce a mangled name for a monomorphized instance of a function.
310+
///
311+
/// Best effort to generate unique names. The strategy is to pick out '$' as
310312
/// a special character.
311313
///
312-
/// We:
313-
/// - construct a new name of the form `{func_name}$$arg0$arg1$arg2` etc
314-
/// - replace any existing `$` in the function name or type args string
315-
/// representation with `r"\$"`
316-
/// - We depend on the `Display` impl of `Type` to generate the string
317-
/// representation of a `TypeArg::Type`. For other constructors we do the
318-
/// simple obvious thing.
319-
/// - For all TypeArg Constructors we choose a short prefix (e.g. `t` for type)
320-
/// and use "t({arg})" as the string representation of that arg.
321-
fn mangle_name(name: &str, type_args: impl AsRef<[TypeArg]>) -> String {
314+
/// - New name of the form `{func_name}$$arg0$arg1$arg2` constructed.
315+
/// - Existing `$` in the function name or type args string
316+
/// representation replaced with `r"\$"`.
317+
/// - `Display` impl of `Type` used to generate the string
318+
/// representation of a `TypeArg::Type`. Other TypeArgs use `Display`
319+
/// of inner type.
320+
/// - For all TypeArg Constructors a short prefix (e.g. `t` for type)
321+
/// is used as "t({arg})" for the string representation of that arg.
322+
pub fn mangle_name(name: &str, type_args: impl AsRef<[TypeArg]>) -> String {
322323
let name = escape_dollar(name);
323324
format!("${name}${}", TypeArgsList(type_args.as_ref()))
324325
}

0 commit comments

Comments
 (0)