]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/def.rs
Remove `LastPrivate`
[rust.git] / src / librustc / middle / def.rs
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.
4 //
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.
10
11 use middle::def_id::DefId;
12 use middle::subst::ParamSpace;
13 use util::nodemap::NodeMap;
14 use syntax::ast;
15 use rustc_front::hir;
16
17 #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
18 pub enum Def {
19     Fn(DefId),
20     SelfTy(Option<DefId>,                    // trait id
21               Option<(ast::NodeId, ast::NodeId)>),   // (impl id, self type id)
22     Mod(DefId),
23     ForeignMod(DefId),
24     Static(DefId, bool /* is_mutbl */),
25     Const(DefId),
26     AssociatedConst(DefId),
27     Local(DefId, // def id of variable
28              ast::NodeId), // node id of variable
29     Variant(DefId /* enum */, DefId /* variant */),
30     Enum(DefId),
31     TyAlias(DefId),
32     AssociatedTy(DefId /* trait */, DefId),
33     Trait(DefId),
34     PrimTy(hir::PrimTy),
35     TyParam(ParamSpace, u32, DefId, ast::Name),
36     Upvar(DefId,        // def id of closed over local
37              ast::NodeId,  // node id of closed over local
38              usize,        // index in the freevars list of the closure
39              ast::NodeId), // expr node that creates the closure
40
41     // If Def::Struct lives in type namespace it denotes a struct item and its DefId refers
42     // to NodeId of the struct itself.
43     // If Def::Struct lives in value namespace (e.g. tuple struct, unit struct expressions)
44     // it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
45     Struct(DefId),
46     Label(ast::NodeId),
47     Method(DefId),
48     Err,
49 }
50
51 /// The result of resolving a path.
52 /// Before type checking completes, `depth` represents the number of
53 /// trailing segments which are yet unresolved. Afterwards, if there
54 /// were no errors, all paths should be fully resolved, with `depth`
55 /// set to `0` and `base_def` representing the final resolution.
56 ///
57 ///     module::Type::AssocX::AssocY::MethodOrAssocType
58 ///     ^~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
59 ///     base_def      depth = 3
60 ///
61 ///     <T as Trait>::AssocX::AssocY::MethodOrAssocType
62 ///           ^~~~~~~~~~~~~~  ^~~~~~~~~~~~~~~~~~~~~~~~~
63 ///           base_def        depth = 2
64 #[derive(Copy, Clone, Debug)]
65 pub struct PathResolution {
66     pub base_def: Def,
67     pub depth: usize
68 }
69
70 impl PathResolution {
71     /// Get the definition, if fully resolved, otherwise panic.
72     pub fn full_def(&self) -> Def {
73         if self.depth != 0 {
74             panic!("path not fully resolved: {:?}", self);
75         }
76         self.base_def
77     }
78
79     /// Get the DefId, if fully resolved, otherwise panic.
80     pub fn def_id(&self) -> DefId {
81         self.full_def().def_id()
82     }
83
84     pub fn new(base_def: Def,
85                depth: usize)
86                -> PathResolution {
87         PathResolution {
88             base_def: base_def,
89             depth: depth,
90         }
91     }
92 }
93
94 // Definition mapping
95 pub type DefMap = NodeMap<PathResolution>;
96 // This is the replacement export map. It maps a module to all of the exports
97 // within.
98 pub type ExportMap = NodeMap<Vec<Export>>;
99
100 #[derive(Copy, Clone)]
101 pub struct Export {
102     pub name: ast::Name,    // The name of the target.
103     pub def_id: DefId, // The definition of the target.
104 }
105
106 impl Def {
107     pub fn var_id(&self) -> ast::NodeId {
108         match *self {
109             Def::Local(_, id) |
110             Def::Upvar(_, id, _, _) => {
111                 id
112             }
113
114             Def::Fn(..) | Def::Mod(..) | Def::ForeignMod(..) | Def::Static(..) |
115             Def::Variant(..) | Def::Enum(..) | Def::TyAlias(..) | Def::AssociatedTy(..) |
116             Def::TyParam(..) | Def::Struct(..) | Def::Trait(..) |
117             Def::Method(..) | Def::Const(..) | Def::AssociatedConst(..) |
118             Def::PrimTy(..) | Def::Label(..) | Def::SelfTy(..) | Def::Err => {
119                 panic!("attempted .var_id() on invalid {:?}", self)
120             }
121         }
122     }
123
124     pub fn def_id(&self) -> DefId {
125         match *self {
126             Def::Fn(id) | Def::Mod(id) | Def::ForeignMod(id) | Def::Static(id, _) |
127             Def::Variant(_, id) | Def::Enum(id) | Def::TyAlias(id) | Def::AssociatedTy(_, id) |
128             Def::TyParam(_, _, id, _) | Def::Struct(id) | Def::Trait(id) |
129             Def::Method(id) | Def::Const(id) | Def::AssociatedConst(id) |
130             Def::Local(id, _) | Def::Upvar(id, _, _, _) => {
131                 id
132             }
133
134             Def::Label(..)  |
135             Def::PrimTy(..) |
136             Def::SelfTy(..) |
137             Def::Err => {
138                 panic!("attempted .def_id() on invalid def: {:?}", self)
139             }
140         }
141     }
142
143     pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
144         match *self {
145             Def::Variant(enum_id, var_id) => {
146                 Some((enum_id, var_id))
147             }
148             _ => None
149         }
150     }
151
152     pub fn kind_name(&self) -> &'static str {
153         match *self {
154             Def::Fn(..) => "function",
155             Def::Mod(..) => "module",
156             Def::ForeignMod(..) => "foreign module",
157             Def::Static(..) => "static",
158             Def::Variant(..) => "variant",
159             Def::Enum(..) => "enum",
160             Def::TyAlias(..) => "type",
161             Def::AssociatedTy(..) => "associated type",
162             Def::Struct(..) => "struct",
163             Def::Trait(..) => "trait",
164             Def::Method(..) => "method",
165             Def::Const(..) => "const",
166             Def::AssociatedConst(..) => "associated const",
167             Def::TyParam(..) => "type parameter",
168             Def::PrimTy(..) => "builtin type",
169             Def::Local(..) => "local variable",
170             Def::Upvar(..) => "closure capture",
171             Def::Label(..) => "label",
172             Def::SelfTy(..) => "self type",
173             Def::Err => "unresolved item",
174         }
175     }
176 }