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
use super::*;
#[derive(Debug, Clone, PartialEq)]
pub struct Typedef<'a> {
pub target: BasicType<'a>,
pub alias: ArrayType<BasicType<'a>>,
}
impl<'a> Typedef<'a> {
pub fn new(mut vs: Vec<Node<'a>>) -> Self {
// Extract the target type
let target = match vs.remove(0) {
Node::Type(t) => t,
_ => unreachable!("incorrect type in typedef"),
};
// Extract the defined alias
let alias = match vs.remove(0) {
Node::Type(t) => t,
_ => unreachable!("incorrect type in typedef"),
};
// Optionally, extract the array definition
let alias = if vs.len() > 0 {
match vs.remove(0) {
Node::ArrayFixed(s) => ArrayType::FixedSize(alias, ArraySize::from(s)),
// Typedefs to opaque types include a variable array identifier so the
// caller knows to read the length prefix bytes. This is already handled
// by the opaque reader however, so map this to a "no array" wrapper.
Node::ArrayVariable(_) if target.is_opaque() => ArrayType::None(alias),
Node::ArrayVariable(s) => ArrayType::VariableSize(
alias,
match s.trim() {
"" => None,
s => Some(ArraySize::from(s)),
},
),
t => unreachable!("incorrect type in typedef {:?}", t),
}
} else {
ArrayType::None(alias)
};
Self {
target: target,
alias: alias,
}
}
}