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;
16 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
18 // Constructor function automatically created by a tuple struct/variant.
20 // Constructor constant automatically created by a unit struct/variant.
22 // Unusable name in value namespace created by a struct variant.
26 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
30 Struct(DefId), // DefId refers to NodeId of the struct itself
39 SelfTy(Option<DefId> /* trait */, Option<DefId> /* impl */),
44 Static(DefId, bool /* is_mutbl */),
45 StructCtor(DefId, CtorKind), // DefId refers to NodeId of the struct's constructor
46 VariantCtor(DefId, CtorKind),
48 AssociatedConst(DefId),
50 Upvar(DefId, // def id of closed over local
51 usize, // index in the freevars list of the closure
52 ast::NodeId), // expr node that creates the closure
62 /// The result of resolving a path.
63 /// Before type checking completes, `depth` represents the number of
64 /// trailing segments which are yet unresolved. Afterwards, if there
65 /// were no errors, all paths should be fully resolved, with `depth`
66 /// set to `0` and `base_def` representing the final resolution.
68 /// module::Type::AssocX::AssocY::MethodOrAssocType
69 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 /// base_def depth = 3
72 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
73 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
74 /// base_def depth = 2
75 #[derive(Copy, Clone, Debug)]
76 pub struct PathResolution {
82 pub fn new(def: Def) -> PathResolution {
83 PathResolution { base_def: def, depth: 0 }
86 /// Get the definition, if fully resolved, otherwise panic.
87 pub fn full_def(&self) -> Def {
89 bug!("path not fully resolved: {:?}", self);
94 pub fn kind_name(&self) -> &'static str {
98 self.base_def.kind_name()
103 // Definition mapping
104 pub type DefMap = NodeMap<PathResolution>;
105 // This is the replacement export map. It maps a module to all of the exports
107 pub type ExportMap = NodeMap<Vec<Export>>;
109 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
111 pub name: ast::Name, // The name of the target.
112 pub def: Def, // The definition of the target.
116 pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
118 ast::VariantData::Tuple(..) => CtorKind::Fn,
119 ast::VariantData::Unit(..) => CtorKind::Const,
120 ast::VariantData::Struct(..) => CtorKind::Fictive,
123 pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
125 hir::VariantData::Tuple(..) => CtorKind::Fn,
126 hir::VariantData::Unit(..) => CtorKind::Const,
127 hir::VariantData::Struct(..) => CtorKind::Fictive,
133 pub fn def_id(&self) -> DefId {
135 Def::Fn(id) | Def::Mod(id) | Def::Static(id, _) |
136 Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
137 Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
138 Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
139 Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id) => {
147 bug!("attempted .def_id() on invalid def: {:?}", self)
152 pub fn kind_name(&self) -> &'static str {
154 Def::Fn(..) => "function",
155 Def::Mod(..) => "module",
156 Def::Static(..) => "static",
157 Def::Variant(..) => "variant",
158 Def::VariantCtor(.., CtorKind::Fn) => "tuple variant",
159 Def::VariantCtor(.., CtorKind::Const) => "unit variant",
160 Def::VariantCtor(.., CtorKind::Fictive) => "struct variant",
161 Def::Enum(..) => "enum",
162 Def::TyAlias(..) => "type alias",
163 Def::AssociatedTy(..) => "associated type",
164 Def::Struct(..) => "struct",
165 Def::StructCtor(.., CtorKind::Fn) => "tuple struct",
166 Def::StructCtor(.., CtorKind::Const) => "unit struct",
167 Def::StructCtor(.., CtorKind::Fictive) => bug!("impossible struct constructor"),
168 Def::Union(..) => "union",
169 Def::Trait(..) => "trait",
170 Def::Method(..) => "method",
171 Def::Const(..) => "constant",
172 Def::AssociatedConst(..) => "associated constant",
173 Def::TyParam(..) => "type parameter",
174 Def::PrimTy(..) => "builtin type",
175 Def::Local(..) => "local variable",
176 Def::Upvar(..) => "closure capture",
177 Def::Label(..) => "label",
178 Def::SelfTy(..) => "self type",
179 Def::Macro(..) => "macro",
180 Def::Err => "unresolved item",