Skip to content

Commit

Permalink
refactor!: remove SignatureDescription (#644)
Browse files Browse the repository at this point in the history
BREAKING CHANGES: `SignatureDescription` no longer exists.
  • Loading branch information
ss2165 committed Nov 6, 2023
1 parent a241381 commit 080eda0
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 147 deletions.
24 changes: 0 additions & 24 deletions src/extension/op_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use super::{
TypeParametrised,
};

use crate::types::SignatureDescription;

use crate::types::FunctionType;

use crate::types::type_param::TypeArg;
Expand All @@ -34,18 +32,6 @@ pub trait CustomSignatureFunc: Send + Sync {
misc: &HashMap<String, serde_yaml::Value>,
extension_registry: &ExtensionRegistry,
) -> Result<FunctionType, SignatureError>;

/// Describe the signature of a node, given the operation name,
/// values for the type parameters,
/// and 'misc' data from the extension definition YAML.
fn describe_signature(
&self,
_name: &SmolStr,
_arg_values: &[TypeArg],
_misc: &HashMap<String, serde_yaml::Value>,
) -> SignatureDescription {
SignatureDescription::default()
}
}

// Note this is very much a utility, rather than definitive;
Expand Down Expand Up @@ -208,16 +194,6 @@ impl OpDef {
Ok(res)
}

/// Optional description of the ports in the signature.
pub fn signature_desc(&self, args: &[TypeArg]) -> SignatureDescription {
match &self.signature_func {
SignatureFunc::FromDecl { .. } => {
todo!()
}
SignatureFunc::CustomFunc(bf) => bf.describe_signature(&self.name, args, &self.misc),
}
}

pub(crate) fn should_serialize_signature(&self) -> bool {
match self.signature_func {
SignatureFunc::CustomFunc(_) => true,
Expand Down
8 changes: 1 addition & 7 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub mod leaf;
pub mod module;
pub mod tag;
pub mod validate;
use crate::types::{EdgeKind, FunctionType, SignatureDescription, Type};
use crate::types::{EdgeKind, FunctionType, Type};
use crate::PortIndex;
use crate::{Direction, Port};

Expand Down Expand Up @@ -189,12 +189,6 @@ pub trait OpTrait {
fn signature(&self) -> FunctionType {
Default::default()
}
/// Optional description of the ports in the signature.
///
/// Only dataflow operations have a non-empty signature.
fn signature_desc(&self) -> SignatureDescription {
Default::default()
}

/// Get the static input type of this operation if it has one (only Some for
/// [`LoadConstant`] and [`Call`])
Expand Down
9 changes: 1 addition & 8 deletions src/ops/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use crate::extension::{ExtensionId, ExtensionRegistry, OpDef, SignatureError};
use crate::hugr::hugrmut::sealed::HugrMutInternals;
use crate::hugr::{HugrView, NodeType};
use crate::types::{type_param::TypeArg, FunctionType, SignatureDescription};
use crate::types::{type_param::TypeArg, FunctionType};
use crate::{Hugr, Node};

use super::tag::OpTag;
Expand Down Expand Up @@ -76,13 +76,6 @@ impl OpTrait for ExternalOp {
}
}

fn signature_desc(&self) -> SignatureDescription {
match self {
Self::Opaque(_) => SignatureDescription::default(),
Self::Extension(ExtensionOp { def, args, .. }) => def.signature_desc(args),
}
}

fn tag(&self) -> OpTag {
OpTag::Leaf
}
Expand Down
11 changes: 1 addition & 10 deletions src/ops/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{OpName, OpTag, OpTrait, StaticTag};

use crate::{
extension::{ExtensionId, ExtensionSet},
types::{EdgeKind, FunctionType, SignatureDescription, Type, TypeRow},
types::{EdgeKind, FunctionType, Type, TypeRow},
};

/// Dataflow operations with no children.
Expand Down Expand Up @@ -118,15 +118,6 @@ impl OpTrait for LeafOp {
}
}

/// Optional description of the ports in the signature.
fn signature_desc(&self) -> SignatureDescription {
match self {
LeafOp::CustomOp(ext) => ext.signature_desc(),
// TODO: More port descriptions
_ => Default::default(),
}
}

fn other_input(&self) -> Option<EdgeKind> {
Some(EdgeKind::StateOrder)
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod type_row;

pub use check::{ConstTypeError, CustomCheckFailure};
pub use custom::CustomType;
pub use signature::{FunctionType, Signature, SignatureDescription};
pub use signature::{FunctionType, Signature};
pub use type_param::TypeArg;
pub use type_row::TypeRow;

Expand Down
98 changes: 1 addition & 97 deletions src/types/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
use pyo3::{pyclass, pymethods};

use delegate::delegate;
use smol_str::SmolStr;
use std::fmt::{self, Display, Write};
use std::ops::Index;

use crate::extension::ExtensionSet;
use crate::types::{Type, TypeRow};
use crate::{Direction, IncomingPort, OutgoingPort, Port, PortIndex};
use crate::{Direction, IncomingPort, OutgoingPort, Port};

#[cfg_attr(feature = "pyo3", pyclass)]
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -249,97 +247,3 @@ impl Display for Signature {
}
}
}

