2019-11-24 08:34:36 -06:00
|
|
|
//! A desugared representation of paths like `crate::foo` or `<Type as Trait>::bar`.
|
2019-12-13 05:12:36 -06:00
|
|
|
mod lower;
|
2019-10-30 08:12:55 -05:00
|
|
|
|
|
|
|
use std::{iter, sync::Arc};
|
|
|
|
|
2019-10-30 10:56:20 -05:00
|
|
|
use hir_expand::{
|
2019-10-30 11:10:53 -05:00
|
|
|
hygiene::Hygiene,
|
2019-12-13 05:12:36 -06:00
|
|
|
name::{AsName, Name},
|
2019-10-30 10:56:20 -05:00
|
|
|
};
|
2019-10-30 08:12:55 -05:00
|
|
|
use ra_db::CrateId;
|
2019-12-13 05:12:36 -06:00
|
|
|
use ra_syntax::ast;
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-11-28 03:50:26 -06:00
|
|
|
use crate::{type_ref::TypeRef, InFile};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-12-13 05:12:36 -06:00
|
|
|
pub struct ModPath {
|
2019-10-30 08:12:55 -05:00
|
|
|
pub kind: PathKind,
|
2019-12-13 05:12:36 -06:00
|
|
|
pub segments: Vec<Name>,
|
|
|
|
}
|
|
|
|
|
2019-12-18 10:06:52 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum PathKind {
|
|
|
|
Plain,
|
|
|
|
/// `self::` is `Super(0)`
|
|
|
|
Super(u8),
|
|
|
|
Crate,
|
|
|
|
/// Absolute path (::foo)
|
|
|
|
Abs,
|
|
|
|
/// `$crate` from macro expansion
|
|
|
|
DollarCrate(CrateId),
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
impl ModPath {
|
|
|
|
pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
|
|
|
|
lower::lower_path(path, hygiene).map(|it| it.mod_path)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_simple_segments(
|
|
|
|
kind: PathKind,
|
|
|
|
segments: impl IntoIterator<Item = Name>,
|
|
|
|
) -> ModPath {
|
|
|
|
let segments = segments.into_iter().collect::<Vec<_>>();
|
|
|
|
ModPath { kind, segments }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> ModPath {
|
|
|
|
name_ref.as_name().into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts an `tt::Ident` into a single-identifier `Path`.
|
|
|
|
pub(crate) fn from_tt_ident(ident: &tt::Ident) -> ModPath {
|
|
|
|
ident.as_name().into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calls `cb` with all paths, represented by this use item.
|
|
|
|
pub(crate) fn expand_use_item(
|
|
|
|
item_src: InFile<ast::UseItem>,
|
|
|
|
hygiene: &Hygiene,
|
|
|
|
mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<Name>),
|
|
|
|
) {
|
|
|
|
if let Some(tree) = item_src.value.use_tree() {
|
|
|
|
lower::lower_use_tree(None, tree, hygiene, &mut cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_ident(&self) -> bool {
|
|
|
|
self.kind == PathKind::Plain && self.segments.len() == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_self(&self) -> bool {
|
2019-12-17 08:38:28 -06:00
|
|
|
self.kind == PathKind::Super(0) && self.segments.is_empty()
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If this path is a single identifier, like `foo`, return its name.
|
|
|
|
pub fn as_ident(&self) -> Option<&Name> {
|
|
|
|
if self.kind != PathKind::Plain || self.segments.len() > 1 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
self.segments.first()
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
2019-12-13 05:12:36 -06:00
|
|
|
pub struct Path {
|
2019-12-18 10:41:33 -06:00
|
|
|
/// Type based path like `<T>::foo`
|
|
|
|
type_anchor: Option<Box<TypeRef>>,
|
2019-12-13 05:12:36 -06:00
|
|
|
mod_path: ModPath,
|
|
|
|
/// Invariant: the same len as self.path.segments
|
|
|
|
generic_args: Vec<Option<Arc<GenericArgs>>>,
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic arguments to a path segment (e.g. the `i32` in `Option<i32>`). This
|
2019-12-12 10:17:57 -06:00
|
|
|
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
|
2019-10-30 08:12:55 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct GenericArgs {
|
|
|
|
pub args: Vec<GenericArg>,
|
|
|
|
/// This specifies whether the args contain a Self type as the first
|
|
|
|
/// element. This is the case for path segments like `<T as Trait>`, where
|
|
|
|
/// `T` is actually a type parameter for the path `Trait` specifying the
|
|
|
|
/// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
|
|
|
|
/// is left out.
|
|
|
|
pub has_self_type: bool,
|
|
|
|
/// Associated type bindings like in `Iterator<Item = T>`.
|
|
|
|
pub bindings: Vec<(Name, TypeRef)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single generic argument.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum GenericArg {
|
|
|
|
Type(TypeRef),
|
|
|
|
// or lifetime...
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Path {
|
|
|
|
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
|
|
|
/// DEPRECATED: It does not handle `$crate` from macro call.
|
|
|
|
pub fn from_ast(path: ast::Path) -> Option<Path> {
|
2019-12-13 05:12:36 -06:00
|
|
|
lower::lower_path(path, &Hygiene::new_unhygienic())
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
|
|
|
/// It correctly handles `$crate` based path from macro call.
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn from_src(path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
|
|
|
|
lower::lower_path(path, hygiene)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts an `ast::NameRef` into a single-identifier `Path`.
|
2019-11-24 11:06:55 -06:00
|
|
|
pub(crate) fn from_name_ref(name_ref: &ast::NameRef) -> Path {
|
2019-12-18 10:41:33 -06:00
|
|
|
Path { type_anchor: None, mod_path: name_ref.as_name().into(), generic_args: vec![None] }
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
/// `true` if this path is just a standalone `self`
|
|
|
|
pub fn is_self(&self) -> bool {
|
|
|
|
self.mod_path.is_self()
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn kind(&self) -> &PathKind {
|
|
|
|
&self.mod_path.kind
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-18 10:41:33 -06:00
|
|
|
pub fn type_anchor(&self) -> Option<&TypeRef> {
|
|
|
|
self.type_anchor.as_ref().map(|it| &**it)
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn segments(&self) -> PathSegments<'_> {
|
|
|
|
PathSegments {
|
|
|
|
segments: self.mod_path.segments.as_slice(),
|
|
|
|
generic_args: self.generic_args.as_slice(),
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn mod_path(&self) -> &ModPath {
|
|
|
|
&self.mod_path
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn qualifier(&self) -> Option<Path> {
|
|
|
|
if self.mod_path.is_ident() {
|
2019-10-30 08:12:55 -05:00
|
|
|
return None;
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
let res = Path {
|
2019-12-18 10:41:33 -06:00
|
|
|
type_anchor: self.type_anchor.clone(),
|
2019-12-13 05:12:36 -06:00
|
|
|
mod_path: ModPath {
|
|
|
|
kind: self.mod_path.kind.clone(),
|
|
|
|
segments: self.mod_path.segments[..self.mod_path.segments.len() - 1].to_vec(),
|
|
|
|
},
|
|
|
|
generic_args: self.generic_args[..self.generic_args.len() - 1].to_vec(),
|
|
|
|
};
|
|
|
|
Some(res)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct PathSegment<'a> {
|
|
|
|
pub name: &'a Name,
|
|
|
|
pub args_and_bindings: Option<&'a GenericArgs>,
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub struct PathSegments<'a> {
|
|
|
|
segments: &'a [Name],
|
|
|
|
generic_args: &'a [Option<Arc<GenericArgs>>],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PathSegments<'a> {
|
|
|
|
pub const EMPTY: PathSegments<'static> = PathSegments { segments: &[], generic_args: &[] };
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.segments.len()
|
|
|
|
}
|
|
|
|
pub fn first(&self) -> Option<PathSegment<'a>> {
|
|
|
|
self.get(0)
|
|
|
|
}
|
|
|
|
pub fn last(&self) -> Option<PathSegment<'a>> {
|
|
|
|
self.get(self.len().checked_sub(1)?)
|
|
|
|
}
|
|
|
|
pub fn get(&self, idx: usize) -> Option<PathSegment<'a>> {
|
|
|
|
assert_eq!(self.segments.len(), self.generic_args.len());
|
|
|
|
let res = PathSegment {
|
|
|
|
name: self.segments.get(idx)?,
|
|
|
|
args_and_bindings: self.generic_args.get(idx).unwrap().as_ref().map(|it| &**it),
|
|
|
|
};
|
|
|
|
Some(res)
|
|
|
|
}
|
|
|
|
pub fn skip(&self, len: usize) -> PathSegments<'a> {
|
|
|
|
assert_eq!(self.segments.len(), self.generic_args.len());
|
|
|
|
PathSegments { segments: &self.segments[len..], generic_args: &self.generic_args[len..] }
|
|
|
|
}
|
|
|
|
pub fn take(&self, len: usize) -> PathSegments<'a> {
|
|
|
|
assert_eq!(self.segments.len(), self.generic_args.len());
|
|
|
|
PathSegments { segments: &self.segments[..len], generic_args: &self.generic_args[..len] }
|
|
|
|
}
|
|
|
|
pub fn iter(&self) -> impl Iterator<Item = PathSegment<'a>> {
|
|
|
|
self.segments.iter().zip(self.generic_args.iter()).map(|(name, args)| PathSegment {
|
|
|
|
name,
|
|
|
|
args_and_bindings: args.as_ref().map(|it| &**it),
|
|
|
|
})
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GenericArgs {
|
2019-11-24 07:47:29 -06:00
|
|
|
pub(crate) fn from_ast(node: ast::TypeArgList) -> Option<GenericArgs> {
|
2019-12-13 05:12:36 -06:00
|
|
|
lower::lower_generic_args(node)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn empty() -> GenericArgs {
|
|
|
|
GenericArgs { args: Vec::new(), has_self_type: false, bindings: Vec::new() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Name> for Path {
|
|
|
|
fn from(name: Name) -> Path {
|
2019-12-13 05:12:36 -06:00
|
|
|
Path {
|
2019-12-18 10:41:33 -06:00
|
|
|
type_anchor: None,
|
2019-12-13 05:12:36 -06:00
|
|
|
mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)),
|
|
|
|
generic_args: vec![None],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Name> for ModPath {
|
|
|
|
fn from(name: Name) -> ModPath {
|
|
|
|
ModPath::from_simple_segments(PathKind::Plain, iter::once(name))
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 15:32:44 -06:00
|
|
|
pub use hir_expand::name as __name;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __known_path {
|
|
|
|
(std::iter::IntoIterator) => {};
|
|
|
|
(std::result::Result) => {};
|
|
|
|
(std::ops::Range) => {};
|
|
|
|
(std::ops::RangeFrom) => {};
|
|
|
|
(std::ops::RangeFull) => {};
|
|
|
|
(std::ops::RangeTo) => {};
|
|
|
|
(std::ops::RangeToInclusive) => {};
|
|
|
|
(std::ops::RangeInclusive) => {};
|
|
|
|
(std::boxed::Box) => {};
|
|
|
|
(std::future::Future) => {};
|
|
|
|
(std::ops::Try) => {};
|
|
|
|
(std::ops::Neg) => {};
|
|
|
|
(std::ops::Not) => {};
|
|
|
|
($path:path) => {
|
|
|
|
compile_error!("Please register your known path in the path module")
|
|
|
|
};
|
|
|
|
}
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2019-12-13 15:32:44 -06:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __path {
|
|
|
|
($start:ident $(:: $seg:ident)*) => ({
|
|
|
|
$crate::__known_path!($start $(:: $seg)*);
|
2019-12-13 05:12:36 -06:00
|
|
|
$crate::path::ModPath::from_simple_segments($crate::path::PathKind::Abs, vec![
|
2019-12-13 15:32:44 -06:00
|
|
|
$crate::path::__name![$start], $($crate::path::__name![$seg],)*
|
|
|
|
])
|
|
|
|
});
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
2019-12-13 15:32:44 -06:00
|
|
|
|
|
|
|
pub use crate::__path as path;
|