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 not registered in any way (`#[my_attr]`).
43 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
47 Struct(DefId), // DefId refers to NodeId of the struct itself
52 /// `existential type Foo: Bar;`
59 /// `existential type Foo: Bar;`
60 AssociatedExistential(DefId),
63 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
64 ToolMod, // e.g. `rustfmt` in `#[rustfmt::skip]`
69 Static(DefId, bool /* is_mutbl */),
70 StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
71 VariantCtor(DefId, CtorKind),
73 AssociatedConst(DefId),
76 Upvar(ast::NodeId, // node id of closed over local
77 usize, // index in the freevars list of the closure
78 ast::NodeId), // expr node that creates the closure
82 Macro(DefId, MacroKind),
83 NonMacroAttr(NonMacroAttrKind), // e.g. `#[inline]` or `#[rustfmt::skip]`
89 /// The result of resolving a path before lowering to HIR.
90 /// `base_def` is definition of resolved part of the
91 /// path, `unresolved_segments` is the number of unresolved
95 /// module::Type::AssocX::AssocY::MethodOrAssocType
96 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97 /// base_def unresolved_segments = 3
99 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
100 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
101 /// base_def unresolved_segments = 2
103 #[derive(Copy, Clone, Debug)]
104 pub struct PathResolution {
106 unresolved_segments: usize,
109 impl PathResolution {
110 pub fn new(def: Def) -> Self {
111 PathResolution { base_def: def, unresolved_segments: 0 }
114 pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self {
115 if def == Def::Err { unresolved_segments = 0 }
116 PathResolution { base_def: def, unresolved_segments: unresolved_segments }
120 pub fn base_def(&self) -> Def {
125 pub fn unresolved_segments(&self) -> usize {
126 self.unresolved_segments
129 pub fn kind_name(&self) -> &'static str {
130 if self.unresolved_segments != 0 {
133 self.base_def.kind_name()
138 /// Different kinds of symbols don't influence each other.
140 /// Therefore, they have a separate universe (namespace).
141 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
149 pub fn descr(self) -> &'static str {
158 /// Just a helper ‒ separate structure for each namespace.
159 #[derive(Copy, Clone, Default, Debug)]
160 pub struct PerNS<T> {
167 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
169 value_ns: f(self.value_ns),
170 type_ns: f(self.type_ns),
171 macro_ns: f(self.macro_ns),
176 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
178 fn index(&self, ns: Namespace) -> &T {
180 ValueNS => &self.value_ns,
181 TypeNS => &self.type_ns,
182 MacroNS => &self.macro_ns,
187 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
188 fn index_mut(&mut self, ns: Namespace) -> &mut T {
190 ValueNS => &mut self.value_ns,
191 TypeNS => &mut self.type_ns,
192 MacroNS => &mut self.macro_ns,
197 impl<T> PerNS<Option<T>> {
198 /// Returns whether all the items in this collection are `None`.
199 pub fn is_empty(&self) -> bool {
200 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
203 /// Returns an iterator over the items which are `Some`.
204 pub fn present_items(self) -> impl Iterator<Item=T> {
208 .chain(once(self.value_ns))
209 .chain(once(self.macro_ns))
214 /// Definition mapping
215 pub type DefMap = NodeMap<PathResolution>;
217 /// This is the replacement export map. It maps a module to all of the exports
219 pub type ExportMap = DefIdMap<Vec<Export>>;
221 /// Map used to track the `use` statements within a scope, matching it with all the items in every
223 pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;
225 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
227 /// The name of the target.
228 pub ident: ast::Ident,
229 /// The definition of the target.
231 /// The span of the target definition.
233 /// The visibility of the export.
234 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
235 pub vis: ty::Visibility,
239 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
241 ast::VariantData::Tuple(..) => CtorKind::Fn,
242 ast::VariantData::Unit(..) => CtorKind::Const,
243 ast::VariantData::Struct(..) => CtorKind::Fictive,
246 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
248 hir::VariantData::Tuple(..) => CtorKind::Fn,
249 hir::VariantData::Unit(..) => CtorKind::Const,
250 hir::VariantData::Struct(..) => CtorKind::Fictive,
255 impl NonMacroAttrKind {
256 fn descr(self) -> &'static str {
258 NonMacroAttrKind::Builtin => "built-in attribute",
259 NonMacroAttrKind::Tool => "tool attribute",
260 NonMacroAttrKind::DeriveHelper => "derive helper attribute",
261 NonMacroAttrKind::Custom => "custom attribute",
267 pub fn def_id(&self) -> DefId {
269 Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
270 Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
271 Def::TyAlias(id) | Def::TraitAlias(id) |
272 Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
273 Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
274 Def::AssociatedConst(id) | Def::Macro(id, ..) |
275 Def::Existential(id) | Def::AssociatedExistential(id) | Def::TyForeign(id) => {
285 Def::NonMacroAttr(..) |
287 bug!("attempted .def_id() on invalid def: {:?}", self)
292 /// A human readable kind name
293 pub fn kind_name(&self) -> &'static str {
295 Def::Fn(..) => "function",
296 Def::Mod(..) => "module",
297 Def::Static(..) => "static",
298 Def::Variant(..) => "variant",
299 Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
300 Def::VariantCtor(.., CtorKind::Const) => "unit variant",
301 Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
302 Def::Enum(..) => "enum",
303 Def::Existential(..) => "existential type",
304 Def::TyAlias(..) => "type alias",
305 Def::TraitAlias(..) => "trait alias",
306 Def::AssociatedTy(..) => "associated type",
307 Def::AssociatedExistential(..) => "associated existential type",
308 Def::Struct(..) => "struct",
309 Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
310 Def::StructCtor(.., CtorKind::Const) => "unit struct",
311 Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
312 Def::Union(..) => "union",
313 Def::Trait(..) => "trait",
314 Def::TyForeign(..) => "foreign type",
315 Def::Method(..) => "method",
316 Def::Const(..) => "constant",
317 Def::AssociatedConst(..) => "associated constant",
318 Def::TyParam(..) => "type parameter",
319 Def::PrimTy(..) => "builtin type",
320 Def::Local(..) => "local variable",
321 Def::Upvar(..) => "closure capture",
322 Def::Label(..) => "label",
323 Def::SelfTy(..) => "self type",
324 Def::Macro(.., macro_kind) => macro_kind.descr(),
325 Def::ToolMod => "tool module",
326 Def::NonMacroAttr(attr_kind) => attr_kind.descr(),
327 Def::Err => "unresolved item",