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
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
use std::{
|
|
|
|
fmt::{self, Display},
|
|
|
|
iter,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2021-05-06 12:59:54 -05:00
|
|
|
use crate::{body::LowerCtx, db::DefDatabase, intern::Interned, type_ref::LifetimeRef};
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::CrateId;
|
2021-03-14 07:03:39 -05:00
|
|
|
use hir_expand::{
|
|
|
|
hygiene::Hygiene,
|
|
|
|
name::{name, Name},
|
|
|
|
};
|
2020-12-18 18:09:48 -06:00
|
|
|
use syntax::ast;
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2020-04-10 15:05:46 -05:00
|
|
|
use crate::{
|
|
|
|
type_ref::{TypeBound, TypeRef},
|
|
|
|
InFile,
|
|
|
|
};
|
2019-10-30 08:12:55 -05:00
|
|
|
|
2020-02-02 06:06:51 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2019-12-13 05:12:36 -06:00
|
|
|
pub struct ModPath {
|
2019-10-30 08:12:55 -05:00
|
|
|
pub kind: PathKind,
|
2021-02-04 13:49:24 -06:00
|
|
|
segments: Vec<Name>,
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
2020-02-02 06:06:51 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2019-12-18 10:06:52 -06:00
|
|
|
pub enum PathKind {
|
|
|
|
Plain,
|
|
|
|
/// `self::` is `Super(0)`
|
|
|
|
Super(u8),
|
|
|
|
Crate,
|
|
|
|
/// Absolute path (::foo)
|
|
|
|
Abs,
|
|
|
|
/// `$crate` from macro expansion
|
|
|
|
DollarCrate(CrateId),
|
|
|
|
}
|
|
|
|
|
2020-02-02 07:04:06 -06:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ImportAlias {
|
|
|
|
/// Unnamed alias, as in `use Foo as _;`
|
|
|
|
Underscore,
|
|
|
|
/// Named alias
|
|
|
|
Alias(Name),
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
impl ModPath {
|
2021-05-06 12:59:54 -05:00
|
|
|
pub fn from_src(db: &dyn DefDatabase, path: ast::Path, hygiene: &Hygiene) -> Option<ModPath> {
|
|
|
|
let ctx = LowerCtx::with_hygiene(db, hygiene);
|
|
|
|
lower::lower_path(db, path, &ctx).map(|it| (*it.mod_path).clone())
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
2020-01-15 10:44:12 -06:00
|
|
|
pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
|
2019-12-13 05:12:36 -06:00
|
|
|
let segments = segments.into_iter().collect::<Vec<_>>();
|
|
|
|
ModPath { kind, segments }
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:49:24 -06:00
|
|
|
/// Creates a `ModPath` from a `PathKind`, with no extra path segments.
|
|
|
|
pub const fn from_kind(kind: PathKind) -> ModPath {
|
|
|
|
ModPath { kind, segments: Vec::new() }
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
/// Calls `cb` with all paths, represented by this use item.
|
|
|
|
pub(crate) fn expand_use_item(
|
2021-05-06 12:59:54 -05:00
|
|
|
db: &dyn DefDatabase,
|
2020-07-30 07:12:04 -05:00
|
|
|
item_src: InFile<ast::Use>,
|
2019-12-13 05:12:36 -06:00
|
|
|
hygiene: &Hygiene,
|
2020-02-02 07:04:06 -06:00
|
|
|
mut cb: impl FnMut(ModPath, &ast::UseTree, /* is_glob */ bool, Option<ImportAlias>),
|
2019-12-13 05:12:36 -06:00
|
|
|
) {
|
|
|
|
if let Some(tree) = item_src.value.use_tree() {
|
2021-05-06 12:59:54 -05:00
|
|
|
lower::lower_use_tree(db, None, tree, hygiene, &mut cb);
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:49:24 -06:00
|
|
|
pub fn segments(&self) -> &[Name] {
|
|
|
|
&self.segments
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_segment(&mut self, segment: Name) {
|
|
|
|
self.segments.push(segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop_segment(&mut self) -> Option<Name> {
|
|
|
|
self.segments.pop()
|
|
|
|
}
|
|
|
|
|
2020-05-20 16:51:20 -05:00
|
|
|
/// Returns the number of segments in the path (counting special segments like `$crate` and
|
|
|
|
/// `super`).
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.segments.len()
|
|
|
|
+ match self.kind {
|
|
|
|
PathKind::Plain => 0,
|
|
|
|
PathKind::Super(i) => i as usize,
|
|
|
|
PathKind::Crate => 1,
|
|
|
|
PathKind::Abs => 0,
|
|
|
|
PathKind::DollarCrate(_) => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
pub fn is_ident(&self) -> bool {
|
2021-02-04 13:49:24 -06:00
|
|
|
self.as_ident().is_some()
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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> {
|
2021-02-04 13:49:24 -06:00
|
|
|
if self.kind != PathKind::Plain {
|
2019-12-13 05:12:36 -06:00
|
|
|
return None;
|
|
|
|
}
|
2021-02-04 13:49:24 -06:00
|
|
|
|
|
|
|
match &*self.segments {
|
|
|
|
[name] => Some(name),
|
|
|
|
_ => None,
|
|
|
|
}
|
2019-12-13 05:12:36 -06:00
|
|
|
}
|
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:42:49 -06:00
|
|
|
/// Type based path like `<T>::foo`.
|
|
|
|
/// Note that paths like `<Type as Trait>::foo` are desugard to `Trait::<Self=Type>::foo`.
|
2021-04-04 19:03:37 -05:00
|
|
|
type_anchor: Option<Interned<TypeRef>>,
|
2021-04-01 13:35:21 -05:00
|
|
|
mod_path: Interned<ModPath>,
|
2020-03-22 12:38:55 -05:00
|
|
|
/// Invariant: the same len as `self.mod_path.segments`
|
2019-12-13 05:12:36 -06:00
|
|
|
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>`.
|
2020-04-10 15:05:46 -05:00
|
|
|
pub bindings: Vec<AssociatedTypeBinding>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An associated type binding like in `Iterator<Item = T>`.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub struct AssociatedTypeBinding {
|
|
|
|
/// The name of the associated type.
|
|
|
|
pub name: Name,
|
|
|
|
/// The type bound to this associated type (in `Item = T`, this would be the
|
|
|
|
/// `T`). This can be `None` if there are bounds instead.
|
|
|
|
pub type_ref: Option<TypeRef>,
|
|
|
|
/// Bounds for the associated type, like in `Iterator<Item:
|
|
|
|
/// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
|
|
|
|
/// feature.)
|
|
|
|
pub bounds: Vec<TypeBound>,
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A single generic argument.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
pub enum GenericArg {
|
|
|
|
Type(TypeRef),
|
2020-12-11 06:49:32 -06:00
|
|
|
Lifetime(LifetimeRef),
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Path {
|
|
|
|
/// Converts an `ast::Path` to `Path`. Works with use trees.
|
|
|
|
/// It correctly handles `$crate` based path from macro call.
|
2021-05-06 12:59:54 -05:00
|
|
|
pub fn from_src(db: &dyn DefDatabase, path: ast::Path, ctx: &LowerCtx) -> Option<Path> {
|
|
|
|
lower::lower_path(db, path, ctx)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
|
2019-12-24 05:45:28 -06:00
|
|
|
/// Converts a known mod path to `Path`.
|
2019-12-24 05:52:48 -06:00
|
|
|
pub(crate) fn from_known_path(
|
|
|
|
path: ModPath,
|
|
|
|
generic_args: Vec<Option<Arc<GenericArgs>>>,
|
|
|
|
) -> Path {
|
2021-04-01 13:35:21 -05:00
|
|
|
Path { type_anchor: None, mod_path: Interned::new(path), generic_args }
|
2019-12-24 05:45:28 -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> {
|
2019-12-20 13:15:54 -06:00
|
|
|
self.type_anchor.as_deref()
|
2019-12-18 10:41:33 -06:00
|
|
|
}
|
|
|
|
|
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(),
|
2021-04-01 13:35:21 -05:00
|
|
|
mod_path: Interned::new(ModPath::from_segments(
|
2021-02-04 15:42:21 -06:00
|
|
|
self.mod_path.kind.clone(),
|
|
|
|
self.mod_path.segments[..self.mod_path.segments.len() - 1].iter().cloned(),
|
2021-04-01 13:35:21 -05:00
|
|
|
)),
|
2019-12-13 05:12:36 -06:00
|
|
|
generic_args: self.generic_args[..self.generic_args.len() - 1].to_vec(),
|
|
|
|
};
|
|
|
|
Some(res)
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
2021-03-14 07:03:39 -05:00
|
|
|
|
|
|
|
pub fn is_self_type(&self) -> bool {
|
|
|
|
self.type_anchor.is_none()
|
|
|
|
&& self.generic_args == &[None]
|
|
|
|
&& self.mod_path.as_ident() == Some(&name!(Self))
|
|
|
|
}
|
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 {
|
2020-07-31 11:29:29 -05:00
|
|
|
pub(crate) fn from_ast(lower_ctx: &LowerCtx, node: ast::GenericArgList) -> Option<GenericArgs> {
|
2020-04-30 05:20:13 -05:00
|
|
|
lower::lower_generic_args(lower_ctx, 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,
|
2021-04-01 13:35:21 -05:00
|
|
|
mod_path: Interned::new(ModPath::from_segments(PathKind::Plain, iter::once(name))),
|
2019-12-13 05:12:36 -06:00
|
|
|
generic_args: vec![None],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-30 16:55:18 -05:00
|
|
|
impl From<Name> for Box<Path> {
|
|
|
|
fn from(name: Name) -> Box<Path> {
|
|
|
|
Box::new(Path::from(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 05:12:36 -06:00
|
|
|
impl From<Name> for ModPath {
|
|
|
|
fn from(name: Name) -> ModPath {
|
2020-01-15 10:44:12 -06:00
|
|
|
ModPath::from_segments(PathKind::Plain, iter::once(name))
|
2019-10-30 08:12:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 09:17:08 -06:00
|
|
|
impl Display for ModPath {
|
2020-01-10 11:40:45 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-12-31 09:17:08 -06:00
|
|
|
let mut first_segment = true;
|
2020-06-03 21:05:55 -05:00
|
|
|
let mut add_segment = |s| -> fmt::Result {
|
2019-12-31 09:17:08 -06:00
|
|
|
if !first_segment {
|
|
|
|
f.write_str("::")?;
|
|
|
|
}
|
|
|
|
first_segment = false;
|
|
|
|
f.write_str(s)?;
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
match self.kind {
|
|
|
|
PathKind::Plain => {}
|
2020-09-16 14:29:48 -05:00
|
|
|
PathKind::Super(0) => add_segment("self")?,
|
2019-12-31 09:17:08 -06:00
|
|
|
PathKind::Super(n) => {
|
|
|
|
for _ in 0..n {
|
|
|
|
add_segment("super")?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PathKind::Crate => add_segment("crate")?,
|
|
|
|
PathKind::Abs => add_segment("")?,
|
|
|
|
PathKind::DollarCrate(_) => add_segment("$crate")?,
|
|
|
|
}
|
|
|
|
for segment in &self.segments {
|
|
|
|
if !first_segment {
|
|
|
|
f.write_str("::")?;
|
|
|
|
}
|
|
|
|
first_segment = false;
|
|
|
|
write!(f, "{}", segment)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 15:32:44 -06:00
|
|
|
pub use hir_expand::name as __name;
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! __known_path {
|
2020-06-11 09:22:31 -05:00
|
|
|
(core::iter::IntoIterator) => {};
|
2021-01-01 15:11:08 -06:00
|
|
|
(core::iter::Iterator) => {};
|
2020-06-11 09:22:31 -05:00
|
|
|
(core::result::Result) => {};
|
2020-12-30 11:23:00 -06:00
|
|
|
(core::option::Option) => {};
|
2020-06-11 09:22:31 -05:00
|
|
|
(core::ops::Range) => {};
|
|
|
|
(core::ops::RangeFrom) => {};
|
|
|
|
(core::ops::RangeFull) => {};
|
|
|
|
(core::ops::RangeTo) => {};
|
|
|
|
(core::ops::RangeToInclusive) => {};
|
|
|
|
(core::ops::RangeInclusive) => {};
|
|
|
|
(core::future::Future) => {};
|
|
|
|
(core::ops::Try) => {};
|
2019-12-13 15:32:44 -06:00
|
|
|
($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)*);
|
2020-01-15 10:44:12 -06:00
|
|
|
$crate::path::ModPath::from_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;
|