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]`.
47 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
51 /// Refers to the struct itself, `DefKind::Ctor` refers to its constructor if it exists.
55 /// Refers to the variant itself, `DefKind::Ctor` refers to its constructor if it exists.
58 /// `type Foo = impl Bar;`
65 /// `type Foo = impl Bar;`
74 /// Refers to the struct or enum variant's constructor.
75 Ctor(CtorOf, CtorKind),
84 pub fn descr(self, def_id: DefId) -> &'static str {
86 DefKind::Fn => "function",
87 DefKind::Mod if def_id.index == CRATE_DEF_INDEX && def_id.krate != LOCAL_CRATE =>
89 DefKind::Mod => "module",
90 DefKind::Static => "static",
91 DefKind::Enum => "enum",
92 DefKind::Variant => "variant",
93 DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
94 DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
95 DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
96 DefKind::Struct => "struct",
97 DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
98 DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
99 DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) =>
100 bug!("impossible struct constructor"),
101 DefKind::OpaqueTy => "opaque type",
102 DefKind::TyAlias => "type alias",
103 DefKind::TraitAlias => "trait alias",
104 DefKind::AssocTy => "associated type",
105 DefKind::AssocOpaqueTy => "associated opaque type",
106 DefKind::Union => "union",
107 DefKind::Trait => "trait",
108 DefKind::ForeignTy => "foreign type",
109 DefKind::Method => "method",
110 DefKind::Const => "constant",
111 DefKind::AssocConst => "associated constant",
112 DefKind::TyParam => "type parameter",
113 DefKind::ConstParam => "const parameter",
114 DefKind::Macro(macro_kind) => macro_kind.descr(),
118 /// Gets an English article for the definition.
119 pub fn article(&self) -> &'static str {
122 | DefKind::AssocConst
123 | DefKind::AssocOpaqueTy
125 | DefKind::OpaqueTy => "an",
126 DefKind::Macro(macro_kind) => macro_kind.article(),
132 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, HashStable)]
133 pub enum Res<Id = hir::HirId> {
139 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
140 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
144 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
149 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
156 /// The result of resolving a path before lowering to HIR,
157 /// with "module" segments resolved and associated item
158 /// segments deferred to type checking.
159 /// `base_res` is the resolution of the resolved part of the
160 /// path, `unresolved_segments` is the number of unresolved
164 /// module::Type::AssocX::AssocY::MethodOrAssocType
165 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166 /// base_res unresolved_segments = 3
168 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
169 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
170 /// base_res unresolved_segments = 2
172 #[derive(Copy, Clone, Debug)]
173 pub struct PartialRes {
174 base_res: Res<NodeId>,
175 unresolved_segments: usize,
180 pub fn new(base_res: Res<NodeId>) -> Self {
181 PartialRes { base_res, unresolved_segments: 0 }
185 pub fn with_unresolved_segments(base_res: Res<NodeId>, mut unresolved_segments: usize) -> Self {
186 if base_res == Res::Err { unresolved_segments = 0 }
187 PartialRes { base_res, unresolved_segments }
191 pub fn base_res(&self) -> Res<NodeId> {
196 pub fn unresolved_segments(&self) -> usize {
197 self.unresolved_segments
201 /// Different kinds of symbols don't influence each other.
203 /// Therefore, they have a separate universe (namespace).
204 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
212 pub fn descr(self) -> &'static str {
221 /// Just a helper ‒ separate structure for each namespace.
222 #[derive(Copy, Clone, Default, Debug)]
223 pub struct PerNS<T> {
230 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
232 value_ns: f(self.value_ns),
233 type_ns: f(self.type_ns),
234 macro_ns: f(self.macro_ns),
239 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
242 fn index(&self, ns: Namespace) -> &T {
244 ValueNS => &self.value_ns,
245 TypeNS => &self.type_ns,
246 MacroNS => &self.macro_ns,
251 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
252 fn index_mut(&mut self, ns: Namespace) -> &mut T {
254 ValueNS => &mut self.value_ns,
255 TypeNS => &mut self.type_ns,
256 MacroNS => &mut self.macro_ns,
261 impl<T> PerNS<Option<T>> {
262 /// Returns `true` if all the items in this collection are `None`.
263 pub fn is_empty(&self) -> bool {
264 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
267 /// Returns an iterator over the items which are `Some`.
268 pub fn present_items(self) -> impl Iterator<Item=T> {
272 .chain(once(self.value_ns))
273 .chain(once(self.macro_ns))
278 /// This is the replacement export map. It maps a module to all of the exports
280 pub type ExportMap<Id> = DefIdMap<Vec<Export<Id>>>;
282 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, HashStable)]
283 pub struct Export<Id> {
284 /// The name of the target.
285 pub ident: ast::Ident,
286 /// The resolution of the target.
288 /// The span of the target.
290 /// The visibility of the export.
291 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
292 pub vis: ty::Visibility,
295 impl<Id> Export<Id> {
296 pub fn map_id<R>(self, map: impl FnMut(Id) -> R) -> Export<R> {
299 res: self.res.map_id(map),
307 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
309 ast::VariantData::Tuple(..) => CtorKind::Fn,
310 ast::VariantData::Unit(..) => CtorKind::Const,
311 ast::VariantData::Struct(..) => CtorKind::Fictive,
315 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
317 hir::VariantData::Tuple(..) => CtorKind::Fn,
318 hir::VariantData::Unit(..) => CtorKind::Const,
319 hir::VariantData::Struct(..) => CtorKind::Fictive,
324 impl NonMacroAttrKind {
325 pub fn descr(self) -> &'static str {
327 NonMacroAttrKind::Builtin => "built-in attribute",
328 NonMacroAttrKind::Tool => "tool attribute",
329 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
330 NonMacroAttrKind::Registered => "explicitly registered attribute",
334 pub fn article(self) -> &'static str {
336 NonMacroAttrKind::Registered => "an",
341 /// Users of some attributes cannot mark them as used, so they are considered always used.
342 pub fn is_used(self) -> bool {
344 NonMacroAttrKind::Tool | NonMacroAttrKind::DeriveHelper => true,
345 NonMacroAttrKind::Builtin | NonMacroAttrKind::Registered => false,
351 /// Return the `DefId` of this `Def` if it has an ID, else panic.
352 pub fn def_id(&self) -> DefId
356 self.opt_def_id().unwrap_or_else(|| {
357 bug!("attempted .def_id() on invalid res: {:?}", self)
361 /// Return `Some(..)` with the `DefId` of this `Res` if it has a ID, else `None`.
362 pub fn opt_def_id(&self) -> Option<DefId> {
364 Res::Def(_, id) => Some(id),
371 Res::NonMacroAttr(..) |
378 /// Return the `DefId` of this `Res` if it represents a module.
379 pub fn mod_def_id(&self) -> Option<DefId> {
381 Res::Def(DefKind::Mod, id) => Some(id),
386 /// A human readable name for the res kind ("function", "module", etc.).
387 pub fn descr(&self) -> &'static str {
389 Res::Def(kind, def_id) => kind.descr(def_id),
390 Res::SelfCtor(..) => "self constructor",
391 Res::PrimTy(..) => "builtin type",
392 Res::Local(..) => "local variable",
393 Res::SelfTy(..) => "self type",
394 Res::ToolMod => "tool module",
395 Res::NonMacroAttr(attr_kind) => attr_kind.descr(),
396 Res::Err => "unresolved item",
400 /// Gets an English article for the `Res`.
401 pub fn article(&self) -> &'static str {
403 Res::Def(kind, _) => kind.article(),
404 Res::NonMacroAttr(kind) => kind.article(),
410 pub fn map_id<R>(self, mut map: impl FnMut(Id) -> R) -> Res<R> {
412 Res::Def(kind, id) => Res::Def(kind, id),
413 Res::SelfCtor(id) => Res::SelfCtor(id),
414 Res::PrimTy(id) => Res::PrimTy(id),
415 Res::Local(id) => Res::Local(map(id)),
416 Res::SelfTy(a, b) => Res::SelfTy(a, b),
417 Res::ToolMod => Res::ToolMod,
418 Res::NonMacroAttr(attr_kind) => Res::NonMacroAttr(attr_kind),
419 Res::Err => Res::Err,
423 pub fn macro_kind(self) -> Option<MacroKind> {
425 Res::Def(DefKind::Macro(kind), _) => Some(kind),
426 Res::NonMacroAttr(..) => Some(MacroKind::Attr),