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;
14 use syntax::ext::base::MacroKind;
18 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
20 // Constructor function automatically created by a tuple struct/variant.
22 // Constructor constant automatically created by a unit struct/variant.
24 // Unusable name in value namespace created by a struct variant.
28 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
32 Struct(DefId), // DefId refers to NodeId of the struct itself
41 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
46 Static(DefId, bool /* is_mutbl */),
47 StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
48 VariantCtor(DefId, CtorKind),
50 AssociatedConst(DefId),
52 Upvar(DefId, // def id of closed over local
53 usize, // index in the freevars list of the closure
54 ast::NodeId), // expr node that creates the closure
58 Macro(DefId, MacroKind),
66 /// The result of resolving a path before lowering to HIR.
67 /// `base_def` is definition of resolved part of the
68 /// path, `unresolved_segments` is the number of unresolved
70 /// module::Type::AssocX::AssocY::MethodOrAssocType
71 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
72 /// base_def unresolved_segments = 3
74 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
75 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
76 /// base_def unresolved_segments = 2
77 #[derive(Copy, Clone, Debug)]
78 pub struct PathResolution {
80 unresolved_segments: usize,
84 pub fn new(def: Def) -> Self {
85 PathResolution { base_def: def, unresolved_segments: 0 }
88 pub fn with_unresolved_segments(def: Def, mut unresolved_segments: usize) -> Self {
89 if def == Def::Err { unresolved_segments = 0 }
90 PathResolution { base_def: def, unresolved_segments: unresolved_segments }
94 pub fn base_def(&self) -> Def {
99 pub fn unresolved_segments(&self) -> usize {
100 self.unresolved_segments
103 pub fn kind_name(&self) -> &'static str {
104 if self.unresolved_segments != 0 {
107 self.base_def.kind_name()
112 // Definition mapping
113 pub type DefMap = NodeMap<PathResolution>;
114 // This is the replacement export map. It maps a module to all of the exports
116 pub type ExportMap = NodeMap<Vec<Export>>;
118 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
120 pub ident: ast::Ident, // The name of the target.
121 pub def: Def, // The definition of the target.
122 pub span: Span, // The span of the target definition.
126 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
128 ast::VariantData::Tuple(..) => CtorKind::Fn,
129 ast::VariantData::Unit(..) => CtorKind::Const,
130 ast::VariantData::Struct(..) => CtorKind::Fictive,
133 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
135 hir::VariantData::Tuple(..) => CtorKind::Fn,
136 hir::VariantData::Unit(..) => CtorKind::Const,
137 hir::VariantData::Struct(..) => CtorKind::Fictive,
143 pub fn def_id(&self) -> DefId {
145 Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
146 Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
147 Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
148 Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
149 Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) |
150 Def::GlobalAsm(id) => {
158 bug!("attempted .def_id() on invalid def: {:?}", self)
163 pub fn kind_name(&self) -> &'static str {
165 Def::Fn(..) => "function",
166 Def::Mod(..) => "module",
167 Def::Static(..) => "static",
168 Def::Variant(..) => "variant",
169 Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
170 Def::VariantCtor(.., CtorKind::Const) => "unit variant",
171 Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
172 Def::Enum(..) => "enum",
173 Def::TyAlias(..) => "type alias",
174 Def::AssociatedTy(..) => "associated type",
175 Def::Struct(..) => "struct",
176 Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
177 Def::StructCtor(.., CtorKind::Const) => "unit struct",
178 Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
179 Def::Union(..) => "union",
180 Def::Trait(..) => "trait",
181 Def::Method(..) => "method",
182 Def::Const(..) => "constant",
183 Def::AssociatedConst(..) => "associated constant",
184 Def::TyParam(..) => "type parameter",
185 Def::PrimTy(..) => "builtin type",
186 Def::Local(..) => "local variable",
187 Def::Upvar(..) => "closure capture",
188 Def::Label(..) => "label",
189 Def::SelfTy(..) => "self type",
190 Def::Macro(..) => "macro",
191 Def::GlobalAsm(..) => "global asm",
192 Def::Err => "unresolved item",