Avoid some code duplication around getting names of numeric types.

This commit is contained in:
Michael Woerister
2015-11-23 15:59:36 +01:00
parent 4891c00634
commit 3be1d8ca7d
6 changed files with 44 additions and 110 deletions

View File

@@ -1258,7 +1258,7 @@ impl fmt::Debug for IntTy {
impl fmt::Display for IntTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::int_ty_to_string(*self, None))
write!(f, "{}", ast_util::int_ty_to_string(*self))
}
}
@@ -1303,7 +1303,7 @@ impl fmt::Debug for UintTy {
impl fmt::Display for UintTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", ast_util::uint_ty_to_string(*self, None))
write!(f, "{}", ast_util::uint_ty_to_string(*self))
}
}

View File

@@ -111,26 +111,23 @@ pub fn is_path(e: P<Expr>) -> bool {
match e.node { ExprPath(..) => true, _ => false }
}
/// Get a string representation of a signed int type, with its value.
/// We want to avoid "45int" and "-3int" in favor of "45" and "-3"
pub fn int_ty_to_string(t: IntTy, val: Option<i64>) -> String {
let s = match t {
pub fn int_ty_to_string(t: IntTy) -> &'static str {
match t {
TyIs => "isize",
TyI8 => "i8",
TyI16 => "i16",
TyI32 => "i32",
TyI64 => "i64"
};
match val {
// cast to a u64 so we can correctly print INT64_MIN. All integral types
// are parsed as u64, so we wouldn't want to print an extra negative
// sign.
Some(n) => format!("{}{}", n as u64, s),
None => s.to_string()
}
}
pub fn int_val_to_string(t: IntTy, val: i64) -> String {
// cast to a u64 so we can correctly print INT64_MIN. All integral types
// are parsed as u64, so we wouldn't want to print an extra negative
// sign.
format!("{}{}", val as u64, int_ty_to_string(t))
}
pub fn int_ty_max(t: IntTy) -> u64 {
match t {
TyI8 => 0x80,
@@ -140,23 +137,20 @@ pub fn int_ty_max(t: IntTy) -> u64 {
}
}
/// Get a string representation of an unsigned int type, with its value.
/// We want to avoid "42u" in favor of "42us". "42uint" is right out.
pub fn uint_ty_to_string(t: UintTy, val: Option<u64>) -> String {
let s = match t {
pub fn uint_ty_to_string(t: UintTy) -> &'static str {
match t {
TyUs => "usize",
TyU8 => "u8",
TyU16 => "u16",
TyU32 => "u32",
TyU64 => "u64"
};
match val {
Some(n) => format!("{}{}", n, s),
None => s.to_string()
}
}
pub fn uint_val_to_string(t: UintTy, val: u64) -> String {
format!("{}{}", val, uint_ty_to_string(t))
}
pub fn uint_ty_max(t: UintTy) -> u64 {
match t {
TyU8 => 0xff,
@@ -166,10 +160,10 @@ pub fn uint_ty_max(t: UintTy) -> u64 {
}
}
pub fn float_ty_to_string(t: FloatTy) -> String {
pub fn float_ty_to_string(t: FloatTy) -> &'static str {
match t {
TyF32 => "f32".to_string(),
TyF64 => "f64".to_string(),
TyF32 => "f32",
TyF64 => "f64",
}
}

View File

@@ -651,15 +651,15 @@ pub trait PrintState<'a> {
match t {
ast::SignedIntLit(st, ast::Plus) => {
word(self.writer(),
&ast_util::int_ty_to_string(st, Some(i as i64)))
&ast_util::int_val_to_string(st, i as i64))
}
ast::SignedIntLit(st, ast::Minus) => {
let istr = ast_util::int_ty_to_string(st, Some(-(i as i64)));
let istr = ast_util::int_val_to_string(st, -(i as i64));
word(self.writer(),
&format!("-{}", istr))
}
ast::UnsignedIntLit(ut) => {
word(self.writer(), &ast_util::uint_ty_to_string(ut, Some(i)))
word(self.writer(), &ast_util::uint_val_to_string(ut, i))
}
ast::UnsuffixedIntLit(ast::Plus) => {
word(self.writer(), &format!("{}", i))