/// Descriptive names for the ports in a [`Signature`].
///
/// This is a separate type from [`Signature`] as it is not normally used during the compiler operations.
#[cfg_attr(feature = "pyo3", pyclass)]
#[derive(Clone, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SignatureDescription {
/// Input of the function.
pub input: Vec<SmolStr>,
/// Output of the function.
pub output: Vec<SmolStr>,
}

#[cfg_attr(feature = "pyo3", pymethods)]
impl SignatureDescription {
/// The number of wires in the signature.
#[inline(always)]
pub fn is_empty(&self) -> bool {
self.input.is_empty() && self.output.is_empty()
}
}

impl SignatureDescription {
/// Create a new signature.
pub fn new(input: impl Into<Vec<SmolStr>>, output: impl Into<Vec<SmolStr>>) -> Self {
Self {
input: input.into(),
output: output.into(),
}
}

/// Create a new signature with only linear inputs and outputs.
pub fn new_linear(linear: impl Into<Vec<SmolStr>>) -> Self {
let linear = linear.into();
SignatureDescription::new(linear.clone(), linear)
}

pub(crate) fn row_zip<'a>(
type_row: &'a TypeRow,
name_row: &'a [SmolStr],
) -> impl Iterator<Item = (&'a SmolStr, &'a Type)> {
name_row
.iter()
.chain(&EmptyStringIterator)
.zip(type_row.iter())
}

/// Iterate over the input wires of the signature and their names.
///
/// Unnamed wires are given an empty string name.
///
/// TODO: Return Option<&String> instead of &String for the description.
pub fn input_zip<'a>(
&'a self,
signature: &'a Signature,
) -> impl Iterator<Item = (&SmolStr, &Type)> {
Self::row_zip(signature.input(), &self.input)
}

/// Iterate over the output wires of the signature and their names.
///
/// Unnamed wires are given an empty string name.
pub fn output_zip<'a>(
&'a self,
signature: &'a Signature,
) -> impl Iterator<Item = (&SmolStr, &Type)> {
Self::row_zip(signature.output(), &self.output)
}
}

impl Index<Port> for SignatureDescription {
type Output = SmolStr;

fn index(&self, index: Port) -> &Self::Output {
match index.direction() {
Direction::Incoming => self.input.get(index.index()).unwrap_or(EMPTY_STRING_REF),
Direction::Outgoing => self.output.get(index.index()).unwrap_or(EMPTY_STRING_REF),
}
}
}

/// An iterator that always returns the an empty string.
pub(crate) struct EmptyStringIterator;

/// A reference to an empty string. Used by [`EmptyStringIterator`].
pub(crate) const EMPTY_STRING_REF: &SmolStr = &SmolStr::new_inline("");

impl<'a> Iterator for &'a EmptyStringIterator {
type Item = &'a SmolStr;

fn next(&mut self) -> Option<Self::Item> {
Some(EMPTY_STRING_REF)
}
}

0 comments on commit 080eda0

Please sign in to comment.