2018-12-27 17:07:21 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use ra_syntax::{ast, SmolStr};
|
|
|
|
|
|
|
|
/// `Name` is a wrapper around string, which is used in hir for both references
|
2019-01-08 23:47:12 +00:00
|
|
|
/// and declarations. In theory, names should also carry hygiene info, but we are
|
2018-12-27 17:07:21 +00:00
|
|
|
/// not there yet!
|
2019-05-21 10:18:30 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
2018-12-27 17:07:21 +00:00
|
|
|
pub struct Name {
|
|
|
|
text: SmolStr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Display::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 18:02:08 +00:00
|
|
|
impl fmt::Debug for Name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self.text, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 17:07:21 +00:00
|
|
|
impl Name {
|
2019-03-20 19:33:26 +00:00
|
|
|
/// Note: this is private to make creating name from random string hard.
|
|
|
|
/// Hopefully, this should allow us to integrate hygiene cleaner in the
|
|
|
|
/// future, and to switch to interned representation of names.
|
|
|
|
fn new(text: SmolStr) -> Name {
|
2018-12-28 18:34:58 +00:00
|
|
|
Name { text }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn missing() -> Name {
|
|
|
|
Name::new("[missing name]".into())
|
|
|
|
}
|
|
|
|
|
2019-01-05 21:37:59 +00:00
|
|
|
pub(crate) fn self_param() -> Name {
|
|
|
|
Name::new("self".into())
|
|
|
|
}
|
|
|
|
|
2019-01-06 00:00:34 +00:00
|
|
|
pub(crate) fn self_type() -> Name {
|
|
|
|
Name::new("Self".into())
|
|
|
|
}
|
|
|
|
|
2018-12-28 18:34:58 +00:00
|
|
|
pub(crate) fn tuple_field_name(idx: usize) -> Name {
|
|
|
|
Name::new(idx.to_string().into())
|
|
|
|
}
|
|
|
|
|
2019-05-12 16:33:47 +00:00
|
|
|
// Needed for Deref
|
|
|
|
pub(crate) fn target() -> Name {
|
|
|
|
Name::new("Target".into())
|
|
|
|
}
|
|
|
|
|
2019-04-22 12:56:28 +00:00
|
|
|
// There's should be no way to extract a string out of `Name`: `Name` in the
|
|
|
|
// future, `Name` will include hygiene information, and you can't encode
|
|
|
|
// hygiene into a String.
|
|
|
|
//
|
|
|
|
// If you need to compare something with `Name`, compare `Name`s directly.
|
|
|
|
//
|
|
|
|
// If you need to render `Name` for the user, use the `Display` impl, but be
|
|
|
|
// aware that it strips hygiene info.
|
|
|
|
#[deprecated(note = "use to_string instead")]
|
|
|
|
pub fn as_smolstr(&self) -> &SmolStr {
|
|
|
|
&self.text
|
2019-02-19 21:32:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 17:26:15 +00:00
|
|
|
pub(crate) fn as_known_name(&self) -> Option<KnownName> {
|
|
|
|
let name = match self.text.as_str() {
|
|
|
|
"isize" => KnownName::Isize,
|
|
|
|
"i8" => KnownName::I8,
|
|
|
|
"i16" => KnownName::I16,
|
|
|
|
"i32" => KnownName::I32,
|
|
|
|
"i64" => KnownName::I64,
|
|
|
|
"i128" => KnownName::I128,
|
|
|
|
"usize" => KnownName::Usize,
|
|
|
|
"u8" => KnownName::U8,
|
|
|
|
"u16" => KnownName::U16,
|
|
|
|
"u32" => KnownName::U32,
|
|
|
|
"u64" => KnownName::U64,
|
|
|
|
"u128" => KnownName::U128,
|
|
|
|
"f32" => KnownName::F32,
|
|
|
|
"f64" => KnownName::F64,
|
2019-01-07 19:43:41 +00:00
|
|
|
"bool" => KnownName::Bool,
|
|
|
|
"char" => KnownName::Char,
|
|
|
|
"str" => KnownName::Str,
|
2019-01-05 21:37:59 +00:00
|
|
|
"Self" => KnownName::SelfType,
|
|
|
|
"self" => KnownName::SelfParam,
|
2019-03-02 20:59:04 +00:00
|
|
|
"macro_rules" => KnownName::MacroRules,
|
2018-12-27 17:26:15 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(name)
|
2018-12-27 17:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait AsName {
|
|
|
|
fn as_name(&self) -> Name;
|
|
|
|
}
|
|
|
|
|
2019-01-08 08:28:42 +00:00
|
|
|
impl AsName for ast::NameRef {
|
2018-12-27 17:07:21 +00:00
|
|
|
fn as_name(&self) -> Name {
|
2019-01-08 09:23:10 +00:00
|
|
|
Name::new(self.text().clone())
|
2018-12-27 17:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-08 08:28:42 +00:00
|
|
|
impl AsName for ast::Name {
|
2018-12-27 17:07:21 +00:00
|
|
|
fn as_name(&self) -> Name {
|
2019-01-08 09:23:10 +00:00
|
|
|
Name::new(self.text().clone())
|
2018-12-27 17:07:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 20:34:45 +00:00
|
|
|
impl<'a> AsName for ast::FieldKind<'a> {
|
|
|
|
fn as_name(&self) -> Name {
|
|
|
|
match self {
|
|
|
|
ast::FieldKind::Name(nr) => nr.as_name(),
|
|
|
|
ast::FieldKind::Index(idx) => Name::new(idx.text().clone()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-27 17:07:21 +00:00
|
|
|
impl AsName for ra_db::Dependency {
|
|
|
|
fn as_name(&self) -> Name {
|
|
|
|
Name::new(self.name.clone())
|
|
|
|
}
|
|
|
|
}
|
2018-12-27 17:26:15 +00:00
|
|
|
|
|
|
|
// Ideally, should be replaced with
|
|
|
|
// ```
|
|
|
|
// const ISIZE: Name = Name::new("isize")
|
|
|
|
// ```
|
|
|
|
// but const-fn is not that powerful yet.
|
2019-05-30 12:03:58 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
2018-12-27 17:26:15 +00:00
|
|
|
pub(crate) enum KnownName {
|
|
|
|
Isize,
|
|
|
|
I8,
|
|
|
|
I16,
|
|
|
|
I32,
|
|
|
|
I64,
|
|
|
|
I128,
|
|
|
|
|
|
|
|
Usize,
|
|
|
|
U8,
|
|
|
|
U16,
|
|
|
|
U32,
|
|
|
|
U64,
|
|
|
|
U128,
|
|
|
|
|
|
|
|
F32,
|
|
|
|
F64,
|
2018-12-29 22:20:12 +00:00
|
|
|
|
2019-01-07 19:43:41 +00:00
|
|
|
Bool,
|
|
|
|
Char,
|
|
|
|
Str,
|
|
|
|
|
2019-01-05 21:37:59 +00:00
|
|
|
SelfType,
|
|
|
|
SelfParam,
|
2019-03-02 20:59:04 +00:00
|
|
|
|
|
|
|
MacroRules,
|
2018-12-27 17:26:15 +00:00
|
|
|
}
|
2019-05-30 12:03:58 +00:00
|
|
|
|
|
|
|
impl AsName for KnownName {
|
|
|
|
fn as_name(&self) -> Name {
|
|
|
|
let s = match self {
|
|
|
|
KnownName::Isize => "isize",
|
|
|
|
KnownName::I8 => "i8",
|
|
|
|
KnownName::I16 => "i16",
|
|
|
|
KnownName::I32 => "i32",
|
|
|
|
KnownName::I64 => "i64",
|
|
|
|
KnownName::I128 => "i128",
|
|
|
|
KnownName::Usize => "usize",
|
|
|
|
KnownName::U8 => "u8",
|
|
|
|
KnownName::U16 => "u16",
|
|
|
|
KnownName::U32 => "u32",
|
|
|
|
KnownName::U64 => "u64",
|
|
|
|
KnownName::U128 => "u128",
|
|
|
|
KnownName::F32 => "f32",
|
|
|
|
KnownName::F64 => "f64",
|
|
|
|
KnownName::Bool => "bool",
|
|
|
|
KnownName::Char => "char",
|
|
|
|
KnownName::Str => "str",
|
|
|
|
KnownName::SelfType => "Self",
|
|
|
|
KnownName::SelfParam => "self",
|
|
|
|
KnownName::MacroRules => "macro_rules",
|
|
|
|
};
|
|
|
|
Name::new(s.into())
|
|
|
|
}
|
|
|
|
}
|