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 middle::def_id::DefId;
12 use middle::privacy::LastPrivate;
13 use middle::subst::ParamSpace;
14 use util::nodemap::NodeMap;
18 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
21 SelfTy(Option<DefId>, // trait id
22 Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id)
25 Static(DefId, bool /* is_mutbl */),
27 AssociatedConst(DefId),
28 Local(DefId, // def id of variable
29 ast::NodeId), // node id of variable
30 Variant(DefId /* enum */, DefId /* variant */),
33 AssociatedTy(DefId /* trait */, DefId),
36 TyParam(ParamSpace, u32, DefId, ast::Name),
37 Upvar(DefId, // def id of closed over local
38 ast::NodeId, // node id of closed over local
39 usize, // index in the freevars list of the closure
40 ast::NodeId), // expr node that creates the closure
42 // If Def::Struct lives in type namespace it denotes a struct item and its DefId refers
43 // to NodeId of the struct itself.
44 // If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
45 // it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
52 /// The result of resolving a path.
53 /// Before type checking completes, `depth` represents the number of
54 /// trailing segments which are yet unresolved. Afterwards, if there
55 /// were no errors, all paths should be fully resolved, with `depth`
56 /// set to `0` and `base_def` representing the final resolution.
58 /// module::Type::AssocX::AssocY::MethodOrAssocType
59 /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 /// base_def depth = 3
62 /// <T as Trait>::AssocX::AssocY::MethodOrAssocType
63 /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~
64 /// base_def depth = 2
65 #[derive(Copy, Clone, Debug)]
66 pub struct PathResolution {
68 pub last_private: LastPrivate,
73 /// Get the definition, if fully resolved, otherwise panic.
74 pub fn full_def(&self) -> Def {
76 panic!("path not fully resolved: {:?}", self);
81 /// Get the DefId, if fully resolved, otherwise panic.
82 pub fn def_id(&self) -> DefId {
83 self.full_def().def_id()
86 pub fn new(base_def: Def,
87 last_private: LastPrivate,
92 last_private: last_private,
99 pub type DefMap = NodeMap<PathResolution>;
100 // This is the replacement export map. It maps a module to all of the exports
102 pub type ExportMap = NodeMap<Vec<Export>>;
104 #[derive(Copy, Clone)]
106 pub name: ast::Name, // The name of the target.
107 pub def_id: DefId, // The definition of the target.
111 pub fn var_id(&self) -> ast::NodeId {
114 Def::Upvar(_, id, _, _) => {
118 Def::Fn(..) | Def::Mod(..) | Def::ForeignMod(..) | Def::Static(..) |
119 Def::Variant(..) | Def::Enum(..) | Def::TyAlias(..) | Def::AssociatedTy(..) |
120 Def::TyParam(..) | Def::Struct(..) | Def::Trait(..) |
121 Def::Method(..) | Def::Const(..) | Def::AssociatedConst(..) |
122 Def::PrimTy(..) | Def::Label(..) | Def::SelfTy(..) | Def::Err => {
123 panic!("attempted .var_id() on invalid {:?}", self)
128 pub fn def_id(&self) -> DefId {
130 Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
131 Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
132 Def::TyParam(_, _, id, _) | Def::Struct(id) | Def::Trait(id) |
133 Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
134 Def::Local(id, _) | Def::Upvar(id, _, _, _) => {
142 panic!("attempted .def_id() on invalid def: {:?}", self)
147 pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
149 Def::Variant(enum_id, var_id) => {
150 Some((enum_id, var_id))
156 pub fn kind_name(&self) -> &'static str {
158 Def::Fn(..) => "function",
159 Def::Mod(..) => "module",
160 Def::ForeignMod(..) => "foreign module",
161 Def::Static(..) => "static",
162 Def::Variant(..) => "variant",
163 Def::Enum(..) => "enum",
164 Def::TyAlias(..) => "type",
165 Def::AssociatedTy(..) => "associated type",
166 Def::Struct(..) => "struct",
167 Def::Trait(..) => "trait",
168 Def::Method(..) => "method",
169 Def::Const(..) => "const",
170 Def::AssociatedConst(..) => "associated const",
171 Def::TyParam(..) => "type parameter",
172 Def::PrimTy(..) => "builtin type",
173 Def::Local(..) => "local variable",
174 Def::Upvar(..) => "closure capture",
175 Def::Label(..) => "label",
176 Def::SelfTy(..) => "self type",
177 Def::Err => "unresolved item",