1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use hir::def_id::DefId;
12 use util::nodemap::{NodeMap, DefIdMap};
14 use syntax::ext::base::MacroKind;
19 use self::Namespace::*;
21 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
23 /// Constructor function automatically created by a tuple struct/variant.
25 /// Constructor constant automatically created by a unit struct/variant.
27 /// Unusable name in value namespace created by a struct variant.
31 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
32 pub enum NonMacroAttrKind {
33 /// Single-segment attribute defined by the language (`#[inline]`)
35 /// Multi-segment custom attribute living in a "tool module" (`#[rustfmt::skip]`).
37 /// Single-segment custom attribute registered by a derive macro (`#[serde(default)]`).
39 /// Single-segment custom attribute registered by a legacy plugin (`register_attribute`).
41 /// Single-segment custom attribute not registered in any way (`#[my_attr]`).
45 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
49 Struct(DefId), // `DefId` refers to `NodeId` of the struct itself
54 /// `existential type Foo: Bar;`
61 /// `existential type Foo: Bar;`
62 AssociatedExistential(DefId),
65 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
66 ToolMod, // e.g., `rustfmt` in `#[rustfmt::skip]`
71 Static(DefId, bool /* is_mutbl */),
72 StructCtor(DefId, CtorKind), // `DefId` refers to `NodeId` of the struct's constructor
73 VariantCtor(DefId, CtorKind), // `DefId` refers to the enum variant
74 SelfCtor(DefId /* impl */), // `DefId` refers to the impl
76 AssociatedConst(DefId),
79 Upvar(ast::NodeId, // `NodeId` of closed over local
80 usize, // index in the `freevars` list of the closure
81 ast::NodeId), // expr node that creates the closure
85 Macro(DefId, MacroKind),
86 NonMacroAttr(NonMacroAttrKind), // e.g., `#[inline]` or `#[rustfmt::skip]`
92 /// The result of resolving a path before lowering to HIR.
93 /// `base_def` is definition of resolved part of the
94 /// path, `unresolved_segments` is the number of unresolved
98 /// module::Type::AssocX::AssocY::MethodOrAssocType
99 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100 /// base_def unresolved_segments = 3
102 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
103 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
104 /// base_def unresolved_segments = 2
106 #[derive(Copy, Clone, Debug)]
107 pub struct PathResolution {
109 unresolved_segments: usize,
112 impl PathResolution {
113 pub fn new(def: Def) -> Self {
114 PathResolution { base_def: def, unresolved_segments: 0 }
117 pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self {
118 if def == Def::Err { unresolved_segments = 0 }
119 PathResolution { base_def: def, unresolved_segments: unresolved_segments }
123 pub fn base_def(&self) -> Def {
128 pub fn unresolved_segments(&self) -> usize {
129 self.unresolved_segments
133 /// Different kinds of symbols don't influence each other.
135 /// Therefore, they have a separate universe (namespace).
136 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
144 pub fn descr(self) -> &'static str {
153 /// Just a helper ‒ separate structure for each namespace.
154 #[derive(Copy, Clone, Default, Debug)]
155 pub struct PerNS<T> {
162 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
164 value_ns: f(self.value_ns),
165 type_ns: f(self.type_ns),
166 macro_ns: f(self.macro_ns),
171 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
174 fn index(&self, ns: Namespace) -> &T {
176 ValueNS => &self.value_ns,
177 TypeNS => &self.type_ns,
178 MacroNS => &self.macro_ns,
183 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
184 fn index_mut(&mut self, ns: Namespace) -> &mut T {
186 ValueNS => &mut self.value_ns,
187 TypeNS => &mut self.type_ns,
188 MacroNS => &mut self.macro_ns,
193 impl<T> PerNS<Option<T>> {
194 /// Returns whether all the items in this collection are `None`.
195 pub fn is_empty(&self) -> bool {
196 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
199 /// Returns an iterator over the items which are `Some`.
200 pub fn present_items(self) -> impl Iterator<Item=T> {
204 .chain(once(self.value_ns))
205 .chain(once(self.macro_ns))
210 /// Definition mapping
211 pub type DefMap = NodeMap<PathResolution>;
213 /// This is the replacement export map. It maps a module to all of the exports
215 pub type ExportMap = DefIdMap<Vec<Export>>;
217 /// Map used to track the `use` statements within a scope, matching it with all the items in every
219 pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;
221 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
223 /// The name of the target.
224 pub ident: ast::Ident,
225 /// The definition of the target.
227 /// The span of the target definition.
229 /// The visibility of the export.
230 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
231 pub vis: ty::Visibility,
235 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
237 ast::VariantData::Tuple(..) => CtorKind::Fn,
238 ast::VariantData::Unit(..) => CtorKind::Const,
239 ast::VariantData::Struct(..) => CtorKind::Fictive,
243 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
245 hir::VariantData::Tuple(..) => CtorKind::Fn,
246 hir::VariantData::Unit(..) => CtorKind::Const,
247 hir::VariantData::Struct(..) => CtorKind::Fictive,
252 impl NonMacroAttrKind {
253 fn descr(self) -> &'static str {
255 NonMacroAttrKind::Builtin => "built-in attribute",
256 NonMacroAttrKind::Tool => "tool attribute",
257 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
258 NonMacroAttrKind::LegacyPluginHelper => "legacy plugin helper attribute",
259 NonMacroAttrKind::Custom => "custom attribute",
265 pub fn def_id(&self) -> DefId {
266 self.opt_def_id().unwrap_or_else(|| {
267 bug!("attempted .def_id() on invalid def: {:?}", self)
271 pub fn opt_def_id(&self) -> Option<DefId> {
273 Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
274 Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
275 Def::TyAlias(id) | Def::TraitAlias(id) |
276 Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
277 Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
278 Def::AssociatedConst(id) | Def::Macro(id, ..) |
279 Def::Existential(id) | Def::AssociatedExistential(id) | Def::ForeignTy(id) => {
290 Def::NonMacroAttr(..) |
297 /// A human readable kind name
298 pub fn kind_name(&self) -> &'static str {
300 Def::Fn(..) => "function",
301 Def::Mod(..) => "module",
302 Def::Static(..) => "static",
303 Def::Variant(..) => "variant",
304 Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
305 Def::VariantCtor(.., CtorKind::Const) => "unit variant",
306 Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
307 Def::Enum(..) => "enum",
308 Def::Existential(..) => "existential type",
309 Def::TyAlias(..) => "type alias",
310 Def::TraitAlias(..) => "trait alias",
311 Def::AssociatedTy(..) => "associated type",
312 Def::AssociatedExistential(..) => "associated existential type",
313 Def::Struct(..) => "struct",
314 Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
315 Def::StructCtor(.., CtorKind::Const) => "unit struct",
316 Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
317 Def::SelfCtor(..) => "self constructor",
318 Def::Union(..) => "union",
319 Def::Trait(..) => "trait",
320 Def::ForeignTy(..) => "foreign type",
321 Def::Method(..) => "method",
322 Def::Const(..) => "constant",
323 Def::AssociatedConst(..) => "associated constant",
324 Def::TyParam(..) => "type parameter",
325 Def::PrimTy(..) => "builtin type",
326 Def::Local(..) => "local variable",
327 Def::Upvar(..) => "closure capture",
328 Def::Label(..) => "label",
329 Def::SelfTy(..) => "self type",
330 Def::Macro(.., macro_kind) => macro_kind.descr(),
331 Def::ToolMod => "tool module",
332 Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
333 Def::Err => "unresolved item",
337 pub fn article(&self) -> &'static str {
339 Def::AssociatedTy(..) | Def::AssociatedConst(..) | Def::AssociatedExistential(..) |
340 Def::Enum(..) | Def::Existential(..) | Def::Err => "an",
341 Def::Macro(.., macro_kind) => macro_kind.article(),