Use Display rather than having an explicit name() function for types.

This is more idiomatic Rust.
This commit is contained in:
Dan Gohman
2018-10-29 13:31:50 -07:00
committed by Benjamin Bouvier
parent 2a5f245b1d
commit 0b769f5020
2 changed files with 40 additions and 41 deletions

View File

@@ -79,16 +79,6 @@ impl ValueType {
self.width() / 8 self.width() / 8
} }
/// Get the name of this type.
pub fn name(&self) -> String {
match *self {
ValueType::BV(ref b) => b.name(),
ValueType::Lane(l) => l.name(),
ValueType::Special(s) => s.name(),
ValueType::Vector(ref v) => v.name(),
}
}
/// Find the unique number associated with this type. /// Find the unique number associated with this type.
pub fn number(&self) -> Option<u8> { pub fn number(&self) -> Option<u8> {
match *self { match *self {
@@ -101,7 +91,7 @@ impl ValueType {
/// Return the name of this type for generated Rust source files. /// Return the name of this type for generated Rust source files.
pub fn _rust_name(&self) -> String { pub fn _rust_name(&self) -> String {
format!("{}{}", _RUST_NAME_PREFIX, self.name().to_uppercase()) format!("{}{}", _RUST_NAME_PREFIX, self.to_string().to_uppercase())
} }
/// Return true iff: /// Return true iff:
@@ -119,7 +109,12 @@ impl ValueType {
impl fmt::Display for ValueType { impl fmt::Display for ValueType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name()) match *self {
ValueType::BV(ref b) => b.fmt(f),
ValueType::Lane(l) => l.fmt(f),
ValueType::Special(s) => s.fmt(f),
ValueType::Vector(ref v) => v.fmt(f),
}
} }
} }
@@ -193,15 +188,6 @@ impl LaneType {
} }
} }
/// Get the name of this lane type.
pub fn name(self) -> String {
match self {
LaneType::BoolType(_) => format!("b{}", self.lane_bits()),
LaneType::FloatType(_) => format!("f{}", self.lane_bits()),
LaneType::IntType(_) => format!("i{}", self.lane_bits()),
}
}
/// Find the unique number associated with this lane type. /// Find the unique number associated with this lane type.
pub fn number(self) -> u8 { pub fn number(self) -> u8 {
LANE_BASE + match self { LANE_BASE + match self {
@@ -220,6 +206,16 @@ impl LaneType {
} }
} }
impl fmt::Display for LaneType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LaneType::BoolType(_) => write!(f, "b{}", self.lane_bits()),
LaneType::FloatType(_) => write!(f, "f{}", self.lane_bits()),
LaneType::IntType(_) => write!(f, "i{}", self.lane_bits()),
}
}
}
impl fmt::Debug for LaneType { impl fmt::Debug for LaneType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let inner_msg = format!("bits={}", self.lane_bits()); let inner_msg = format!("bits={}", self.lane_bits());
@@ -309,7 +305,7 @@ impl VectorType {
format!( format!(
"A SIMD vector with {} lanes containing a `{}` each.", "A SIMD vector with {} lanes containing a `{}` each.",
self.lane_count(), self.lane_count(),
self.base.name() self.base
) )
} }
@@ -323,11 +319,6 @@ impl VectorType {
self.lanes self.lanes
} }
/// Get the name of this vector type.
pub fn name(&self) -> String {
format!("{}x{}", self.base.name(), self.lane_count())
}
/// Find the unique number associated with this vector type. /// Find the unique number associated with this vector type.
/// ///
/// Vector types are encoded with the lane type in the low 4 bits and /// Vector types are encoded with the lane type in the low 4 bits and
@@ -340,12 +331,18 @@ impl VectorType {
} }
} }
impl fmt::Display for VectorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}x{}", self.base, self.lane_count())
}
}
impl fmt::Debug for VectorType { impl fmt::Debug for VectorType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"VectorType(base={}, lanes={})", "VectorType(base={}, lanes={})",
self.base.name(), self.base,
self.lane_count() self.lane_count()
) )
} }
@@ -371,10 +368,11 @@ impl BVType {
pub fn lane_bits(&self) -> u64 { pub fn lane_bits(&self) -> u64 {
self.bits self.bits
} }
}
/// Get the name of this bitvector type. impl fmt::Display for BVType {
pub fn name(&self) -> String { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
format!("bv{}", self.bits) write!(f, "bv{}", self.bits)
} }
} }
@@ -414,14 +412,6 @@ impl SpecialType {
} }
} }
/// Get the name of this special type.
pub fn name(self) -> String {
match self {
SpecialType::Flag(base_types::Flag::IFlags) => "iflags".to_string(),
SpecialType::Flag(base_types::Flag::FFlags) => "fflags".to_string(),
}
}
/// Find the unique number associated with this special type. /// Find the unique number associated with this special type.
pub fn number(self) -> u8 { pub fn number(self) -> u8 {
match self { match self {
@@ -431,13 +421,22 @@ impl SpecialType {
} }
} }
impl fmt::Display for SpecialType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
SpecialType::Flag(base_types::Flag::IFlags) => write!(f, "iflags"),
SpecialType::Flag(base_types::Flag::FFlags) => write!(f, "fflags"),
}
}
}
impl fmt::Debug for SpecialType { impl fmt::Debug for SpecialType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!( write!(
f, f,
"{}", "{}",
match *self { match *self {
SpecialType::Flag(_) => format!("FlagsType({})", self.name()), SpecialType::Flag(_) => format!("FlagsType({})", self),
} }
) )
} }

View File

@@ -13,7 +13,7 @@ use srcgen;
/// Emit a constant definition of a single value type. /// Emit a constant definition of a single value type.
fn emit_type(ty: &cdsl_types::ValueType, fmt: &mut srcgen::Formatter) -> Result<(), error::Error> { fn emit_type(ty: &cdsl_types::ValueType, fmt: &mut srcgen::Formatter) -> Result<(), error::Error> {
let name = ty.name().to_uppercase(); let name = ty.to_string().to_uppercase();
let number = ty.number().ok_or_else(|| { let number = ty.number().ok_or_else(|| {
error::Error::with_msg(format!( error::Error::with_msg(format!(
"Could not emit type `{}` which has no number.", "Could not emit type `{}` which has no number.",