]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | ||
b039eaaf | 11 | use middle::def_id::DefId; |
c34b1796 | 12 | use middle::privacy::LastPrivate; |
1a4d82fc | 13 | use middle::subst::ParamSpace; |
1a4d82fc JJ |
14 | use util::nodemap::NodeMap; |
15 | use syntax::ast; | |
e9174d1e | 16 | use rustc_front::hir; |
1a4d82fc | 17 | |
85aaf69f | 18 | #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] |
1a4d82fc | 19 | pub enum Def { |
7453a54e SL |
20 | Fn(DefId), |
21 | SelfTy(Option<DefId>, // trait id | |
9346a6ac | 22 | Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id) |
7453a54e SL |
23 | Mod(DefId), |
24 | ForeignMod(DefId), | |
25 | Static(DefId, bool /* is_mutbl */), | |
26 | Const(DefId), | |
27 | AssociatedConst(DefId), | |
28 | Local(DefId, // def id of variable | |
b039eaaf | 29 | ast::NodeId), // node id of variable |
7453a54e SL |
30 | Variant(DefId /* enum */, DefId /* variant */), |
31 | Enum(DefId), | |
32 | TyAlias(DefId), | |
33 | AssociatedTy(DefId /* trait */, DefId), | |
34 | Trait(DefId), | |
35 | PrimTy(hir::PrimTy), | |
36 | TyParam(ParamSpace, u32, DefId, ast::Name), | |
37 | Upvar(DefId, // def id of closed over local | |
b039eaaf | 38 | ast::NodeId, // node id of closed over local |
e9174d1e | 39 | usize, // index in the freevars list of the closure |
85aaf69f | 40 | ast::NodeId), // expr node that creates the closure |
1a4d82fc | 41 | |
7453a54e SL |
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. | |
46 | Struct(DefId), | |
47 | Label(ast::NodeId), | |
48 | Method(DefId), | |
49 | Err, | |
c34b1796 AL |
50 | } |
51 | ||
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. | |
57 | /// | |
58 | /// module::Type::AssocX::AssocY::MethodOrAssocType | |
59 | /// ^~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
60 | /// base_def depth = 3 | |
61 | /// | |
62 | /// <T as Trait>::AssocX::AssocY::MethodOrAssocType | |
63 | /// ^~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~ | |
64 | /// base_def depth = 2 | |
65 | #[derive(Copy, Clone, Debug)] | |
66 | pub struct PathResolution { | |
67 | pub base_def: Def, | |
68 | pub last_private: LastPrivate, | |
69 | pub depth: usize | |
70 | } | |
71 | ||
72 | impl PathResolution { | |
73 | /// Get the definition, if fully resolved, otherwise panic. | |
74 | pub fn full_def(&self) -> Def { | |
75 | if self.depth != 0 { | |
76 | panic!("path not fully resolved: {:?}", self); | |
77 | } | |
78 | self.base_def | |
79 | } | |
80 | ||
81 | /// Get the DefId, if fully resolved, otherwise panic. | |
e9174d1e | 82 | pub fn def_id(&self) -> DefId { |
c34b1796 AL |
83 | self.full_def().def_id() |
84 | } | |
85 | ||
86 | pub fn new(base_def: Def, | |
87 | last_private: LastPrivate, | |
88 | depth: usize) | |
89 | -> PathResolution { | |
90 | PathResolution { | |
91 | base_def: base_def, | |
92 | last_private: last_private, | |
93 | depth: depth, | |
94 | } | |
95 | } | |
1a4d82fc JJ |
96 | } |
97 | ||
98 | // Definition mapping | |
92a42be0 | 99 | pub type DefMap = NodeMap<PathResolution>; |
1a4d82fc JJ |
100 | // This is the replacement export map. It maps a module to all of the exports |
101 | // within. | |
102 | pub type ExportMap = NodeMap<Vec<Export>>; | |
103 | ||
c34b1796 | 104 | #[derive(Copy, Clone)] |
1a4d82fc JJ |
105 | pub struct Export { |
106 | pub name: ast::Name, // The name of the target. | |
e9174d1e | 107 | pub def_id: DefId, // The definition of the target. |
1a4d82fc JJ |
108 | } |
109 | ||
1a4d82fc | 110 | impl Def { |
b039eaaf SL |
111 | pub fn var_id(&self) -> ast::NodeId { |
112 | match *self { | |
7453a54e SL |
113 | Def::Local(_, id) | |
114 | Def::Upvar(_, id, _, _) => { | |
b039eaaf SL |
115 | id |
116 | } | |
117 | ||
7453a54e SL |
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) | |
b039eaaf SL |
124 | } |
125 | } | |
1a4d82fc JJ |
126 | } |
127 | ||
e9174d1e | 128 | pub fn def_id(&self) -> DefId { |
1a4d82fc | 129 | match *self { |
7453a54e SL |
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, _, _, _) => { | |
1a4d82fc JJ |
135 | id |
136 | } | |
1a4d82fc | 137 | |
7453a54e SL |
138 | Def::Label(..) | |
139 | Def::PrimTy(..) | | |
140 | Def::SelfTy(..) | | |
141 | Def::Err => { | |
b039eaaf SL |
142 | panic!("attempted .def_id() on invalid def: {:?}", self) |
143 | } | |
1a4d82fc JJ |
144 | } |
145 | } | |
146 | ||
e9174d1e | 147 | pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> { |
1a4d82fc | 148 | match *self { |
7453a54e | 149 | Def::Variant(enum_id, var_id) => { |
1a4d82fc JJ |
150 | Some((enum_id, var_id)) |
151 | } | |
152 | _ => None | |
153 | } | |
154 | } | |
155 | } |