1 use self::Namespace::*;
3 use crate::hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
6 use crate::util::nodemap::DefIdMap;
9 use syntax::ast::NodeId;
10 use syntax_pos::hygiene::MacroKind;
12 use rustc_macros::HashStable;
16 /// Encodes if a `DefKind::Ctor` is the constructor of an enum variant or a struct.
17 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
19 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit struct.
21 /// This `DefKind::Ctor` is a synthesized constructor of a tuple or unit variant.
25 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
27 /// Constructor function automatically created by a tuple struct/variant.
29 /// Constructor constant automatically created by a unit struct/variant.
31 /// Unusable name in value namespace created by a struct variant.
35 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
36 pub enum NonMacroAttrKind {
37 /// Single-segment attribute defined by the language (`#[inline]`)
39 /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
41 /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
43 /// Single-segment custom attribute registered with `#[register_attr]`.
45 /// Single-segment custom attribute registered by a legacy plugin (`register_attribute`).
49 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
53 /// Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.
57 /// Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.
60 /// `type Foo = impl Bar;`
67 /// `type Foo = impl Bar;`
76 /// Refers to the struct or enum variant's constructor.
77 Ctor(CtorOf, CtorKind),
86 pub fn descr(self, def_id: DefId) -> &'static str {
88 DefKind::Fn => "function",
89 DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE =>
91 DefKind::Mod => "module",
92 DefKind::Static => "static",
93 DefKind::Enum => "enum",
94 DefKind::Variant => "variant",
95 DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
96 DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
97 DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
98 DefKind::Struct => "struct",
99 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
100 DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
101 DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) =>
102 bug!("impossible struct constructor"),
103 DefKind::OpaqueTy => "opaque type",
104 DefKind::TyAlias => "type alias",
105 DefKind::TraitAlias => "trait alias",
106 DefKind::AssocTy => "associated type",
107 DefKind::AssocOpaqueTy => "associated opaque type",
108 DefKind::Union => "union",
109 DefKind::Trait => "trait",
110 DefKind::ForeignTy => "foreign type",
111 DefKind::Method => "method",
112 DefKind::Const => "constant",
113 DefKind::AssocConst => "associated constant",
114 DefKind::TyParam => "type parameter",
115 DefKind::ConstParam => "const parameter",
116 DefKind::Macro(macro_kind) => macro_kind.descr(),
120 /// Gets an English article for the definition.
121 pub fn article(&self) -> &'static str {
124 | DefKind::AssocConst
125 | DefKind::AssocOpaqueTy
127 | DefKind::OpaqueTy => "an",
128 DefKind::Macro(macro_kind) => macro_kind.article(),
134 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
135 pub enum Res<Id = hir::HirId> {
141 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
142 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
146 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
151 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
158 /// The result of resolving a path before lowering to HIR,
159 /// with "module" segments resolved and associated item
160 /// segments deferred to type checking.
161 /// `base_res` is the resolution of the resolved part of the
162 /// path, `unresolved_segments` is the number of unresolved
166 /// module::Type::AssocX::AssocY::MethodOrAssocType
167 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168 /// base_res unresolved_segments = 3
170 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
171 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
172 /// base_res unresolved_segments = 2
174 #[derive(Copy, Clone, Debug)]
175 pub struct PartialRes {
176 base_res: Res<NodeId>,
177 unresolved_segments: usize,
182 pub fn new(base_res: Res<NodeId>) -> Self {
183 PartialRes { base_res, unresolved_segments: 0 }
187 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
188 if base_res == Res::Err { unresolved_segments = 0 }
189 PartialRes { base_res, unresolved_segments }
193 pub fn base_res(&self) -> Res<NodeId> {
198 pub fn unresolved_segments(&self) -> usize {
199 self.unresolved_segments
203 /// Different kinds of symbols don't influence each other.
205 /// Therefore, they have a separate universe (namespace).
206 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
214 pub fn descr(self) -> &'static str {
223 /// Just a helper ‒ separate structure for each namespace.
224 #[derive(Copy, Clone, Default, Debug)]
225 pub struct PerNS<T> {
232 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
234 value_ns: f(self.value_ns),
235 type_ns: f(self.type_ns),
236 macro_ns: f(self.macro_ns),
241 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
244 fn index(&self, ns: Namespace) -> &T {
246 ValueNS => &self.value_ns,
247 TypeNS => &self.type_ns,
248 MacroNS => &self.macro_ns,
253 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
254 fn index_mut(&mut self, ns: Namespace) -> &mut T {
256 ValueNS => &mut self.value_ns,
257 TypeNS => &mut self.type_ns,
258 MacroNS => &mut self.macro_ns,
263 impl<T> PerNS<Option<T>> {
264 /// Returns `true` if all the items in this collection are `None`.
265 pub fn is_empty(&self) -> bool {
266 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
269 /// Returns an iterator over the items which are `Some`.
270 pub fn present_items(self) -> impl Iterator<Item=T> {
274 .chain(once(self.value_ns))
275 .chain(once(self.macro_ns))
280 /// This is the replacement export map. It maps a module to all of the exports
282 pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;
284 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
285 pub struct Export<Id> {
286 /// The name of the target.
287 pub ident: ast::Ident,
288 /// The resolution of the target.
290 /// The span of the target.
292 /// The visibility of the export.
293 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
294 pub vis: ty::Visibility,
297 impl<Id> Export<Id> {
298 pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
301 res: self.res.map_id(map),
309 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
311 ast::VariantData::Tuple(..) => CtorKind::Fn,
312 ast::VariantData::Unit(..) => CtorKind::Const,
313 ast::VariantData::Struct(..) => CtorKind::Fictive,
317 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
319 hir::VariantData::Tuple(..) => CtorKind::Fn,
320 hir::VariantData::Unit(..) => CtorKind::Const,
321 hir::VariantData::Struct(..) => CtorKind::Fictive,
326 impl NonMacroAttrKind {
327 pub fn descr(self) -> &'static str {
329 NonMacroAttrKind::Builtin => "built-in attribute",
330 NonMacroAttrKind::Tool => "tool attribute",
331 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
332 NonMacroAttrKind::Registered => "explicitly registered attribute",
333 NonMacroAttrKind::LegacyPluginHelper => "legacy plugin helper attribute",
337 pub fn article(self) -> &'static str {
339 NonMacroAttrKind::Registered => "an",
344 /// Users of some attributes cannot mark them as used, so they are considered always used.
345 pub fn is_used(self) -> bool {
347 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
348 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered |
349 NonMacroAttrKind::LegacyPluginHelper => false,
355 /// Return the `DefId` of this `Def` if it has an ID, else panic.
356 pub fn def_id(&self) -> DefId
360 self.opt_def_id().unwrap_or_else(|| {
361 bug!("attempted .def_id() on invalid res: {:?}", self)
365 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
366 pub fn opt_def_id(&self) -> Option<DefId> {
368 Res::Def(_, id) => Some(id),
375 Res::NonMacroAttr(..) |
382 /// Return the `DefId` of this `Res` if it represents a module.
383 pub fn mod_def_id(&self) -> Option<DefId> {
385 Res::Def(DefKind::Mod, id) => Some(id),
390 /// A human readable name for the res kind ("function", "module", etc.).
391 pub fn descr(&self) -> &'static str {
393 Res::Def(kind, def_id) => kind.descr(def_id),
394 Res::SelfCtor(..) => "self constructor",
395 Res::PrimTy(..) => "builtin type",
396 Res::Local(..) => "local variable",
397 Res::SelfTy(..) => "self type",
398 Res::ToolMod => "tool module",
399 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
400 Res::Err => "unresolved item",
404 /// Gets an English article for the `Res`.
405 pub fn article(&self) -> &'static str {
407 Res::Def(kind, _) => kind.article(),
408 Res::NonMacroAttr(kind) => kind.article(),
414 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
416 Res::Def(kind, id) => Res::Def(kind, id),
417 Res::SelfCtor(id) => Res::SelfCtor(id),
418 Res::PrimTy(id) => Res::PrimTy(id),
419 Res::Local(id) => Res::Local(map(id)),
420 Res::SelfTy(a, b) => Res::SelfTy(a, b),
421 Res::ToolMod => Res::ToolMod,
422 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
423 Res::Err => Res::Err,
427 pub fn macro_kind(self) -> Option<MacroKind> {
429 Res::Def(DefKind::Macro(kind), _) => Some(kind),
430 Res::NonMacroAttr(..) => Some(MacroKind::Attr),