2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::hir::def_id::DefId;
|
|
|
|
|
use crate::util::nodemap::{NodeMap, DefIdMap};
|
2014-05-14 15:31:30 -04:00
|
|
|
|
use syntax::ast;
|
2017-02-23 20:12:33 +10:30
|
|
|
|
use syntax::ext::base::MacroKind;
|
2019-04-03 09:07:45 +02:00
|
|
|
|
use syntax::ast::NodeId;
|
2017-03-14 05:16:54 +00:00
|
|
|
|
use syntax_pos::Span;
|
2018-12-03 01:14:35 +01:00
|
|
|
|
use rustc_macros::HashStable;
|
2019-02-05 11:20:45 -06:00
|
|
|
|
use crate::hir;
|
|
|
|
|
use crate::ty;
|
2019-04-03 09:07:45 +02:00
|
|
|
|
use std::fmt::Debug;
|
2014-05-14 15:31:30 -04:00
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
use self::Namespace::*;
|
|
|
|
|
|
2019-03-24 18:54:56 +01:00
|
|
|
|
/// Encodes if a `Def::Ctor` is the constructor of an enum variant or a struct.
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
|
|
|
|
|
pub enum CtorOf {
|
|
|
|
|
/// This `Def::Ctor` is a synthesized constructor of a tuple or unit struct.
|
|
|
|
|
Struct,
|
|
|
|
|
/// This `Def::Ctor` is a synthesized constructor of a tuple or unit variant.
|
|
|
|
|
Variant,
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
|
2016-09-15 00:51:46 +03:00
|
|
|
|
pub enum CtorKind {
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Constructor function automatically created by a tuple struct/variant.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Fn,
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Constructor constant automatically created by a unit struct/variant.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Const,
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Unusable name in value namespace created by a struct variant.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Fictive,
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
|
2018-08-03 02:05:00 +03:00
|
|
|
|
pub enum NonMacroAttrKind {
|
|
|
|
|
/// Single-segment attribute defined by the language (`#[inline]`)
|
|
|
|
|
Builtin,
|
|
|
|
|
/// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
|
|
|
|
|
Tool,
|
|
|
|
|
/// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
|
|
|
|
|
DeriveHelper,
|
2018-11-12 13:05:20 -05:00
|
|
|
|
/// Single-segment custom attribute registered by a legacy plugin (`register_attribute`).
|
2018-09-15 23:46:54 +03:00
|
|
|
|
LegacyPluginHelper,
|
2018-08-03 02:05:00 +03:00
|
|
|
|
/// Single-segment custom attribute not registered in any way (`#[my_attr]`).
|
|
|
|
|
Custom,
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub enum Def<Id = hir::HirId> {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
// Type namespace
|
2016-01-20 22:31:10 +03:00
|
|
|
|
Mod(DefId),
|
2019-03-24 15:29:57 +03:00
|
|
|
|
/// `DefId` refers to the struct itself, `Def::Ctor` refers to its constructor if it exists.
|
2019-03-21 23:38:50 +01:00
|
|
|
|
Struct(DefId),
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Union(DefId),
|
2016-01-20 22:31:10 +03:00
|
|
|
|
Enum(DefId),
|
2019-03-24 15:29:57 +03:00
|
|
|
|
/// `DefId` refers to the variant itself, `Def::Ctor` refers to its constructor if it exists.
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Variant(DefId),
|
|
|
|
|
Trait(DefId),
|
2018-07-03 19:38:14 +02:00
|
|
|
|
/// `existential type Foo: Bar;`
|
2018-05-22 14:31:56 +02:00
|
|
|
|
Existential(DefId),
|
2018-07-03 19:38:14 +02:00
|
|
|
|
/// `type Foo = Bar;`
|
2016-01-20 22:31:10 +03:00
|
|
|
|
TyAlias(DefId),
|
2018-08-22 11:47:31 +01:00
|
|
|
|
ForeignTy(DefId),
|
2017-10-02 12:28:16 +00:00
|
|
|
|
TraitAlias(DefId),
|
2016-09-08 19:05:50 +03:00
|
|
|
|
AssociatedTy(DefId),
|
2018-07-03 19:38:14 +02:00
|
|
|
|
/// `existential type Foo: Bar;`
|
|
|
|
|
AssociatedExistential(DefId),
|
2016-01-20 22:31:10 +03:00
|
|
|
|
PrimTy(hir::PrimTy),
|
2018-08-22 02:13:31 +01:00
|
|
|
|
TyParam(DefId),
|
2016-09-15 00:51:46 +03:00
|
|
|
|
SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
|
2018-11-27 02:59:49 +00:00
|
|
|
|
ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
|
2016-09-15 00:51:46 +03:00
|
|
|
|
|
|
|
|
|
// Value namespace
|
|
|
|
|
Fn(DefId),
|
|
|
|
|
Const(DefId),
|
2019-02-15 22:26:05 +00:00
|
|
|
|
ConstParam(DefId),
|
2019-04-19 23:32:26 +03:00
|
|
|
|
Static(DefId),
|
2019-03-24 15:29:57 +03:00
|
|
|
|
/// `DefId` refers to the struct or enum variant's constructor.
|
2019-03-24 19:16:44 +01:00
|
|
|
|
Ctor(DefId, CtorOf, CtorKind),
|
2018-11-27 02:59:49 +00:00
|
|
|
|
SelfCtor(DefId /* impl */), // `DefId` refers to the impl
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Method(DefId),
|
|
|
|
|
AssociatedConst(DefId),
|
2017-04-29 14:39:47 +03:00
|
|
|
|
|
2019-04-03 09:07:45 +02:00
|
|
|
|
Local(Id),
|
|
|
|
|
Upvar(Id, // `HirId` of closed over local
|
2018-11-27 02:59:49 +00:00
|
|
|
|
usize, // index in the `freevars` list of the closure
|
2016-09-15 00:51:46 +03:00
|
|
|
|
ast::NodeId), // expr node that creates the closure
|
2016-01-20 22:31:10 +03:00
|
|
|
|
Label(ast::NodeId),
|
2016-09-15 00:51:46 +03:00
|
|
|
|
|
2016-10-25 22:05:02 +00:00
|
|
|
|
// Macro namespace
|
2017-02-23 20:12:33 +10:30
|
|
|
|
Macro(DefId, MacroKind),
|
2018-11-27 02:59:49 +00:00
|
|
|
|
NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
|
2016-10-25 22:05:02 +00:00
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
|
// Both namespaces
|
2016-01-20 22:31:10 +03:00
|
|
|
|
Err,
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-18 22:11:42 +03:00
|
|
|
|
/// The result of resolving a path before lowering to HIR.
|
|
|
|
|
/// `base_def` is definition of resolved part of the
|
|
|
|
|
/// path, `unresolved_segments` is the number of unresolved
|
|
|
|
|
/// segments.
|
2015-02-05 13:20:48 +02:00
|
|
|
|
///
|
2017-12-31 17:17:01 +01:00
|
|
|
|
/// ```text
|
|
|
|
|
/// module::Type::AssocX::AssocY::MethodOrAssocType
|
|
|
|
|
/// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
/// base_def unresolved_segments = 3
|
|
|
|
|
///
|
|
|
|
|
/// <T as Trait>::AssocX::AssocY::MethodOrAssocType
|
|
|
|
|
/// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
/// base_def unresolved_segments = 2
|
|
|
|
|
/// ```
|
2015-03-30 12:21:20 +13:00
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
2015-02-17 06:44:23 +02:00
|
|
|
|
pub struct PathResolution {
|
2019-04-03 09:07:45 +02:00
|
|
|
|
base_def: Def<NodeId>,
|
2017-02-18 22:11:42 +03:00
|
|
|
|
unresolved_segments: usize,
|
2015-02-17 06:44:23 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PathResolution {
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub fn new(def: Def<NodeId>) -> Self {
|
2017-02-18 22:11:42 +03:00
|
|
|
|
PathResolution { base_def: def, unresolved_segments: 0 }
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub fn with_unresolved_segments(def: Def<NodeId>, mut unresolved_segments: usize) -> Self {
|
2017-02-18 22:11:42 +03:00
|
|
|
|
if def == Def::Err { unresolved_segments = 0 }
|
|
|
|
|
PathResolution { base_def: def, unresolved_segments: unresolved_segments }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub fn base_def(&self) -> Def<NodeId> {
|
2017-02-18 22:11:42 +03:00
|
|
|
|
self.base_def
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn unresolved_segments(&self) -> usize {
|
|
|
|
|
self.unresolved_segments
|
2016-06-03 23:15:00 +03:00
|
|
|
|
}
|
2015-02-05 13:20:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
/// Different kinds of symbols don't influence each other.
|
|
|
|
|
///
|
|
|
|
|
/// Therefore, they have a separate universe (namespace).
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
|
|
|
pub enum Namespace {
|
|
|
|
|
TypeNS,
|
|
|
|
|
ValueNS,
|
|
|
|
|
MacroNS,
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-07 23:07:06 +03:00
|
|
|
|
impl Namespace {
|
|
|
|
|
pub fn descr(self) -> &'static str {
|
|
|
|
|
match self {
|
|
|
|
|
TypeNS => "type",
|
|
|
|
|
ValueNS => "value",
|
|
|
|
|
MacroNS => "macro",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
/// Just a helper ‒ separate structure for each namespace.
|
|
|
|
|
#[derive(Copy, Clone, Default, Debug)]
|
|
|
|
|
pub struct PerNS<T> {
|
|
|
|
|
pub value_ns: T,
|
|
|
|
|
pub type_ns: T,
|
|
|
|
|
pub macro_ns: T,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PerNS<T> {
|
|
|
|
|
pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
|
|
|
|
|
PerNS {
|
|
|
|
|
value_ns: f(self.value_ns),
|
|
|
|
|
type_ns: f(self.type_ns),
|
|
|
|
|
macro_ns: f(self.macro_ns),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
|
|
|
|
|
type Output = T;
|
2018-11-27 02:59:49 +00:00
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
fn index(&self, ns: Namespace) -> &T {
|
|
|
|
|
match ns {
|
|
|
|
|
ValueNS => &self.value_ns,
|
|
|
|
|
TypeNS => &self.type_ns,
|
|
|
|
|
MacroNS => &self.macro_ns,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
|
|
|
|
|
fn index_mut(&mut self, ns: Namespace) -> &mut T {
|
|
|
|
|
match ns {
|
|
|
|
|
ValueNS => &mut self.value_ns,
|
|
|
|
|
TypeNS => &mut self.type_ns,
|
|
|
|
|
MacroNS => &mut self.macro_ns,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> PerNS<Option<T>> {
|
2019-02-08 14:53:55 +01:00
|
|
|
|
/// Returns `true` if all the items in this collection are `None`.
|
2018-06-13 11:44:06 -05:00
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
|
self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns an iterator over the items which are `Some`.
|
|
|
|
|
pub fn present_items(self) -> impl Iterator<Item=T> {
|
|
|
|
|
use std::iter::once;
|
|
|
|
|
|
|
|
|
|
once(self.type_ns)
|
|
|
|
|
.chain(once(self.value_ns))
|
|
|
|
|
.chain(once(self.macro_ns))
|
|
|
|
|
.filter_map(|it| it)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// Definition mapping
|
2015-11-04 00:02:22 -06:00
|
|
|
|
pub type DefMap = NodeMap<PathResolution>;
|
2017-06-03 18:18:32 +02:00
|
|
|
|
|
|
|
|
|
/// This is the replacement export map. It maps a module to all of the exports
|
|
|
|
|
/// within.
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;
|
2014-12-19 00:03:00 +02:00
|
|
|
|
|
2018-06-13 11:44:06 -05:00
|
|
|
|
/// Map used to track the `use` statements within a scope, matching it with all the items in every
|
|
|
|
|
/// namespace.
|
|
|
|
|
pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;
|
|
|
|
|
|
2018-12-03 01:14:35 +01:00
|
|
|
|
#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub struct Export<Id> {
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// The name of the target.
|
|
|
|
|
pub ident: ast::Ident,
|
|
|
|
|
/// The definition of the target.
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub def: Def<Id>,
|
2017-06-03 18:18:32 +02:00
|
|
|
|
/// The span of the target definition.
|
|
|
|
|
pub span: Span,
|
2017-11-29 11:20:49 -08:00
|
|
|
|
/// The visibility of the export.
|
|
|
|
|
/// We include non-`pub` exports for hygienic macros that get used from extern crates.
|
|
|
|
|
pub vis: ty::Visibility,
|
2014-12-19 00:03:00 +02:00
|
|
|
|
}
|
2014-12-18 21:03:56 +02:00
|
|
|
|
|
2019-04-03 09:07:45 +02:00
|
|
|
|
impl<Id> Export<Id> {
|
|
|
|
|
pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
|
|
|
|
|
Export {
|
|
|
|
|
ident: self.ident,
|
|
|
|
|
def: self.def.map_id(map),
|
|
|
|
|
span: self.span,
|
|
|
|
|
vis: self.vis,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
|
impl CtorKind {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
|
2016-09-15 00:51:46 +03:00
|
|
|
|
match *vdata {
|
|
|
|
|
ast::VariantData::Tuple(..) => CtorKind::Fn,
|
|
|
|
|
ast::VariantData::Unit(..) => CtorKind::Const,
|
|
|
|
|
ast::VariantData::Struct(..) => CtorKind::Fictive,
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-27 02:59:49 +00:00
|
|
|
|
|
2016-09-15 00:51:46 +03:00
|
|
|
|
pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
|
|
|
|
|
match *vdata {
|
|
|
|
|
hir::VariantData::Tuple(..) => CtorKind::Fn,
|
|
|
|
|
hir::VariantData::Unit(..) => CtorKind::Const,
|
|
|
|
|
hir::VariantData::Struct(..) => CtorKind::Fictive,
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-15 00:51:46 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 02:05:00 +03:00
|
|
|
|
impl NonMacroAttrKind {
|
2018-12-12 04:11:46 +03:00
|
|
|
|
pub fn descr(self) -> &'static str {
|
2018-08-03 02:05:00 +03:00
|
|
|
|
match self {
|
|
|
|
|
NonMacroAttrKind::Builtin => "built-in attribute",
|
|
|
|
|
NonMacroAttrKind::Tool => "tool attribute",
|
|
|
|
|
NonMacroAttrKind::DeriveHelper => "derive helper attribute",
|
2018-09-15 23:46:54 +03:00
|
|
|
|
NonMacroAttrKind::LegacyPluginHelper => "legacy plugin helper attribute",
|
2018-08-03 02:05:00 +03:00
|
|
|
|
NonMacroAttrKind::Custom => "custom attribute",
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 09:07:45 +02:00
|
|
|
|
impl<Id> Def<Id> {
|
2019-01-26 20:30:52 +01:00
|
|
|
|
/// Return the `DefId` of this `Def` if it has an id, else panic.
|
2019-04-03 09:07:45 +02:00
|
|
|
|
pub fn def_id(&self) -> DefId
|
|
|
|
|
where
|
|
|
|
|
Id: Debug,
|
|
|
|
|
{
|
2018-11-11 20:28:56 +03:00
|
|
|
|
self.opt_def_id().unwrap_or_else(|| {
|
|
|
|
|
bug!("attempted .def_id() on invalid def: {:?}", self)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:30:52 +01:00
|
|
|
|
/// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
|
2018-11-11 20:28:56 +03:00
|
|
|
|
pub fn opt_def_id(&self) -> Option<DefId> {
|
2014-05-14 15:31:30 -04:00
|
|
|
|
match *self {
|
2019-04-19 23:32:26 +03:00
|
|
|
|
Def::Fn(id) | Def::Mod(id) | Def::Static(id) |
|
2019-03-24 19:16:44 +01:00
|
|
|
|
Def::Variant(id) | Def::Ctor(id, ..) | Def::Enum(id) |
|
2017-10-02 12:28:16 +00:00
|
|
|
|
Def::TyAlias(id) | Def::TraitAlias(id) |
|
2019-02-05 16:51:49 +01:00
|
|
|
|
Def::AssociatedTy(id) | Def::TyParam(id) | Def::ConstParam(id) | Def::Struct(id) |
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
|
2017-04-29 14:39:47 +03:00
|
|
|
|
Def::AssociatedConst(id) | Def::Macro(id, ..) |
|
2018-11-11 20:28:56 +03:00
|
|
|
|
Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) => {
|
|
|
|
|
Some(id)
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|
2015-09-02 16:11:32 -04:00
|
|
|
|
|
2017-04-29 14:39:47 +03:00
|
|
|
|
Def::Local(..) |
|
|
|
|
|
Def::Upvar(..) |
|
2016-01-20 22:31:10 +03:00
|
|
|
|
Def::Label(..) |
|
|
|
|
|
Def::PrimTy(..) |
|
|
|
|
|
Def::SelfTy(..) |
|
2018-11-11 20:28:56 +03:00
|
|
|
|
Def::SelfCtor(..) |
|
2018-07-23 02:52:51 +03:00
|
|
|
|
Def::ToolMod |
|
2018-08-03 02:05:00 +03:00
|
|
|
|
Def::NonMacroAttr(..) |
|
2016-01-20 22:31:10 +03:00
|
|
|
|
Def::Err => {
|
2018-11-11 20:28:56 +03:00
|
|
|
|
None
|
2015-09-07 14:27:13 -04:00
|
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-04 18:15:19 +02:00
|
|
|
|
|
2019-01-26 20:30:52 +01:00
|
|
|
|
/// Return the `DefId` of this `Def` if it represents a module.
|
|
|
|
|
pub fn mod_def_id(&self) -> Option<DefId> {
|
|
|
|
|
match *self {
|
|
|
|
|
Def::Mod(id) => Some(id),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 14:11:00 -05:00
|
|
|
|
/// A human readable name for the def kind ("function", "module", etc.).
|
2016-02-25 01:55:54 +00:00
|
|
|
|
pub fn kind_name(&self) -> &'static str {
|
|
|
|
|
match *self {
|
|
|
|
|
Def::Fn(..) => "function",
|
|
|
|
|
Def::Mod(..) => "module",
|
|
|
|
|
Def::Static(..) => "static",
|
|
|
|
|
Def::Enum(..) => "enum",
|
2019-03-21 23:38:50 +01:00
|
|
|
|
Def::Variant(..) => "variant",
|
2019-03-24 19:16:44 +01:00
|
|
|
|
Def::Ctor(_, CtorOf::Variant, CtorKind::Fn) => "tuple variant",
|
|
|
|
|
Def::Ctor(_, CtorOf::Variant, CtorKind::Const) => "unit variant",
|
|
|
|
|
Def::Ctor(_, CtorOf::Variant, CtorKind::Fictive) => "struct variant",
|
2019-03-21 23:38:50 +01:00
|
|
|
|
Def::Struct(..) => "struct",
|
2019-03-24 19:16:44 +01:00
|
|
|
|
Def::Ctor(_, CtorOf::Struct, CtorKind::Fn) => "tuple struct",
|
|
|
|
|
Def::Ctor(_, CtorOf::Struct, CtorKind::Const) => "unit struct",
|
|
|
|
|
Def::Ctor(_, CtorOf::Struct, CtorKind::Fictive) =>
|
2019-03-21 23:38:50 +01:00
|
|
|
|
bug!("impossible struct constructor"),
|
2018-05-22 14:31:56 +02:00
|
|
|
|
Def::Existential(..) => "existential type",
|
2016-09-15 00:51:46 +03:00
|
|
|
|
Def::TyAlias(..) => "type alias",
|
2017-10-02 12:28:16 +00:00
|
|
|
|
Def::TraitAlias(..) => "trait alias",
|
2016-02-25 01:55:54 +00:00
|
|
|
|
Def::AssociatedTy(..) => "associated type",
|
2018-07-03 19:38:14 +02:00
|
|
|
|
Def::AssociatedExistential(..) => "associated existential type",
|
2018-09-06 10:46:55 +08:00
|
|
|
|
Def::SelfCtor(..) => "self constructor",
|
2016-08-06 21:56:02 +03:00
|
|
|
|
Def::Union(..) => "union",
|
2016-02-25 01:55:54 +00:00
|
|
|
|
Def::Trait(..) => "trait",
|
2018-08-22 11:47:31 +01:00
|
|
|
|
Def::ForeignTy(..) => "foreign type",
|
2016-02-25 01:55:54 +00:00
|
|
|
|
Def::Method(..) => "method",
|
2016-06-03 23:15:00 +03:00
|
|
|
|
Def::Const(..) => "constant",
|
|
|
|
|
Def::AssociatedConst(..) => "associated constant",
|
2018-08-22 02:13:31 +01:00
|
|
|
|
Def::TyParam(..) => "type parameter",
|
2019-02-05 16:51:49 +01:00
|
|
|
|
Def::ConstParam(..) => "const parameter",
|
2016-02-25 01:55:54 +00:00
|
|
|
|
Def::PrimTy(..) => "builtin type",
|
|
|
|
|
Def::Local(..) => "local variable",
|
|
|
|
|
Def::Upvar(..) => "closure capture",
|
|
|
|
|
Def::Label(..) => "label",
|
|
|
|
|
Def::SelfTy(..) => "self type",
|
2018-05-28 22:13:59 +03:00
|
|
|
|
Def::Macro(.., macro_kind) => macro_kind.descr(),
|
2018-07-23 02:52:51 +03:00
|
|
|
|
Def::ToolMod => "tool module",
|
2018-08-03 02:05:00 +03:00
|
|
|
|
Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
|
2016-02-25 01:55:54 +00:00
|
|
|
|
Def::Err => "unresolved item",
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-05 01:11:59 +03:00
|
|
|
|
|
2019-01-09 14:11:00 -05:00
|
|
|
|
/// An English article for the def.
|
2018-11-05 01:11:59 +03:00
|
|
|
|
pub fn article(&self) -> &'static str {
|
|
|
|
|
match *self {
|
|
|
|
|
Def::AssociatedTy(..) | Def::AssociatedConst(..) | Def::AssociatedExistential(..) |
|
|
|
|
|
Def::Enum(..) | Def::Existential(..) | Def::Err => "an",
|
2018-11-10 18:58:37 +03:00
|
|
|
|
Def::Macro(.., macro_kind) => macro_kind.article(),
|
2018-11-05 01:11:59 +03:00
|
|
|
|
_ => "a",
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-03 09:07:45 +02:00
|
|
|
|
|
|
|
|
|
pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Def<R> {
|
|
|
|
|
match self {
|
|
|
|
|
Def::Fn(id) => Def::Fn(id),
|
|
|
|
|
Def::Mod(id) => Def::Mod(id),
|
2019-04-19 23:32:26 +03:00
|
|
|
|
Def::Static(id) => Def::Static(id),
|
2019-04-03 09:07:45 +02:00
|
|
|
|
Def::Enum(id) => Def::Enum(id),
|
|
|
|
|
Def::Variant(id) => Def::Variant(id),
|
|
|
|
|
Def::Ctor(a, b, c) => Def::Ctor(a, b, c),
|
|
|
|
|
Def::Struct(id) => Def::Struct(id),
|
|
|
|
|
Def::Existential(id) => Def::Existential(id),
|
|
|
|
|
Def::TyAlias(id) => Def::TyAlias(id),
|
|
|
|
|
Def::TraitAlias(id) => Def::TraitAlias(id),
|
|
|
|
|
Def::AssociatedTy(id) => Def::AssociatedTy(id),
|
|
|
|
|
Def::AssociatedExistential(id) => Def::AssociatedExistential(id),
|
|
|
|
|
Def::SelfCtor(id) => Def::SelfCtor(id),
|
|
|
|
|
Def::Union(id) => Def::Union(id),
|
|
|
|
|
Def::Trait(id) => Def::Trait(id),
|
|
|
|
|
Def::ForeignTy(id) => Def::ForeignTy(id),
|
|
|
|
|
Def::Method(id) => Def::Method(id),
|
|
|
|
|
Def::Const(id) => Def::Const(id),
|
|
|
|
|
Def::AssociatedConst(id) => Def::AssociatedConst(id),
|
|
|
|
|
Def::TyParam(id) => Def::TyParam(id),
|
|
|
|
|
Def::ConstParam(id) => Def::ConstParam(id),
|
|
|
|
|
Def::PrimTy(id) => Def::PrimTy(id),
|
|
|
|
|
Def::Local(id) => Def::Local(map(id)),
|
|
|
|
|
Def::Upvar(id, index, closure) => Def::Upvar(
|
|
|
|
|
map(id),
|
|
|
|
|
index,
|
|
|
|
|
closure
|
|
|
|
|
),
|
|
|
|
|
Def::Label(id) => Def::Label(id),
|
|
|
|
|
Def::SelfTy(a, b) => Def::SelfTy(a, b),
|
|
|
|
|
Def::Macro(id, macro_kind) => Def::Macro(id, macro_kind),
|
|
|
|
|
Def::ToolMod => Def::ToolMod,
|
|
|
|
|
Def::NonMacroAttr(attr_kind) => Def::NonMacroAttr(attr_kind),
|
|
|
|
|
Def::Err => Def::Err,
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-14 15:31:30 -04:00
|
|
|
|
}
|