1//! Module that define a common trait for things that represent a crate definition,
2//! such as, a function, a trait, an enum, and any other definitions.
34use crate::ty::{GenericArgs, Span, Ty, index_impl};
5use crate::{Crate, Symbol, ThreadLocalIndex, with};
67/// A unique identification number for each item accessible for the current compilation unit.
8#[derive(#[automatically_derived]
impl ::core::clone::Clone for DefId {
#[inline]
fn clone(&self) -> DefId {
let _: ::core::clone::AssertParamIsClone<usize>;
let _: ::core::clone::AssertParamIsClone<ThreadLocalIndex>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for DefId { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for DefId {
#[inline]
fn eq(&self, other: &DefId) -> bool {
self.0 == other.0 && self.1 == other.1
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for DefId {
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<usize>;
let _: ::core::cmp::AssertParamIsEq<ThreadLocalIndex>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for DefId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state);
::core::hash::Hash::hash(&self.1, state)
}
}Hash)]
9pub struct DefId(pub(crate) usize, ThreadLocalIndex);
10impl crate::IndexedVal for DefId {
fn to_val(index: usize) -> Self { DefId(index, crate::ThreadLocalIndex) }
fn to_index(&self) -> usize { self.0 }
}
impl ::serde::Serialize for DefId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where
S: ::serde::Serializer {
let n: usize = self.0;
::serde::Serialize::serialize(&n, serializer)
}
}index_impl!(DefId);
1112impl DefId {
13/// Return fully qualified name of this definition
14pub fn name(&self) -> Symbol {
15with(|cx| cx.def_name(*self, false))
16 }
1718/// Return a trimmed name of this definition.
19 ///
20 /// This can be used to print more user friendly diagnostic messages.
21 ///
22 /// If a symbol name can only be imported from one place for a type, and as
23 /// long as it was not glob-imported anywhere in the current crate, we trim its
24 /// path and print only the name.
25 ///
26 /// For example, this function may shorten `std::vec::Vec` to just `Vec`,
27 /// as long as there is no other `Vec` importable anywhere.
28pub fn trimmed_name(&self) -> Symbol {
29with(|cx| cx.def_name(*self, true))
30 }
3132/// Return the parent of this definition, or `None` if this is the root of a
33 /// crate.
34pub fn parent(&self) -> Option<DefId> {
35with(|cx| cx.def_parent(*self))
36 }
3738pub fn span(&self) -> Span {
39with(|cx| cx.span_of_a_def(*self))
40 }
41}
4243/// A trait for retrieving information about a particular definition.
44///
45/// Implementors must provide the implementation of `def_id` which will be used to retrieve
46/// information about a crate's definition.
47pub trait CrateDef {
48/// Retrieve the unique identifier for the current definition.
49fn def_id(&self) -> DefId;
5051/// Return the fully qualified name of the current definition.
52 ///
53 /// See [`DefId::name`] for more details
54fn name(&self) -> Symbol {
55self.def_id().name()
56 }
5758/// Return a trimmed name of this definition.
59 ///
60 /// See [`DefId::trimmed_name`] for more details
61fn trimmed_name(&self) -> Symbol {
62self.def_id().trimmed_name()
63 }
6465/// Return information about the crate where this definition is declared.
66 ///
67 /// This will return the crate number and its name.
68fn krate(&self) -> Crate {
69let def_id = self.def_id();
70with(|cx| cx.krate(def_id))
71 }
7273/// Return the span of this definition.
74fn span(&self) -> Span {
75self.def_id().span()
76 }
7778/// Return registered tool attributes with the given attribute name.
79 ///
80 /// FIXME(jdonszelmann): may panic on non-tool attributes. After more attribute work, non-tool
81 /// attributes will simply return an empty list.
82 ///
83 /// Single segmented name like `#[clippy]` is specified as `&["clippy".to_string()]`.
84 /// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
85fn tool_attrs(&self, attr: &[Symbol]) -> Vec<Attribute> {
86let def_id = self.def_id();
87with(|cx| cx.tool_attrs(def_id, attr))
88 }
8990/// Return all tool attributes of this definition.
91fn all_tool_attrs(&self) -> Vec<Attribute> {
92let def_id = self.def_id();
93with(|cx| cx.all_tool_attrs(def_id))
94 }
95}
9697/// A trait that can be used to retrieve a definition's type.
98///
99/// Note that not every CrateDef has a type `Ty`. They should not implement this trait.
100pub trait CrateDefType: CrateDef {
101/// Returns the type of this crate item.
102fn ty(&self) -> Ty {
103with(|cx| cx.def_ty(self.def_id()))
104 }
105106/// Retrieve the type of this definition by instantiating and normalizing it with `args`.
107 ///
108 /// This will panic if instantiation fails.
109fn ty_with_args(&self, args: &GenericArgs) -> Ty {
110with(|cx| cx.def_ty_with_args(self.def_id(), args))
111 }
112}
113114#[derive(#[automatically_derived]
impl ::core::clone::Clone for Attribute {
#[inline]
fn clone(&self) -> Attribute {
Attribute {
value: ::core::clone::Clone::clone(&self.value),
span: ::core::clone::Clone::clone(&self.span),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Attribute {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "Attribute",
"value", &self.value, "span", &&self.span)
}
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Attribute {
#[inline]
fn eq(&self, other: &Attribute) -> bool {
self.value == other.value && self.span == other.span
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Attribute {
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<String>;
let _: ::core::cmp::AssertParamIsEq<Span>;
}
}Eq)]
115pub struct Attribute {
116 value: String,
117 span: Span,
118}
119120impl Attribute {
121pub fn new(value: String, span: Span) -> Attribute {
122Attribute { value, span }
123 }
124125/// Get the span of this attribute.
126pub fn span(&self) -> Span {
127self.span
128 }
129130/// Get the string representation of this attribute.
131pub fn as_str(&self) -> &str {
132&self.value
133 }
134}
135136macro_rules!crate_def {
137 ( $(#[$attr:meta])*
138$vis:vis $name:ident $(;)?
139) => {
140 $(#[$attr])*
141#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
142$vis struct $name(pub DefId);
143144impl CrateDef for $name {
145fn def_id(&self) -> DefId {
146self.0
147}
148 }
149 };
150}
151152macro_rules!crate_def_with_ty {
153 ( $(#[$attr:meta])*
154$vis:vis $name:ident $(;)?
155) => {
156 $(#[$attr])*
157#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
158$vis struct $name(pub DefId);
159160impl CrateDef for $name {
161fn def_id(&self) -> DefId {
162self.0
163}
164 }
165166impl CrateDefType for $name {}
167 };
168}