Skip to content

Commit

Permalink
feat: Make internals of int ops and the "int" CustomType more public. (
Browse files Browse the repository at this point in the history
…#1114)

closes #1113.
  • Loading branch information
doug-q authored May 29, 2024
1 parent 1dc14fa commit 20f642b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ repos:
- id: cargo-doc
name: cargo doc
description: Generate documentation with `cargo doc`.
entry: cargo doc --no-deps --all-features --workspace
entry: RUSTDOCFLAGS=-Dwarnings cargo doc --no-deps --all-features --workspace
language: system
files: \.rs$
pass_filenames: false
Expand Down
37 changes: 21 additions & 16 deletions hugr-core/src/std_extensions/arithmetic/int_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,22 @@ lazy_static! {

/// Concrete integer operation with integer widths set.
#[derive(Debug, Clone, PartialEq)]
pub struct IntOpType {
def: IntOpDef,
log_widths: Vec<u8>,
#[non_exhaustive]
pub struct ConcreteIntOp {
/// The kind of int op.
pub def: IntOpDef,
/// The width parameters of the int op. These are interpreted differently,
/// depending on `def`. The types of inputs and outputs of the op will have
/// [int_type]s of these widths.
pub log_widths: Vec<u8>,
}

impl NamedOp for IntOpType {
impl NamedOp for ConcreteIntOp {
fn name(&self) -> OpName {
self.def.name()
}
}
impl MakeExtensionOp for IntOpType {
impl MakeExtensionOp for ConcreteIntOp {
fn from_extension_op(ext_op: &ExtensionOp) -> Result<Self, OpLoadError> {
let def = IntOpDef::from_def(ext_op.def())?;
let args = ext_op.args();
Expand All @@ -305,7 +310,7 @@ impl MakeExtensionOp for IntOpType {
}
}

impl MakeRegisteredOp for IntOpType {
impl MakeRegisteredOp for ConcreteIntOp {
fn extension_id(&self) -> ExtensionId {
EXTENSION_ID.to_owned()
}
Expand All @@ -316,26 +321,26 @@ impl MakeRegisteredOp for IntOpType {
}

impl IntOpDef {
/// Initialize a concrete [IntOpType] from a [IntOpDef] which requires no
/// Initialize a [ConcreteIntOp] from a [IntOpDef] which requires no
/// integer widths set.
pub fn without_log_width(self) -> IntOpType {
IntOpType {
pub fn without_log_width(self) -> ConcreteIntOp {
ConcreteIntOp {
def: self,
log_widths: vec![],
}
}
/// Initialize a concrete [IntOpType] from a [IntOpDef] which requires one
/// Initialize a [ConcreteIntOp] from a [IntOpDef] which requires one
/// integer width set.
pub fn with_log_width(self, log_width: u8) -> IntOpType {
IntOpType {
pub fn with_log_width(self, log_width: u8) -> ConcreteIntOp {
ConcreteIntOp {
def: self,
log_widths: vec![log_width],
}
}
/// Initialize a concrete [IntOpType] from a [IntOpDef] which requires two
/// Initialize a [ConcreteIntOp] from a [IntOpDef] which requires two
/// integer widths set.
pub fn with_two_log_widths(self, first_log_width: u8, second_log_width: u8) -> IntOpType {
IntOpType {
pub fn with_two_log_widths(self, first_log_width: u8, second_log_width: u8) -> ConcreteIntOp {
ConcreteIntOp {
def: self,
log_widths: vec![first_log_width, second_log_width],
}
Expand Down Expand Up @@ -419,6 +424,6 @@ mod test {
);
let ext_op = o.clone().to_extension_op().unwrap();

assert_eq!(IntOpType::from_extension_op(&ext_op).unwrap(), o);
assert_eq!(ConcreteIntOp::from_extension_op(&ext_op).unwrap(), o);
}
}
14 changes: 9 additions & 5 deletions hugr-core/src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ pub const EXTENSION_ID: ExtensionId = ExtensionId::new_unchecked("arithmetic.int
/// Identifier for the integer type.
pub const INT_TYPE_ID: TypeName = TypeName::new_inline("int");

pub(crate) fn int_custom_type(width_arg: impl Into<TypeArg>) -> CustomType {
/// Integer type of a given bit width (specified by the TypeArg). Depending on
/// the operation, the semantic interpretation may be unsigned integer, signed
/// integer or bit string.
pub fn int_custom_type(width_arg: impl Into<TypeArg>) -> CustomType {
CustomType::new(INT_TYPE_ID, [width_arg.into()], EXTENSION_ID, TypeBound::Eq)
}

/// Integer type of a given bit width (specified by the TypeArg).
/// Depending on the operation, the semantic interpretation may be unsigned integer, signed integer
/// or bit string.
pub(super) fn int_type(width_arg: impl Into<TypeArg>) -> Type {
///
/// Constructed from [int_custom_type].
pub fn int_type(width_arg: impl Into<TypeArg>) -> Type {
Type::new_extension(int_custom_type(width_arg.into()))
}

Expand All @@ -40,7 +43,8 @@ lazy_static! {
.unwrap();
}

const fn is_valid_log_width(n: u8) -> bool {
/// Returns whether `n` is a valid `log_width` for an [int_type].
pub const fn is_valid_log_width(n: u8) -> bool {
n < LOG_WIDTH_BOUND
}

Expand Down

0 comments on commit 20f642b

Please sign in to comment.