Add `to_symbol` methods.

This commit is contained in:
Nicholas Nethercote 2019-05-23 12:22:43 +10:00
parent 9c7d28d4fd
commit 21f28448e0
5 changed files with 36 additions and 4 deletions

View File

@ -3009,6 +3009,7 @@ dependencies = [
"rustc_cratesio_shim 0.0.0",
"rustc_data_structures 0.0.0",
"serialize 0.0.0",
"syntax_pos 0.0.0",
]
[[package]]

View File

@ -15,3 +15,4 @@ log = "0.4"
rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
rustc_data_structures = { path = "../librustc_data_structures" }
serialize = { path = "../libserialize" }
syntax_pos = { path = "../libsyntax_pos" }

View File

@ -7,6 +7,7 @@ use std::fmt;
use std::ops::{Add, Deref, Sub, Mul, AddAssign, Range, RangeInclusive};
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use syntax_pos::symbol::{sym, Symbol};
pub mod call;
@ -552,6 +553,13 @@ impl FloatTy {
}
}
pub fn to_symbol(self) -> Symbol {
match self {
FloatTy::F32 => sym::f32,
FloatTy::F64 => sym::f64,
}
}
pub fn bit_width(self) -> usize {
match self {
FloatTy::F32 => 32,

View File

@ -10,7 +10,7 @@ use crate::parse::token;
use crate::print::pprust;
use crate::ptr::P;
use crate::source_map::{dummy_spanned, respan, Spanned};
use crate::symbol::{kw, Symbol};
use crate::symbol::{kw, sym, Symbol};
use crate::tokenstream::TokenStream;
use crate::ThinVec;
@ -1531,6 +1531,17 @@ impl IntTy {
}
}
pub fn to_symbol(&self) -> Symbol {
match *self {
IntTy::Isize => sym::isize,
IntTy::I8 => sym::i8,
IntTy::I16 => sym::i16,
IntTy::I32 => sym::i32,
IntTy::I64 => sym::i64,
IntTy::I128 => sym::i128,
}
}
pub fn val_to_string(&self, val: i128) -> String {
// Cast to a `u128` so we can correctly print `INT128_MIN`. All integral types
// are parsed as `u128`, so we wouldn't want to print an extra negative
@ -1572,6 +1583,17 @@ impl UintTy {
}
}
pub fn to_symbol(&self) -> Symbol {
match *self {
UintTy::Usize => sym::usize,
UintTy::U8 => sym::u8,
UintTy::U16 => sym::u16,
UintTy::U32 => sym::u32,
UintTy::U64 => sym::u64,
UintTy::U128 => sym::u128,
}
}
pub fn val_to_string(&self, val: u128) -> String {
format!("{}{}", val, self.ty_to_string())
}

View File

@ -193,14 +193,14 @@ impl LitKind {
}
LitKind::Int(n, ty) => {
let suffix = match ty {
ast::LitIntType::Unsigned(ty) => Some(Symbol::intern(ty.ty_to_string())),
ast::LitIntType::Signed(ty) => Some(Symbol::intern(ty.ty_to_string())),
ast::LitIntType::Unsigned(ty) => Some(ty.to_symbol()),
ast::LitIntType::Signed(ty) => Some(ty.to_symbol()),
ast::LitIntType::Unsuffixed => None,
};
(token::Integer, sym::integer(n), suffix)
}
LitKind::Float(symbol, ty) => {
(token::Float, symbol, Some(Symbol::intern(ty.ty_to_string())))
(token::Float, symbol, Some(ty.to_symbol()))
}
LitKind::FloatUnsuffixed(symbol) => {
(token::Float, symbol, None)