Skip to content

Commit

Permalink
No inline string literals for the constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
zrho committed Jan 15, 2025
1 parent 90bd24b commit 05c8784
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions hugr-core/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ impl<'a> Context<'a> {
parts: contents.into_bump_slice(),
});

let symbol = self.resolve_symbol("collections.array.const");
let symbol = self.resolve_symbol(ArrayValue::CTR_NAME);
let args = self.bump.alloc_slice_copy(&[element_type, len, contents]);
return self.make_term(model::Term::ApplyFull { symbol, args });
}
Expand All @@ -1054,14 +1054,14 @@ impl<'a> Context<'a> {
let bitwidth = self.make_term(model::Term::Nat(v.log_width() as u64));
let literal = self.make_term(model::Term::Nat(v.value_u()));

let symbol = self.resolve_symbol("arithmetic.int.const");
let symbol = self.resolve_symbol(ConstInt::CTR_NAME);
let args = self.bump.alloc_slice_copy(&[bitwidth, literal]);
return self.make_term(model::Term::ApplyFull { symbol, args });
}

if let Some(v) = e.value().downcast_ref::<ConstF64>() {
let literal = self.make_term(model::Term::Nat(v.to_bits()));
let symbol = self.resolve_symbol("arithmetic.float.const-f64");
let symbol = self.resolve_symbol(ConstF64::CTR_NAME);
let args = self.bump.alloc_slice_copy(&[literal]);
return self.make_term(model::Term::ApplyFull { symbol, args });
}
Expand Down
6 changes: 3 additions & 3 deletions hugr-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ impl<'a> Context<'a> {
// NOTE: We have special cased arrays, integers, and floats for now.
// TODO: Allow arbitrary extension values to be imported from terms.

if symbol_name == "collections.array.const" {
if symbol_name == ArrayValue::CTR_NAME {
let element_type_term =
args.first().ok_or(model::ModelError::TypeError(term_id))?;
let element_type = self.import_type(*element_type_term)?;
Expand All @@ -1375,7 +1375,7 @@ impl<'a> Context<'a> {
return Ok(ArrayValue::new(element_type, contents).into());
}

if symbol_name == "arithmetic.int.const" {
if symbol_name == ConstInt::CTR_NAME {
let bitwidth = {
let bitwidth = args.first().ok_or(model::ModelError::TypeError(term_id))?;
let model::Term::Nat(bitwidth) = self.get_term(*bitwidth)? else {
Expand All @@ -1400,7 +1400,7 @@ impl<'a> Context<'a> {
.into());
}

if symbol_name == "arithmetic.float.const-f64" {
if symbol_name == ConstF64::CTR_NAME {
let value = {
let value = args.first().ok_or(model::ModelError::TypeError(term_id))?;
let model::Term::Nat(value) = self.get_term(*value)? else {
Expand Down
3 changes: 3 additions & 0 deletions hugr-core/src/std_extensions/arithmetic/float_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ impl std::ops::Deref for ConstF64 {
}

impl ConstF64 {
/// Name of the constructor for creating constant 64bit floats.
pub(crate) const CTR_NAME: &'static str = "arithmetic.float.const-f64";

/// Create a new [`ConstF64`]
pub fn new(value: f64) -> Self {
// This function can't be `const` because `is_finite()` is not yet stable as a const function.
Expand Down
3 changes: 3 additions & 0 deletions hugr-core/src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ pub struct ConstInt {
}

impl ConstInt {
/// Name of the constructor for creating constant integers.
pub(crate) const CTR_NAME: &'static str = "arithmetic.int.const";

/// Create a new [`ConstInt`] with a given width and unsigned value
pub fn new_u(log_width: u8, value: u64) -> Result<Self, ConstTypeError> {
if !is_valid_log_width(log_width) {
Expand Down
3 changes: 3 additions & 0 deletions hugr-core/src/std_extensions/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub struct ArrayValue {
}

impl ArrayValue {
/// Name of the constructor for creating constant arrays.
pub(crate) const CTR_NAME: &'static str = "collections.array.const";

/// Create a new [CustomConst] for an array of values of type `typ`.
/// That all values are of type `typ` is not checked here.
pub fn new(typ: Type, contents: impl IntoIterator<Item = Value>) -> Self {
Expand Down

0 comments on commit 05c8784

Please sign in to comment.