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
use scroll::ctx::TryFromCtx;
use crate::common::*;
use crate::tpi::constants::*;
#[inline]
fn parse_optional_id_index(buf: &mut ParseBuffer<'_>) -> Result<Option<IdIndex>> {
Ok(match buf.parse()? {
IdIndex(0) => None,
index => Some(index),
})
}
#[inline]
fn parse_string<'t>(leaf: u16, buf: &mut ParseBuffer<'t>) -> Result<RawString<'t>> {
if leaf > LF_ST_MAX {
buf.parse_cstring()
} else {
buf.parse_u8_pascal_string()
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IdData<'t> {
Function(FunctionId<'t>),
MemberFunction(MemberFunctionId<'t>),
BuildInfo(BuildInfoId),
StringList(StringListId),
String(StringId<'t>),
UserDefinedTypeSource(UserDefinedTypeSourceId),
}
impl<'t> IdData<'t> {}
impl<'t> TryFromCtx<'t, scroll::Endian> for IdData<'t> {
type Error = Error;
fn try_from_ctx(this: &'t [u8], _ctx: scroll::Endian) -> Result<(Self, usize)> {
let mut buf = ParseBuffer::from(this);
let leaf = buf.parse_u16()?;
let data = match leaf {
LF_FUNC_ID => IdData::Function(FunctionId {
scope: parse_optional_id_index(&mut buf)?,
function_type: buf.parse()?,
name: parse_string(leaf, &mut buf)?,
}),
LF_MFUNC_ID => IdData::MemberFunction(MemberFunctionId {
parent: buf.parse()?,
function_type: buf.parse()?,
name: parse_string(leaf, &mut buf)?,
}),
LF_BUILDINFO => IdData::BuildInfo({
let count = buf.parse::<u16>()?;
let mut arguments = Vec::with_capacity(count as usize);
for _ in 0..count {
arguments.push(buf.parse()?);
}
BuildInfoId { arguments }
}),
LF_SUBSTR_LIST => IdData::StringList({
let count = buf.parse::<u32>()?;
let mut substrings = Vec::with_capacity(count as usize);
for _ in 0..count {
substrings.push(buf.parse()?);
}
StringListId { substrings }
}),
LF_STRING_ID => IdData::String(StringId {
substrings: parse_optional_id_index(&mut buf)?,
name: parse_string(leaf, &mut buf)?,
}),
LF_UDT_SRC_LINE | LF_UDT_MOD_SRC_LINE => {
let udt = buf.parse()?;
let file_id = buf.parse()?;
let line = buf.parse()?;
let source_file = if leaf == self::LF_UDT_SRC_LINE {
UserDefinedTypeSourceFileRef::Local(IdIndex(file_id))
} else {
UserDefinedTypeSourceFileRef::Remote(buf.parse()?, StringRef(file_id))
};
IdData::UserDefinedTypeSource(UserDefinedTypeSourceId {
udt,
source_file,
line,
})
}
_ => return Err(Error::UnimplementedTypeKind(leaf)),
};
Ok((data, buf.pos()))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FunctionId<'t> {
pub scope: Option<IdIndex>,
pub function_type: TypeIndex,
pub name: RawString<'t>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MemberFunctionId<'t> {
pub parent: TypeIndex,
pub function_type: TypeIndex,
pub name: RawString<'t>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BuildInfoId {
pub arguments: Vec<IdIndex>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringListId {
pub substrings: Vec<TypeIndex>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StringId<'t> {
pub substrings: Option<IdIndex>,
pub name: RawString<'t>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum UserDefinedTypeSourceFileRef {
Local(IdIndex),
Remote(u16, StringRef),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UserDefinedTypeSourceId {
pub udt: TypeIndex,
pub source_file: UserDefinedTypeSourceFileRef,
pub line: u32,
}