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)]
35 Struct(DefId), // DefId refers to NodeId of the struct itself
40 /// `existential type Foo: Bar;`
47 /// `existential type Foo: Bar;`
48 AssociatedExistential(DefId),
51 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
52 ToolMod, // e.g. `rustfmt` in `#[rustfmt::skip]`
57 Static(DefId, bool /* is_mutbl */),
58 StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
59 VariantCtor(DefId, CtorKind),
61 AssociatedConst(DefId),
64 Upvar(ast::NodeId, // node id of closed over local
65 usize, // index in the freevars list of the closure
66 ast::NodeId), // expr node that creates the closure
70 Macro(DefId, MacroKind),
71 NonMacroAttr, // e.g. `#[inline]` or `#[rustfmt::skip]`
79 /// The result of resolving a path before lowering to HIR.
80 /// `base_def` is definition of resolved part of the
81 /// path, `unresolved_segments` is the number of unresolved
85 /// module::Type::AssocX::AssocY::MethodOrAssocType
86 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
87 /// base_def unresolved_segments = 3
89 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
90 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
91 /// base_def unresolved_segments = 2
93 #[derive(Copy, Clone, Debug)]
94 pub struct PathResolution {
96 unresolved_segments: usize,
100 pub fn new(def: Def) -> Self {
101 PathResolution { base_def: def, unresolved_segments: 0 }
104 pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self {
105 if def == Def::Err { unresolved_segments = 0 }
106 PathResolution { base_def: def, unresolved_segments: unresolved_segments }
110 pub fn base_def(&self) -> Def {
115 pub fn unresolved_segments(&self) -> usize {
116 self.unresolved_segments
119 pub fn kind_name(&self) -> &'static str {
120 if self.unresolved_segments != 0 {
123 self.base_def.kind_name()
128 /// Different kinds of symbols don't influence each other.
130 /// Therefore, they have a separate universe (namespace).
131 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
139 pub fn descr(self) -> &'static str {
148 /// Just a helper ‒ separate structure for each namespace.
149 #[derive(Copy, Clone, Default, Debug)]
150 pub struct PerNS<T> {
157 pub fn map<U, F: FnMut(T) -> U>(self, mut f: F) -> PerNS<U> {
159 value_ns: f(self.value_ns),
160 type_ns: f(self.type_ns),
161 macro_ns: f(self.macro_ns),
166 impl<T> ::std::ops::Index<Namespace> for PerNS<T> {
168 fn index(&self, ns: Namespace) -> &T {
170 ValueNS => &self.value_ns,
171 TypeNS => &self.type_ns,
172 MacroNS => &self.macro_ns,
177 impl<T> ::std::ops::IndexMut<Namespace> for PerNS<T> {
178 fn index_mut(&mut self, ns: Namespace) -> &mut T {
180 ValueNS => &mut self.value_ns,
181 TypeNS => &mut self.type_ns,
182 MacroNS => &mut self.macro_ns,
187 impl<T> PerNS<Option<T>> {
188 /// Returns whether all the items in this collection are `None`.
189 pub fn is_empty(&self) -> bool {
190 self.type_ns.is_none() && self.value_ns.is_none() && self.macro_ns.is_none()
193 /// Returns an iterator over the items which are `Some`.
194 pub fn present_items(self) -> impl Iterator<Item=T> {
198 .chain(once(self.value_ns))
199 .chain(once(self.macro_ns))
204 /// Definition mapping
205 pub type DefMap = NodeMap<PathResolution>;
207 /// This is the replacement export map. It maps a module to all of the exports
209 pub type ExportMap = DefIdMap<Vec<Export>>;
211 /// Map used to track the `use` statements within a scope, matching it with all the items in every
213 pub type ImportMap = NodeMap<PerNS<Option<PathResolution>>>;
215 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
217 /// The name of the target.
218 pub ident: ast::Ident,
219 /// The definition of the target.
221 /// The span of the target definition.
223 /// The visibility of the export.
224 /// We include non-`pub` exports for hygienic macros that get used from extern crates.
225 pub vis: ty::Visibility,
229 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
231 ast::VariantData::Tuple(..) => CtorKind::Fn,
232 ast::VariantData::Unit(..) => CtorKind::Const,
233 ast::VariantData::Struct(..) => CtorKind::Fictive,
236 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
238 hir::VariantData::Tuple(..) => CtorKind::Fn,
239 hir::VariantData::Unit(..) => CtorKind::Const,
240 hir::VariantData::Struct(..) => CtorKind::Fictive,
246 pub fn def_id(&self) -> DefId {
248 Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
249 Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) |
250 Def::TyAlias(id) | Def::TraitAlias(id) |
251 Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
252 Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
253 Def::AssociatedConst(id) | Def::Macro(id, ..) |
254 Def::Existential(id) | Def::AssociatedExistential(id) |
255 Def::GlobalAsm(id) | Def::TyForeign(id) => {
267 bug!("attempted .def_id() on invalid def: {:?}", self)
272 /// A human readable kind name
273 pub fn kind_name(&self) -> &'static str {
275 Def::Fn(..) => "function",
276 Def::Mod(..) => "module",
277 Def::Static(..) => "static",
278 Def::Variant(..) => "variant",
279 Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
280 Def::VariantCtor(.., CtorKind::Const) => "unit variant",
281 Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
282 Def::Enum(..) => "enum",
283 Def::Existential(..) => "existential type",
284 Def::TyAlias(..) => "type alias",
285 Def::TraitAlias(..) => "trait alias",
286 Def::AssociatedTy(..) => "associated type",
287 Def::AssociatedExistential(..) => "associated existential type",
288 Def::Struct(..) => "struct",
289 Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
290 Def::StructCtor(.., CtorKind::Const) => "unit struct",
291 Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
292 Def::Union(..) => "union",
293 Def::Trait(..) => "trait",
294 Def::TyForeign(..) => "foreign type",
295 Def::Method(..) => "method",
296 Def::Const(..) => "constant",
297 Def::AssociatedConst(..) => "associated constant",
298 Def::TyParam(..) => "type parameter",
299 Def::PrimTy(..) => "builtin type",
300 Def::Local(..) => "local variable",
301 Def::Upvar(..) => "closure capture",
302 Def::Label(..) => "label",
303 Def::SelfTy(..) => "self type",
304 Def::Macro(.., macro_kind) => macro_kind.descr(),
305 Def::GlobalAsm(..) => "global asm",
306 Def::ToolMod => "tool module",
307 Def::NonMacroAttr => "non-macro attribute",
308 Def::Err => "unresolved item",