-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathread.rs
328 lines (297 loc) · 11.5 KB
/
read.rs
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use crate::capnp::hugr_v0_capnp as hugr_capnp;
use crate::v0 as model;
use bumpalo::collections::Vec as BumpVec;
use bumpalo::Bump;
use std::io::BufRead;
/// An error encounted while deserialising a model.
#[derive(Debug, derive_more::From, derive_more::Display, derive_more::Error)]
#[non_exhaustive]
pub enum ReadError {
#[from(forward)]
/// An error encounted while decoding a model from a `capnproto` buffer.
DecodingError(capnp::Error),
}
type ReadResult<T> = Result<T, ReadError>;
/// Read a hugr module from a byte slice.
pub fn read_from_slice<'a>(slice: &[u8], bump: &'a Bump) -> ReadResult<model::Module<'a>> {
read_from_reader(slice, bump)
}
/// Read a hugr module from an impl of [BufRead].
pub fn read_from_reader(reader: impl BufRead, bump: &Bump) -> ReadResult<model::Module<'_>> {
let reader =
capnp::serialize_packed::read_message(reader, capnp::message::ReaderOptions::new())?;
let root = reader.get_root::<hugr_capnp::module::Reader>()?;
read_module(bump, root)
}
/// Read a list of structs from a reader into a slice allocated through the bump allocator.
macro_rules! read_list {
($bump:expr, $reader:expr, $read:expr) => {{
let mut __list_reader = $reader;
let mut __list = BumpVec::with_capacity_in(__list_reader.len() as _, $bump);
for __item_reader in __list_reader.iter() {
__list.push($read($bump, __item_reader)?);
}
__list.into_bump_slice()
}};
}
/// Read a list of scalars from a reader into a slice allocated through the bump allocator.
macro_rules! read_scalar_list {
($bump:expr, $reader:expr, $get:ident, $wrap:path) => {{
let mut __list_reader = $reader.$get()?;
let mut __list = BumpVec::with_capacity_in(__list_reader.len() as _, $bump);
for __item in __list_reader.iter() {
__list.push($wrap(__item));
}
__list.into_bump_slice()
}};
}
fn read_module<'a>(
bump: &'a Bump,
reader: hugr_capnp::module::Reader,
) -> ReadResult<model::Module<'a>> {
let root = model::RegionId(reader.get_root());
let nodes = reader
.get_nodes()?
.iter()
.map(|r| read_node(bump, r))
.collect::<ReadResult<_>>()?;
let regions = reader
.get_regions()?
.iter()
.map(|r| read_region(bump, r))
.collect::<ReadResult<_>>()?;
let terms = reader
.get_terms()?
.iter()
.map(|r| read_term(bump, r))
.collect::<ReadResult<_>>()?;
Ok(model::Module {
root,
nodes,
regions,
terms,
})
}
fn read_node<'a>(bump: &'a Bump, reader: hugr_capnp::node::Reader) -> ReadResult<model::Node<'a>> {
let operation = read_operation(bump, reader.get_operation()?)?;
let inputs = read_scalar_list!(bump, reader, get_inputs, model::LinkIndex);
let outputs = read_scalar_list!(bump, reader, get_outputs, model::LinkIndex);
let params = read_scalar_list!(bump, reader, get_params, model::TermId);
let regions = read_scalar_list!(bump, reader, get_regions, model::RegionId);
let meta = read_scalar_list!(bump, reader, get_meta, model::TermId);
let signature = reader.get_signature().checked_sub(1).map(model::TermId);
Ok(model::Node {
operation,
inputs,
outputs,
params,
regions,
meta,
signature,
})
}
fn read_operation<'a>(
bump: &'a Bump,
reader: hugr_capnp::operation::Reader,
) -> ReadResult<model::Operation<'a>> {
use hugr_capnp::operation::Which;
Ok(match reader.which()? {
Which::Invalid(()) => model::Operation::Invalid,
Which::Dfg(()) => model::Operation::Dfg,
Which::Cfg(()) => model::Operation::Cfg,
Which::Block(()) => model::Operation::Block,
Which::FuncDefn(reader) => {
let reader = reader?;
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let params = read_list!(bump, reader.get_params()?, read_param);
let constraints = read_scalar_list!(bump, reader, get_constraints, model::TermId);
let signature = model::TermId(reader.get_signature());
let symbol = bump.alloc(model::Symbol {
name,
params,
constraints,
signature,
});
model::Operation::DefineFunc(symbol)
}
Which::FuncDecl(reader) => {
let reader = reader?;
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let params = read_list!(bump, reader.get_params()?, read_param);
let constraints = read_scalar_list!(bump, reader, get_constraints, model::TermId);
let signature = model::TermId(reader.get_signature());
let symbol = bump.alloc(model::Symbol {
name,
params,
constraints,
signature,
});
model::Operation::DeclareFunc(symbol)
}
Which::AliasDefn(reader) => {
let reader = reader?;
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let params = read_list!(bump, reader.get_params()?, read_param);
let signature = model::TermId(reader.get_signature());
let symbol = bump.alloc(model::Symbol {
name,
params,
constraints: &[],
signature,
});
model::Operation::DefineAlias(symbol)
}
Which::AliasDecl(reader) => {
let reader = reader?;
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let params = read_list!(bump, reader.get_params()?, read_param);
let signature = model::TermId(reader.get_signature());
let symbol = bump.alloc(model::Symbol {
name,
params,
constraints: &[],
signature,
});
model::Operation::DeclareAlias(symbol)
}
Which::ConstructorDecl(reader) => {
let reader = reader?;
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let params = read_list!(bump, reader.get_params()?, read_param);
let constraints = read_scalar_list!(bump, reader, get_constraints, model::TermId);
let signature = model::TermId(reader.get_signature());
let symbol = bump.alloc(model::Symbol {
name,
params,
constraints,
signature,
});
model::Operation::DeclareConstructor(symbol)
}
Which::OperationDecl(reader) => {
let reader = reader?;
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let params = read_list!(bump, reader.get_params()?, read_param);
let constraints = read_scalar_list!(bump, reader, get_constraints, model::TermId);
let signature = model::TermId(reader.get_signature());
let symbol = bump.alloc(model::Symbol {
name,
params,
constraints,
signature,
});
model::Operation::DeclareOperation(symbol)
}
Which::Custom(operation) => model::Operation::Custom(model::NodeId(operation)),
Which::TailLoop(()) => model::Operation::TailLoop,
Which::Conditional(()) => model::Operation::Conditional,
Which::Import(name) => model::Operation::Import {
name: bump.alloc_str(name?.to_str()?),
},
})
}
fn read_region<'a>(
bump: &'a Bump,
reader: hugr_capnp::region::Reader,
) -> ReadResult<model::Region<'a>> {
let kind = match reader.get_kind()? {
hugr_capnp::RegionKind::DataFlow => model::RegionKind::DataFlow,
hugr_capnp::RegionKind::ControlFlow => model::RegionKind::ControlFlow,
hugr_capnp::RegionKind::Module => model::RegionKind::Module,
};
let sources = read_scalar_list!(bump, reader, get_sources, model::LinkIndex);
let targets = read_scalar_list!(bump, reader, get_targets, model::LinkIndex);
let children = read_scalar_list!(bump, reader, get_children, model::NodeId);
let meta = read_scalar_list!(bump, reader, get_meta, model::TermId);
let signature = reader.get_signature().checked_sub(1).map(model::TermId);
let scope = if reader.has_scope() {
Some(read_region_scope(reader.get_scope()?)?)
} else {
None
};
Ok(model::Region {
kind,
sources,
targets,
children,
meta,
signature,
scope,
})
}
fn read_region_scope(reader: hugr_capnp::region_scope::Reader) -> ReadResult<model::RegionScope> {
let links = reader.get_links();
let ports = reader.get_ports();
Ok(model::RegionScope { links, ports })
}
fn read_term<'a>(bump: &'a Bump, reader: hugr_capnp::term::Reader) -> ReadResult<model::Term<'a>> {
use hugr_capnp::term::Which;
Ok(match reader.which()? {
Which::Wildcard(()) => model::Term::Wildcard,
Which::String(value) => model::Term::Str(bump.alloc_str(value?.to_str()?)),
Which::Nat(value) => model::Term::Nat(value),
Which::Variable(reader) => {
let node = model::NodeId(reader.get_node());
let index = reader.get_index();
model::Term::Var(model::VarId(node, index))
}
Which::Apply(reader) => {
let symbol = model::NodeId(reader.get_symbol());
let args = read_scalar_list!(bump, reader, get_args, model::TermId);
model::Term::Apply(symbol, args)
}
Which::List(reader) => {
let parts = read_list!(bump, reader?, read_list_part);
model::Term::List(parts)
}
Which::ExtSet(reader) => {
let parts = read_list!(bump, reader?, read_ext_set_part);
model::Term::ExtSet(parts)
}
Which::Tuple(reader) => {
let parts = read_list!(bump, reader?, read_tuple_part);
model::Term::Tuple(parts)
}
Which::ConstFunc(region) => model::Term::ConstFunc(model::RegionId(region)),
Which::Bytes(bytes) => model::Term::Bytes(bump.alloc_slice_copy(bytes?)),
Which::Float(value) => model::Term::Float(value.into()),
})
}
fn read_list_part(
_: &Bump,
reader: hugr_capnp::term::list_part::Reader,
) -> ReadResult<model::ListPart> {
use hugr_capnp::term::list_part::Which;
Ok(match reader.which()? {
Which::Item(term) => model::ListPart::Item(model::TermId(term)),
Which::Splice(list) => model::ListPart::Splice(model::TermId(list)),
})
}
fn read_tuple_part(
_: &Bump,
reader: hugr_capnp::term::tuple_part::Reader,
) -> ReadResult<model::TuplePart> {
use hugr_capnp::term::tuple_part::Which;
Ok(match reader.which()? {
Which::Item(term) => model::TuplePart::Item(model::TermId(term)),
Which::Splice(list) => model::TuplePart::Splice(model::TermId(list)),
})
}
fn read_ext_set_part<'a>(
bump: &'a Bump,
reader: hugr_capnp::term::ext_set_part::Reader,
) -> ReadResult<model::ExtSetPart<'a>> {
use hugr_capnp::term::ext_set_part::Which;
Ok(match reader.which()? {
Which::Extension(ext) => model::ExtSetPart::Extension(bump.alloc_str(ext?.to_str()?)),
Which::Splice(list) => model::ExtSetPart::Splice(model::TermId(list)),
})
}
fn read_param<'a>(
bump: &'a Bump,
reader: hugr_capnp::param::Reader,
) -> ReadResult<model::Param<'a>> {
let name = bump.alloc_str(reader.get_name()?.to_str()?);
let r#type = model::TermId(reader.get_type());
Ok(model::Param { name, r#type })
}