1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
pub use self::Def::*;
pub use self::MethodProvenance::*;
use middle::privacy::LastPrivate;
use middle::subst::ParamSpace;
use util::nodemap::NodeMap;
use syntax::ast;
use syntax::ast_util::local_def;
use std::cell::RefCell;
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Def {
DefFn(ast::DefId, bool ),
DefSelfTy(Option<ast::DefId>,
Option<(ast::NodeId, ast::NodeId)>),
DefMod(ast::DefId),
DefForeignMod(ast::DefId),
DefStatic(ast::DefId, bool ),
DefConst(ast::DefId),
DefLocal(ast::NodeId),
DefVariant(ast::DefId , ast::DefId , bool ),
DefTy(ast::DefId, bool ),
DefAssociatedTy(ast::DefId , ast::DefId),
DefTrait(ast::DefId),
DefPrimTy(ast::PrimTy),
DefTyParam(ParamSpace, u32, ast::DefId, ast::Name),
DefUse(ast::DefId),
DefUpvar(ast::NodeId,
ast::NodeId),
DefStruct(ast::DefId),
DefRegion(ast::NodeId),
DefLabel(ast::NodeId),
DefMethod(ast::DefId , MethodProvenance),
}
#[derive(Copy, Clone, Debug)]
pub struct PathResolution {
pub base_def: Def,
pub last_private: LastPrivate,
pub depth: usize
}
impl PathResolution {
pub fn full_def(&self) -> Def {
if self.depth != 0 {
panic!("path not fully resolved: {:?}", self);
}
self.base_def
}
pub fn def_id(&self) -> ast::DefId {
self.full_def().def_id()
}
pub fn new(base_def: Def,
last_private: LastPrivate,
depth: usize)
-> PathResolution {
PathResolution {
base_def: base_def,
last_private: last_private,
depth: depth,
}
}
}
pub type DefMap = RefCell<NodeMap<PathResolution>>;
pub type ExportMap = NodeMap<Vec<Export>>;
#[derive(Copy, Clone)]
pub struct Export {
pub name: ast::Name,
pub def_id: ast::DefId,
}
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum MethodProvenance {
FromTrait(ast::DefId),
FromImpl(ast::DefId),
}
impl MethodProvenance {
pub fn map<F>(self, f: F) -> MethodProvenance where
F: FnOnce(ast::DefId) -> ast::DefId,
{
match self {
FromTrait(did) => FromTrait(f(did)),
FromImpl(did) => FromImpl(f(did))
}
}
}
impl Def {
pub fn local_node_id(&self) -> ast::NodeId {
let def_id = self.def_id();
assert_eq!(def_id.krate, ast::LOCAL_CRATE);
def_id.node
}
pub fn def_id(&self) -> ast::DefId {
match *self {
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
DefTyParam(_, _, id, _) | DefUse(id) | DefStruct(id) | DefTrait(id) |
DefMethod(id, _) | DefConst(id) | DefSelfTy(Some(id), None)=> {
id
}
DefLocal(id) |
DefUpvar(id, _) |
DefRegion(id) |
DefLabel(id) |
DefSelfTy(_, Some((_, id))) => {
local_def(id)
}
DefPrimTy(_) => panic!("attempted .def_id() on DefPrimTy"),
DefSelfTy(..) => panic!("attempted .def_id() on invalid DefSelfTy"),
}
}
pub fn variant_def_ids(&self) -> Option<(ast::DefId, ast::DefId)> {
match *self {
DefVariant(enum_id, var_id, _) => {
Some((enum_id, var_id))
}
_ => None
}
}
}