rust-analyzer/crates/hir-expand/src/name.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

430 lines
11 KiB
Rust
Raw Normal View History

2021-05-22 14:20:22 +00:00
//! See [`Name`].
2019-10-30 13:12:55 +00:00
use std::fmt;
use syntax::{ast, utils::is_raw_identifier, SmolStr};
2019-10-30 13:12:55 +00:00
/// `Name` is a wrapper around string, which is used in hir for both references
/// and declarations. In theory, names should also carry hygiene info, but we are
/// not there yet!
///
/// Note that `Name` holds and prints escaped name i.e. prefixed with "r#" when it
/// is a raw identifier. Use [`unescaped()`][Name::unescaped] when you need the
/// name without "r#".
2019-10-30 13:12:55 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Name(Repr);
/// Wrapper of `Name` to print the name without "r#" even when it is a raw identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UnescapedName<'a>(&'a Name);
2019-10-30 13:12:55 +00:00
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
enum Repr {
Text(SmolStr),
TupleField(usize),
}
impl fmt::Display for Name {
2022-07-20 13:02:08 +00:00
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2019-10-30 13:12:55 +00:00
match &self.0 {
Repr::Text(text) => fmt::Display::fmt(&text, f),
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
}
}
}
impl<'a> fmt::Display for UnescapedName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 .0 {
Repr::Text(text) => {
let text = text.strip_prefix("r#").unwrap_or(text);
fmt::Display::fmt(&text, f)
}
Repr::TupleField(idx) => fmt::Display::fmt(&idx, f),
}
}
}
impl<'a> UnescapedName<'a> {
/// Returns the textual representation of this name as a [`SmolStr`]. Prefer using this over
/// [`ToString::to_string`] if possible as this conversion is cheaper in the general case.
pub fn to_smol_str(&self) -> SmolStr {
match &self.0 .0 {
Repr::Text(it) => {
if let Some(stripped) = it.strip_prefix("r#") {
SmolStr::new(stripped)
} else {
it.clone()
}
}
2022-12-30 08:05:03 +00:00
Repr::TupleField(it) => SmolStr::new(it.to_string()),
}
}
}
2019-10-30 13:12:55 +00:00
impl Name {
/// 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.
const fn new_text(text: SmolStr) -> Name {
Name(Repr::Text(text))
}
pub fn new_tuple_field(idx: usize) -> Name {
Name(Repr::TupleField(idx))
}
2020-12-15 18:23:51 +00:00
pub fn new_lifetime(lt: &ast::Lifetime) -> Name {
2021-01-19 22:56:11 +00:00
Self::new_text(lt.text().into())
2020-05-31 08:59:40 +00:00
}
2023-03-19 09:32:51 +00:00
/// Shortcut to create inline plain text name. Panics if `text.len() > 22`
2020-09-23 06:45:35 +00:00
const fn new_inline(text: &str) -> Name {
Name::new_text(SmolStr::new_inline(text))
2019-10-30 13:12:55 +00:00
}
/// Resolve a name from the text of token.
2021-01-19 22:56:11 +00:00
fn resolve(raw_text: &str) -> Name {
match raw_text.strip_prefix("r#") {
// When `raw_text` starts with "r#" but the name does not coincide with any
// keyword, we never need the prefix so we strip it.
Some(text) if !is_raw_identifier(text) => Name::new_text(SmolStr::new(text)),
// Keywords (in the current edition) *can* be used as a name in earlier editions of
// Rust, e.g. "try" in Rust 2015. Even in such cases, we keep track of them in their
// escaped form.
None if is_raw_identifier(raw_text) => {
Name::new_text(SmolStr::from_iter(["r#", raw_text]))
}
_ => Name::new_text(raw_text.into()),
2019-10-30 13:12:55 +00:00
}
}
/// A fake name for things missing in the source code.
///
/// For example, `impl Foo for {}` should be treated as a trait impl for a
/// type with a missing name. Similarly, `struct S { : u32 }` should have a
/// single field with a missing name.
///
/// Ideally, we want a `gensym` semantics for missing names -- each missing
/// name is equal only to itself. It's not clear how to implement this in
/// salsa though, so we punt on that bit for a moment.
pub const fn missing() -> Name {
Name::new_inline("[missing name]")
2019-10-30 13:12:55 +00:00
}
2023-03-19 09:32:51 +00:00
/// Generates a new name which is only equal to itself, by incrementing a counter. Due
/// its implementation, it should not be used in things that salsa considers, like
/// type names or field names, and it should be only used in names of local variables
/// and labels and similar things.
pub fn generate_new_name() -> Name {
use std::sync::atomic::{AtomicUsize, Ordering};
static CNT: AtomicUsize = AtomicUsize::new(0);
let c = CNT.fetch_add(1, Ordering::Relaxed);
Name::new_text(format!("<ra@gennew>{c}").into())
2023-03-19 09:32:51 +00:00
}
/// Returns the tuple index this name represents if it is a tuple field.
2019-10-30 13:12:55 +00:00
pub fn as_tuple_index(&self) -> Option<usize> {
match self.0 {
Repr::TupleField(idx) => Some(idx),
_ => None,
}
}
/// Returns the text this name represents if it isn't a tuple field.
pub fn as_text(&self) -> Option<SmolStr> {
match &self.0 {
Repr::Text(it) => Some(it.clone()),
_ => None,
}
}
/// Returns the text this name represents if it isn't a tuple field.
pub fn as_str(&self) -> Option<&str> {
match &self.0 {
Repr::Text(it) => Some(it),
_ => None,
}
}
/// Returns the textual representation of this name as a [`SmolStr`].
/// Prefer using this over [`ToString::to_string`] if possible as this conversion is cheaper in
/// the general case.
pub fn to_smol_str(&self) -> SmolStr {
match &self.0 {
Repr::Text(it) => it.clone(),
2022-12-30 08:05:03 +00:00
Repr::TupleField(it) => SmolStr::new(it.to_string()),
}
}
pub fn unescaped(&self) -> UnescapedName<'_> {
UnescapedName(self)
}
pub fn is_escaped(&self) -> bool {
match &self.0 {
Repr::Text(it) => it.starts_with("r#"),
Repr::TupleField(_) => false,
}
}
2019-10-30 13:12:55 +00:00
}
pub trait AsName {
fn as_name(&self) -> Name;
}
impl AsName for ast::NameRef {
fn as_name(&self) -> Name {
match self.as_tuple_field() {
Some(idx) => Name::new_tuple_field(idx),
None => Name::resolve(&self.text()),
2019-10-30 13:12:55 +00:00
}
}
}
impl AsName for ast::Name {
fn as_name(&self) -> Name {
Name::resolve(&self.text())
2019-10-30 13:12:55 +00:00
}
}
impl AsName for ast::NameOrNameRef {
fn as_name(&self) -> Name {
match self {
ast::NameOrNameRef::Name(it) => it.as_name(),
ast::NameOrNameRef::NameRef(it) => it.as_name(),
}
}
}
2023-01-31 10:49:49 +00:00
impl<Span> AsName for tt::Ident<Span> {
fn as_name(&self) -> Name {
Name::resolve(&self.text)
}
}
2019-10-30 13:12:55 +00:00
impl AsName for ast::FieldKind {
fn as_name(&self) -> Name {
match self {
ast::FieldKind::Name(nr) => nr.as_name(),
2020-01-07 13:49:42 +00:00
ast::FieldKind::Index(idx) => {
let idx = idx.text().parse::<usize>().unwrap_or(0);
Name::new_tuple_field(idx)
}
2019-10-30 13:12:55 +00:00
}
}
}
2020-08-13 14:25:38 +00:00
impl AsName for base_db::Dependency {
2019-10-30 13:12:55 +00:00
fn as_name(&self) -> Name {
2020-07-01 07:53:53 +00:00
Name::new_text(SmolStr::new(&*self.name))
2019-10-30 13:12:55 +00:00
}
}
2019-12-13 20:43:53 +00:00
pub mod known {
macro_rules! known_names {
($($ident:ident),* $(,)?) => {
$(
#[allow(bad_style)]
pub const $ident: super::Name =
2020-09-23 06:45:35 +00:00
super::Name::new_inline(stringify!($ident));
2019-12-13 20:43:53 +00:00
)*
};
}
known_names!(
// Primitives
isize,
i8,
i16,
i32,
i64,
i128,
usize,
u8,
u16,
u32,
u64,
u128,
f32,
f64,
bool,
char,
str,
// Special names
macro_rules,
doc,
2021-03-10 18:43:03 +00:00
cfg,
2020-12-18 17:58:42 +00:00
cfg_attr,
register_attr,
register_tool,
2019-12-13 20:43:53 +00:00
// Components of known path (value or mod name)
std,
2020-01-27 22:09:56 +00:00
core,
alloc,
2019-12-13 20:43:53 +00:00
iter,
ops,
future,
result,
boxed,
option,
2021-06-01 11:39:19 +00:00
prelude,
rust_2015,
rust_2018,
rust_2021,
2021-06-01 17:03:00 +00:00
v1,
2019-12-13 20:43:53 +00:00
// Components of known path (type name)
Iterator,
2019-12-13 20:43:53 +00:00
IntoIterator,
Item,
IntoIter,
2019-12-13 20:43:53 +00:00
Try,
Ok,
Future,
2022-08-09 00:20:45 +00:00
IntoFuture,
2019-12-13 20:43:53 +00:00
Result,
Option,
2019-12-13 20:43:53 +00:00
Output,
Target,
Box,
RangeFrom,
RangeFull,
RangeInclusive,
RangeToInclusive,
RangeTo,
Range,
Neg,
Not,
2021-08-08 15:12:08 +00:00
None,
Index,
// Components of known path (function name)
filter_map,
next,
iter_mut,
len,
is_empty,
new,
2019-12-13 20:43:53 +00:00
// Builtin macros
asm,
assert,
2019-12-13 20:43:53 +00:00
column,
compile_error,
2021-05-13 22:42:10 +00:00
concat_idents,
2022-02-25 10:46:11 +00:00
concat_bytes,
concat,
const_format_args,
core_panic,
env,
file,
format_args_nl,
format_args,
global_asm,
2020-06-27 18:02:47 +00:00
include_bytes,
2020-06-27 12:31:19 +00:00
include_str,
include,
line,
llvm_asm,
log_syntax,
module_path,
option_env,
std_panic,
stringify,
trace_macros,
unreachable,
2019-12-13 20:43:53 +00:00
// Builtin derives
Copy,
Clone,
Default,
Debug,
Hash,
Ord,
PartialOrd,
Eq,
PartialEq,
// Builtin attributes
bench,
cfg_accessible,
cfg_eval,
crate_type,
derive,
global_allocator,
test,
test_case,
recursion_limit,
feature,
// known methods of lang items
call_once,
2023-03-11 18:13:53 +00:00
call_mut,
call,
eq,
ne,
ge,
gt,
le,
lt,
// lang items
add_assign,
add,
bitand_assign,
bitand,
bitor_assign,
bitor,
bitxor_assign,
bitxor,
branch,
deref_mut,
deref,
div_assign,
div,
2023-05-12 14:47:15 +00:00
drop,
fn_mut,
fn_once,
future_trait,
index,
index_mut,
2022-08-09 00:20:45 +00:00
into_future,
mul_assign,
mul,
neg,
not,
owned_box,
partial_ord,
poll,
r#fn,
rem_assign,
rem,
shl_assign,
shl,
shr_assign,
shr,
sub_assign,
sub,
2022-10-23 08:12:05 +00:00
unsafe_cell,
va_list
2019-12-13 20:43:53 +00:00
);
// self/Self cannot be used as an identifier
2020-09-23 06:45:35 +00:00
pub const SELF_PARAM: super::Name = super::Name::new_inline("self");
pub const SELF_TYPE: super::Name = super::Name::new_inline("Self");
2019-12-13 20:43:53 +00:00
2020-12-11 12:49:32 +00:00
pub const STATIC_LIFETIME: super::Name = super::Name::new_inline("'static");
2019-12-13 20:43:53 +00:00
#[macro_export]
2019-12-13 21:01:06 +00:00
macro_rules! name {
2019-12-13 20:43:53 +00:00
(self) => {
$crate::name::known::SELF_PARAM
};
(Self) => {
$crate::name::known::SELF_TYPE
};
2020-12-11 12:49:32 +00:00
('static) => {
$crate::name::known::STATIC_LIFETIME
};
2019-12-13 20:43:53 +00:00
($ident:ident) => {
$crate::name::known::$ident
};
}
}
2019-12-13 21:01:06 +00:00
pub use crate::name;