1 use crate::hir
::def_id
::DefId
;
2 use crate::util
::nodemap
::{NodeMap, DefIdMap}
;
4 use syntax
::ext
::base
::MacroKind
;
9 use self::Namespace
::*;
11 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
13 /// Constructor function automatically created by a tuple struct/variant.
15 /// Constructor constant automatically created by a unit struct/variant.
17 /// Unusable name in value namespace created by a struct variant.
21 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
22 pub enum NonMacroAttrKind
{
23 /// Single-segment attribute defined by the language (`#[inline]`)
25 /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
27 /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
29 /// Single-segment custom attribute registered by a legacy plugin (`register_attribute`).
31 /// Single-segment custom attribute not registered in any way (`#[my_attr]`).
35 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
39 Struct(DefId
), // `DefId` refers to `NodeId` of the struct itself
44 /// `existential type Foo: Bar;`
51 /// `existential type Foo: Bar;`
52 AssociatedExistential(DefId
),
55 SelfTy(Option
<DefId
> /* trait */, Option
<DefId
> /* impl */),
56 ToolMod
, // e.g., `rustfmt` in `#[rustfmt::skip]`
62 Static(DefId
, bool
/* is_mutbl */),
63 StructCtor(DefId
, CtorKind
), // `DefId` refers to `NodeId` of the struct's constructor
64 VariantCtor(DefId
, CtorKind
), // `DefId` refers to the enum variant
65 SelfCtor(DefId
/* impl */), // `DefId` refers to the impl
67 AssociatedConst(DefId
),
70 Upvar(ast
::NodeId
, // `NodeId` of closed over local
71 usize, // index in the `freevars` list of the closure
72 ast
::NodeId
), // expr node that creates the closure
76 Macro(DefId
, MacroKind
),
77 NonMacroAttr(NonMacroAttrKind
), // e.g., `#[inline]` or `#[rustfmt::skip]`
83 /// The result of resolving a path before lowering to HIR.
84 /// `base_def` is definition of resolved part of the
85 /// path, `unresolved_segments` is the number of unresolved
89 /// module::Type::AssocX::AssocY::MethodOrAssocType
90 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91 /// base_def unresolved_segments = 3
93 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
94 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
95 /// base_def unresolved_segments = 2
97 #[derive(Copy, Clone, Debug)]
98 pub struct PathResolution
{
100 unresolved_segments
: usize,
103 impl PathResolution
{
104 pub fn new(def
: Def
) -> Self {
105 PathResolution { base_def: def, unresolved_segments: 0 }
108 pub fn with_unresolved_segments(def
: Def
, mut unresolved_segments
: usize) -> Self {
109 if def
== Def
::Err { unresolved_segments = 0 }
110 PathResolution { base_def: def, unresolved_segments: unresolved_segments }
114 pub fn base_def(&self) -> Def
{
119 pub fn unresolved_segments(&self) -> usize {
120 self.unresolved_segments
124 /// Different kinds of symbols don't influence each other.
126 /// Therefore, they have a separate universe (namespace).
127 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
135 pub fn descr(self) -> &'
static str {
144 /// Just a helper ‒ separate structure for each namespace.
145 #[derive(Copy, Clone, Default, Debug)]
146 pub struct PerNS
<T
> {
153 pub fn map
<U
, F
: FnMut(T
) -> U
>(self, mut f
: F
) -> PerNS
<U
> {
155 value_ns
: f(self.value_ns
),
156 type_ns
: f(self.type_ns
),
157 macro_ns
: f(self.macro_ns
),
162 impl<T
> ::std
::ops
::Index
<Namespace
> for PerNS
<T
> {
165 fn index(&self, ns
: Namespace
) -> &T
{
167 ValueNS
=> &self.value_ns
,
168 TypeNS
=> &self.type_ns
,
169 MacroNS
=> &self.macro_ns
,
174 impl<T
> ::std
::ops
::IndexMut
<Namespace
> for PerNS
<T
> {
175 fn index_mut(&mut self, ns
: Namespace
) -> &mut T
{
177 ValueNS
=> &mut self.value_ns
,
178 TypeNS
=> &mut self.type_ns
,
179 MacroNS
=> &mut self.macro_ns
,
184 impl<T
> PerNS
<Option
<T
>> {
185 /// Returns `true` if all the items in this collection are `None`.
186 pub fn is_empty(&self) -> bool
{
187 self.type_ns
.is_none() && self.value_ns
.is_none() && self.macro_ns
.is_none()
190 /// Returns an iterator over the items which are `Some`.
191 pub fn present_items(self) -> impl Iterator
<Item
=T
> {
195 .chain(once(self.value_ns
))
196 .chain(once(self.macro_ns
))
201 /// Definition mapping
202 pub type DefMap
= NodeMap
<PathResolution
>;
204 /// This is the replacement export map. It maps a module to all of the exports
206 pub type ExportMap
= DefIdMap
<Vec
<Export
>>;
208 /// Map used to track the `use` statements within a scope, matching it with all the items in every
210 pub type ImportMap
= NodeMap
<PerNS
<Option
<PathResolution
>>>;
212 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
214 /// The name of the target.
215 pub ident
: ast
::Ident
,
216 /// The definition of the target.
218 /// The span of the target definition.
220 /// The visibility of the export.
221 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
222 pub vis
: ty
::Visibility
,
226 pub fn from_ast(vdata
: &ast
::VariantData
) -> CtorKind
{
228 ast
::VariantData
::Tuple(..) => CtorKind
::Fn
,
229 ast
::VariantData
::Unit(..) => CtorKind
::Const
,
230 ast
::VariantData
::Struct(..) => CtorKind
::Fictive
,
234 pub fn from_hir(vdata
: &hir
::VariantData
) -> CtorKind
{
236 hir
::VariantData
::Tuple(..) => CtorKind
::Fn
,
237 hir
::VariantData
::Unit(..) => CtorKind
::Const
,
238 hir
::VariantData
::Struct(..) => CtorKind
::Fictive
,
243 impl NonMacroAttrKind
{
244 pub fn descr(self) -> &'
static str {
246 NonMacroAttrKind
::Builtin
=> "built-in attribute",
247 NonMacroAttrKind
::Tool
=> "tool attribute",
248 NonMacroAttrKind
::DeriveHelper
=> "derive helper attribute",
249 NonMacroAttrKind
::LegacyPluginHelper
=> "legacy plugin helper attribute",
250 NonMacroAttrKind
::Custom
=> "custom attribute",
256 /// Return the `DefId` of this `Def` if it has an id, else panic.
257 pub fn def_id(&self) -> DefId
{
258 self.opt_def_id().unwrap_or_else(|| {
259 bug
!("attempted .def_id() on invalid def: {:?}", self)
263 /// Return `Some(..)` with the `DefId` of this `Def` if it has a id, else `None`.
264 pub fn opt_def_id(&self) -> Option
<DefId
> {
266 Def
::Fn(id
) | Def
::Mod(id
) | Def
::Static(id
, _
) |
267 Def
::Variant(id
) | Def
::VariantCtor(id
, ..) | Def
::Enum(id
) |
268 Def
::TyAlias(id
) | Def
::TraitAlias(id
) |
269 Def
::AssociatedTy(id
) | Def
::TyParam(id
) | Def
::ConstParam(id
) | Def
::Struct(id
) |
270 Def
::StructCtor(id
, ..) |
271 Def
::Union(id
) | Def
::Trait(id
) | Def
::Method(id
) | Def
::Const(id
) |
272 Def
::AssociatedConst(id
) | Def
::Macro(id
, ..) |
273 Def
::Existential(id
) | Def
::AssociatedExistential(id
) | Def
::ForeignTy(id
) => {
284 Def
::NonMacroAttr(..) |
291 /// Return the `DefId` of this `Def` if it represents a module.
292 pub fn mod_def_id(&self) -> Option
<DefId
> {
294 Def
::Mod(id
) => Some(id
),
299 /// A human readable name for the def kind ("function", "module", etc.).
300 pub fn kind_name(&self) -> &'
static str {
302 Def
::Fn(..) => "function",
303 Def
::Mod(..) => "module",
304 Def
::Static(..) => "static",
305 Def
::Variant(..) => "variant",
306 Def
::VariantCtor(.., CtorKind
::Fn
) => "tuple variant",
307 Def
::VariantCtor(.., CtorKind
::Const
) => "unit variant",
308 Def
::VariantCtor(.., CtorKind
::Fictive
) => "struct variant",
309 Def
::Enum(..) => "enum",
310 Def
::Existential(..) => "existential type",
311 Def
::TyAlias(..) => "type alias",
312 Def
::TraitAlias(..) => "trait alias",
313 Def
::AssociatedTy(..) => "associated type",
314 Def
::AssociatedExistential(..) => "associated existential type",
315 Def
::Struct(..) => "struct",
316 Def
::StructCtor(.., CtorKind
::Fn
) => "tuple struct",
317 Def
::StructCtor(.., CtorKind
::Const
) => "unit struct",
318 Def
::StructCtor(.., CtorKind
::Fictive
) => bug
!("impossible struct constructor"),
319 Def
::SelfCtor(..) => "self constructor",
320 Def
::Union(..) => "union",
321 Def
::Trait(..) => "trait",
322 Def
::ForeignTy(..) => "foreign type",
323 Def
::Method(..) => "method",
324 Def
::Const(..) => "constant",
325 Def
::AssociatedConst(..) => "associated constant",
326 Def
::TyParam(..) => "type parameter",
327 Def
::ConstParam(..) => "const parameter",
328 Def
::PrimTy(..) => "builtin type",
329 Def
::Local(..) => "local variable",
330 Def
::Upvar(..) => "closure capture",
331 Def
::Label(..) => "label",
332 Def
::SelfTy(..) => "self type",
333 Def
::Macro(.., macro_kind
) => macro_kind
.descr(),
334 Def
::ToolMod
=> "tool module",
335 Def
::NonMacroAttr(attr_kind
) => attr_kind
.descr(),
336 Def
::Err
=> "unresolved item",
340 /// An English article for the def.
341 pub fn article(&self) -> &'
static str {
343 Def
::AssociatedTy(..) | Def
::AssociatedConst(..) | Def
::AssociatedExistential(..) |
344 Def
::Enum(..) | Def
::Existential(..) | Def
::Err
=> "an",
345 Def
::Macro(.., macro_kind
) => macro_kind
.article(),