5 use syntax::{ast, SmolStr};
7 /// `Name` is a wrapper around string, which is used in hir for both references
8 /// and declarations. In theory, names should also carry hygiene info, but we are
10 #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
11 pub struct Name(Repr);
13 #[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
19 impl fmt::Display for Name {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22 Repr::Text(text) => fmt::Display::fmt(&text, f),
23 Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
29 /// Note: this is private to make creating name from random string hard.
30 /// Hopefully, this should allow us to integrate hygiene cleaner in the
31 /// future, and to switch to interned representation of names.
32 const fn new_text(text: SmolStr) -> Name {
33 Name(Repr::Text(text))
36 pub fn new_tuple_field(idx: usize) -> Name {
37 Name(Repr::TupleField(idx))
40 pub fn new_lifetime(lt: &ast::Lifetime) -> Name {
41 Self::new_text(lt.text().into())
44 /// Shortcut to create inline plain text name
45 const fn new_inline(text: &str) -> Name {
46 Name::new_text(SmolStr::new_inline(text))
49 /// Resolve a name from the text of token.
50 fn resolve(raw_text: &str) -> Name {
51 match raw_text.strip_prefix("r#") {
52 Some(text) => Name::new_text(SmolStr::new(text)),
53 None => Name::new_text(raw_text.into()),
57 /// A fake name for things missing in the source code.
59 /// For example, `impl Foo for {}` should be treated as a trait impl for a
60 /// type with a missing name. Similarly, `struct S { : u32 }` should have a
61 /// single field with a missing name.
63 /// Ideally, we want a `gensym` semantics for missing names -- each missing
64 /// name is equal only to itself. It's not clear how to implement this in
65 /// salsa though, so we punt on that bit for a moment.
66 pub const fn missing() -> Name {
67 Name::new_inline("[missing name]")
70 /// Returns the tuple index this name represents if it is a tuple field.
71 pub fn as_tuple_index(&self) -> Option<usize> {
73 Repr::TupleField(idx) => Some(idx),
78 /// Returns the text this name represents if it isn't a tuple field.
79 pub fn as_text(&self) -> Option<SmolStr> {
81 Repr::Text(it) => Some(it.clone()),
86 /// Returns the textual representation of this name as a [`SmolStr`].
87 /// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
89 pub fn to_smol_str(&self) -> SmolStr {
91 Repr::Text(it) => it.clone(),
92 Repr::TupleField(it) => SmolStr::new(&it.to_string()),
98 fn as_name(&self) -> Name;
101 impl AsName for ast::NameRef {
102 fn as_name(&self) -> Name {
103 match self.as_tuple_field() {
104 Some(idx) => Name::new_tuple_field(idx),
105 None => Name::resolve(&self.text()),
110 impl AsName for ast::Name {
111 fn as_name(&self) -> Name {
112 Name::resolve(&self.text())
116 impl AsName for ast::NameOrNameRef {
117 fn as_name(&self) -> Name {
119 ast::NameOrNameRef::Name(it) => it.as_name(),
120 ast::NameOrNameRef::NameRef(it) => it.as_name(),
125 impl AsName for tt::Ident {
126 fn as_name(&self) -> Name {
127 Name::resolve(&self.text)
131 impl AsName for ast::FieldKind {
132 fn as_name(&self) -> Name {
134 ast::FieldKind::Name(nr) => nr.as_name(),
135 ast::FieldKind::Index(idx) => {
136 let idx = idx.text().parse::<usize>().unwrap_or(0);
137 Name::new_tuple_field(idx)
143 impl AsName for base_db::Dependency {
144 fn as_name(&self) -> Name {
145 Name::new_text(SmolStr::new(&*self.name))
150 macro_rules! known_names {
151 ($($ident:ident),* $(,)?) => {
154 pub const $ident: super::Name =
155 super::Name::new_inline(stringify!($ident));
186 // Components of known path (value or mod name)
201 // Components of known path (type name)
223 // Components of known path (function name)
267 // Builtin attributes
315 // known methods of lang items
357 // self/Self cannot be used as an identifier
358 pub const SELF_PARAM: super::Name = super::Name::new_inline("self");
359 pub const SELF_TYPE: super::Name = super::Name::new_inline("Self");
361 pub const STATIC_LIFETIME: super::Name = super::Name::new_inline("'static");
366 $crate::name::known::SELF_PARAM
369 $crate::name::known::SELF_TYPE
372 $crate::name::known::STATIC_LIFETIME
375 $crate::name::known::$ident