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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use std::borrow::Cow;
use std::cell::Cell;
use std::cmp;
use std::rc::Rc;
use std::usize;
use crate::file::FileHash;
use crate::location::{self, FrameLocation, Location, Piece, Register};
use crate::namespace::Namespace;
use crate::range::Range;
use crate::source::Source;
use crate::types::{Type, TypeOffset};
use crate::{Address, Size};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct VariableOffset(usize);
impl VariableOffset {
#[inline]
pub(crate) fn new(offset: usize) -> VariableOffset {
debug_assert!(VariableOffset(offset) != VariableOffset::none());
VariableOffset(offset)
}
#[inline]
pub(crate) fn none() -> VariableOffset {
VariableOffset(usize::MAX)
}
}
impl Default for VariableOffset {
#[inline]
fn default() -> Self {
VariableOffset::none()
}
}
#[derive(Debug, Default)]
pub struct Variable<'input> {
pub(crate) id: Cell<usize>,
pub(crate) offset: VariableOffset,
pub(crate) namespace: Option<Rc<Namespace<'input>>>,
pub(crate) name: Option<&'input str>,
pub(crate) linkage_name: Option<&'input str>,
pub(crate) symbol_name: Option<&'input str>,
pub(crate) ty: TypeOffset,
pub(crate) source: Source<'input>,
pub(crate) address: Address,
pub(crate) size: Size,
pub(crate) declaration: bool,
}
impl<'input> Variable<'input> {
#[inline]
pub fn id(&self) -> usize {
self.id.get()
}
#[inline]
pub fn set_id(&self, id: usize) {
self.id.set(id)
}
pub fn namespace(&self) -> Option<&Namespace> {
self.namespace.as_ref().map(|x| &**x)
}
#[inline]
pub fn name(&self) -> Option<&str> {
self.name
}
#[inline]
pub fn linkage_name(&self) -> Option<&str> {
self.linkage_name
}
#[inline]
pub fn symbol_name(&self) -> Option<&str> {
self.symbol_name
}
#[inline]
pub fn ty<'a>(&self, hash: &'a FileHash<'input>) -> Option<Cow<'a, Type<'input>>> {
Type::from_offset(hash, self.ty)
}
#[inline]
pub fn source(&self) -> &Source<'input> {
&self.source
}
#[inline]
pub fn address(&self) -> Option<u64> {
self.address.get()
}
pub fn byte_size(&self, hash: &FileHash) -> Option<u64> {
if self.size.is_some() {
self.size.get()
} else {
self.ty(hash).and_then(|t| t.byte_size(hash))
}
}
pub fn range(&self, hash: &FileHash) -> Option<Range> {
match (self.address(), self.byte_size(hash)) {
(Some(begin), Some(size)) => {
if size != 0 {
Some(Range {
begin,
end: begin + size,
})
} else {
None
}
}
_ => None,
}
}
#[inline]
pub fn is_declaration(&self) -> bool {
self.declaration
}
pub fn cmp_id(
_hash_a: &FileHash,
a: &Variable,
_hash_b: &FileHash,
b: &Variable,
) -> cmp::Ordering {
Namespace::cmp_ns_and_name(a.namespace(), a.name(), b.namespace(), b.name())
}
}
#[derive(Debug, Default, Clone)]
pub struct LocalVariable<'input> {
pub(crate) offset: VariableOffset,
pub(crate) name: Option<&'input str>,
pub(crate) ty: TypeOffset,
pub(crate) source: Source<'input>,
pub(crate) address: Address,
pub(crate) size: Size,
pub(crate) locations: Vec<(Range, Piece)>,
}
impl<'input> LocalVariable<'input> {
#[inline]
pub fn name(&self) -> Option<&'input str> {
self.name
}
#[inline]
pub fn type_offset(&self) -> TypeOffset {
self.ty
}
#[inline]
pub fn ty<'a>(&self, hash: &'a FileHash<'input>) -> Option<Cow<'a, Type<'input>>> {
Type::from_offset(hash, self.ty)
}
#[inline]
pub fn source(&self) -> &Source<'input> {
&self.source
}
#[inline]
pub fn address(&self) -> Option<u64> {
self.address.get()
}
pub fn byte_size(&self, hash: &FileHash) -> Option<u64> {
if self.size.is_some() {
self.size.get()
} else {
self.ty(hash).and_then(|t| t.byte_size(hash))
}
}
pub fn registers<'a>(&'a self) -> impl Iterator<Item = (Range, Register)> + 'a {
location::registers(&self.locations)
}
pub fn register_offsets<'a>(&'a self) -> impl Iterator<Item = (Range, Register, i64)> + 'a {
location::register_offsets(&self.locations)
}
pub fn frame_locations<'a>(&'a self) -> impl Iterator<Item = FrameLocation> + 'a {
self.locations.iter().filter_map(|(_, piece)| {
if piece.is_value {
return None;
}
match piece.location {
Location::FrameOffset { offset } | Location::CfaOffset { offset } => {
Some(FrameLocation {
offset,
bit_size: piece.bit_size,
})
}
_ => None,
}
})
}
pub fn cmp_id(_hash_a: &FileHash, a: &Self, _hash_b: &FileHash, b: &Self) -> cmp::Ordering {
a.name.cmp(&b.name)
}
}