1use std::borrow::Cow;
4use std::hash::Hash;
5
6use proc_macro2::{Ident, Span, TokenStream};
7use quote::ToTokens;
8use serde::{Deserialize, Serialize};
9use syn::punctuated::Punctuated;
10use syn::spanned::Spanned;
11use syn::{Expr, ExprPath, GenericArgument, Token, Type};
12
13use self::ops::{OperatorConstraints, Persistence};
14use crate::diagnostic::{Diagnostic, Diagnostics, Level};
15use crate::parse::{DfirCode, IndexInt, Operator, PortIndex, Ported, SingletonRef};
16use crate::pretty_span::PrettySpan;
17
18mod di_mul_graph;
19mod eliminate_extra_unions_tees;
20mod flat_graph_builder;
21mod flat_to_partitioned;
22mod graph_write;
23mod meta_graph;
24mod meta_graph_debugging;
25
26use std::fmt::Display;
27
28pub use di_mul_graph::DiMulGraph;
29pub use eliminate_extra_unions_tees::eliminate_extra_unions_tees;
30pub use flat_graph_builder::{FlatGraphBuilder, FlatGraphBuilderOutput};
31pub use flat_to_partitioned::partition_graph;
32pub use meta_graph::{DfirGraph, WriteConfig, WriteGraphType};
33
34pub use crate::graph_ids::{GraphEdgeId, GraphLoopId, GraphNodeId, GraphSubgraphId};
35
36pub mod graph_algorithms;
37pub mod ops;
38
39impl GraphSubgraphId {
40 pub fn as_ident(self, span: Span) -> Ident {
42 use slotmap::Key;
43 Ident::new(&format!("sgid_{:?}", self.data()), span)
44 }
45}
46
47impl GraphLoopId {
48 pub fn as_ident(self, span: Span) -> Ident {
50 use slotmap::Key;
51 Ident::new(&format!("loop_{:?}", self.data()), span)
52 }
53}
54
55const CONTEXT: &str = "context";
57const GRAPH: &str = "df";
59
60const HANDOFF_NODE_STR: &str = "handoff";
61const SINGLETON_SLOT_NODE_STR: &str = "singleton";
62const MODULE_BOUNDARY_NODE_STR: &str = "module_boundary";
63
64mod serde_syn {
65 use serde::{Deserialize, Deserializer, Serializer};
66
67 pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
68 where
69 S: Serializer,
70 T: quote::ToTokens,
71 {
72 serializer.serialize_str(&value.to_token_stream().to_string())
73 }
74
75 pub fn deserialize<'de, D, T>(deserializer: D) -> Result<T, D::Error>
76 where
77 D: Deserializer<'de>,
78 T: syn::parse::Parse,
79 {
80 let s = String::deserialize(deserializer)?;
81 syn::parse_str(&s).map_err(<D::Error as serde::de::Error>::custom)
82 }
83}
84
85#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq, Hash)]
89pub struct Varname(#[serde(with = "serde_syn")] pub Ident);
90
91#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
93pub enum HandoffKind {
94 Vec,
96 Singleton,
99 Optional,
102}
103
104#[derive(Clone, Serialize, Deserialize)]
106pub enum GraphNode {
107 Operator(#[serde(with = "serde_syn")] Operator),
109 Handoff {
111 kind: HandoffKind,
113 #[serde(skip, default = "Span::call_site")]
115 src_span: Span,
116 #[serde(skip, default = "Span::call_site")]
118 dst_span: Span,
119 },
120
121 ModuleBoundary {
123 input: bool,
125
126 #[serde(skip, default = "Span::call_site")]
130 import_expr: Span,
131 },
132}
133impl GraphNode {
134 pub fn to_pretty_string(&self) -> Cow<'static, str> {
136 match self {
137 GraphNode::Operator(op) => op.to_pretty_string().into(),
138 GraphNode::Handoff {
139 kind: HandoffKind::Vec,
140 ..
141 } => HANDOFF_NODE_STR.into(),
142 GraphNode::Handoff {
143 kind: HandoffKind::Singleton | HandoffKind::Optional,
144 ..
145 } => SINGLETON_SLOT_NODE_STR.into(),
146 GraphNode::ModuleBoundary { .. } => MODULE_BOUNDARY_NODE_STR.into(),
147 }
148 }
149
150 pub fn to_name_string(&self) -> Cow<'static, str> {
152 match self {
153 GraphNode::Operator(op) => op.name_string().into(),
154 GraphNode::Handoff {
155 kind: HandoffKind::Vec,
156 ..
157 } => HANDOFF_NODE_STR.into(),
158 GraphNode::Handoff {
159 kind: HandoffKind::Singleton | HandoffKind::Optional,
160 ..
161 } => SINGLETON_SLOT_NODE_STR.into(),
162 GraphNode::ModuleBoundary { .. } => MODULE_BOUNDARY_NODE_STR.into(),
163 }
164 }
165
166 pub fn span(&self) -> Span {
168 match self {
169 Self::Operator(op) => op.span(),
170 &Self::Handoff {
171 src_span, dst_span, ..
172 } => src_span.join(dst_span).unwrap_or(src_span),
173 Self::ModuleBoundary { import_expr, .. } => *import_expr,
174 }
175 }
176}
177impl std::fmt::Debug for GraphNode {
178 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179 match self {
180 Self::Operator(operator) => {
181 write!(f, "Node::Operator({} span)", PrettySpan(operator.span()))
182 }
183 Self::Handoff { kind, .. } => write!(f, "Node::Handoff({kind:?})"),
184 Self::ModuleBoundary { input, .. } => {
185 write!(f, "Node::ModuleBoundary{{input: {}}}", input)
186 }
187 }
188 }
189}
190
191#[derive(Clone, Debug)]
200pub struct OperatorInstance {
201 pub op_constraints: &'static OperatorConstraints,
203 pub input_ports: Vec<PortIndexValue>,
205 pub output_ports: Vec<PortIndexValue>,
207 pub singletons_referenced: Vec<SingletonRef>,
209
210 pub generics: OpInstGenerics,
212 pub arguments_pre: Punctuated<Expr, Token![,]>,
218 pub arguments_raw: TokenStream,
220}
221
222#[derive(Clone, Debug)]
224pub struct OpInstGenerics {
225 pub generic_args: Option<Punctuated<GenericArgument, Token![,]>>,
227 pub persistence_args: Vec<Persistence>,
229 pub type_args: Vec<Type>,
231}
232
233impl OpInstGenerics {
234 fn join_spans<I>(mut spans: I) -> Option<Span>
239 where
240 I: Iterator<Item = Span>,
241 {
242 let mut span = spans.next()?;
243 for s in spans {
244 span = span.join(s)?;
245 }
246 Some(span)
247 }
248
249 pub fn persistence_args_span(&self) -> Option<Span> {
251 self.generic_args.as_ref().and_then(|args| {
252 Self::join_spans(
253 args.iter()
254 .filter(|a| matches!(a, GenericArgument::Lifetime(_)))
255 .map(|a| a.span()),
256 )
257 })
258 }
259
260 pub fn type_args_span(&self) -> Option<Span> {
262 self.generic_args.as_ref().and_then(|args| {
263 Self::join_spans(
264 args.iter()
265 .filter(|a| matches!(a, GenericArgument::Type(_)))
266 .map(|a| a.span()),
267 )
268 })
269 }
270}
271
272pub fn get_operator_generics(diagnostics: &mut Diagnostics, operator: &Operator) -> OpInstGenerics {
277 let generic_args = operator.type_arguments().cloned();
279 let persistence_args = generic_args.iter().flatten().map_while(|generic_arg| match generic_arg {
280 GenericArgument::Lifetime(lifetime) => {
281 match &*lifetime.ident.to_string() {
282 "none" => Some(Persistence::None),
283 "loop" => Some(Persistence::Loop),
284 "tick" => Some(Persistence::Tick),
285 "static" => Some(Persistence::Static),
286 _ => {
287 diagnostics.push(Diagnostic::spanned(
288 generic_arg.span(),
289 Level::Error,
290 format!("Unknown lifetime generic argument `'{}`, expected `'none`, `'loop`, `'tick`, or `'static`.", lifetime.ident),
291 ));
292 None
294 }
295 }
296 },
297 _ => None,
298 }).collect::<Vec<_>>();
299 let type_args = generic_args
300 .iter()
301 .flatten()
302 .skip(persistence_args.len())
303 .map_while(|generic_arg| match generic_arg {
304 GenericArgument::Type(typ) => Some(typ),
305 _ => None,
306 })
307 .cloned()
308 .collect::<Vec<_>>();
309
310 OpInstGenerics {
311 generic_args,
312 persistence_args,
313 type_args,
314 }
315}
316
317#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
319pub enum Color {
320 Pull,
322 Push,
324 Comp,
326 Hoff,
328}
329
330#[derive(Clone, Debug, Serialize, Deserialize)]
332pub enum PortIndexValue {
333 Int(#[serde(with = "serde_syn")] IndexInt),
335 Path(#[serde(with = "serde_syn")] ExprPath),
337 Elided(#[serde(skip)] Option<Span>),
340}
341impl PortIndexValue {
342 pub fn from_ported<Inner>(ported: Ported<Inner>) -> (Self, Inner, Self)
345 where
346 Inner: Spanned,
347 {
348 let ported_span = Some(ported.inner.span());
349 let port_inn = ported
350 .inn
351 .map(|idx| idx.index.into())
352 .unwrap_or_else(|| Self::Elided(ported_span));
353 let inner = ported.inner;
354 let port_out = ported
355 .out
356 .map(|idx| idx.index.into())
357 .unwrap_or_else(|| Self::Elided(ported_span));
358 (port_inn, inner, port_out)
359 }
360
361 pub fn is_specified(&self) -> bool {
363 !matches!(self, Self::Elided(_))
364 }
365
366 #[allow(clippy::allow_attributes, reason = "Only triggered on nightly.")]
370 #[allow(
371 clippy::result_large_err,
372 reason = "variants are same size, error isn't to be propagated."
373 )]
374 pub fn combine(self, other: Self) -> Result<Self, Self> {
375 match (self.is_specified(), other.is_specified()) {
376 (false, _other) => Ok(other),
377 (true, false) => Ok(self),
378 (true, true) => Err(self),
379 }
380 }
381
382 pub fn as_error_message_string(&self) -> String {
384 match self {
385 PortIndexValue::Int(n) => format!("`{}`", n.value),
386 PortIndexValue::Path(path) => format!("`{}`", path.to_token_stream()),
387 PortIndexValue::Elided(_) => "<elided>".to_owned(),
388 }
389 }
390
391 pub fn span(&self) -> Span {
393 match self {
394 PortIndexValue::Int(x) => x.span(),
395 PortIndexValue::Path(x) => x.span(),
396 PortIndexValue::Elided(span) => span.unwrap_or_else(Span::call_site),
397 }
398 }
399}
400impl From<PortIndex> for PortIndexValue {
401 fn from(value: PortIndex) -> Self {
402 match value {
403 PortIndex::Int(x) => Self::Int(x),
404 PortIndex::Path(x) => Self::Path(x),
405 }
406 }
407}
408impl PartialEq for PortIndexValue {
409 fn eq(&self, other: &Self) -> bool {
410 match (self, other) {
411 (Self::Int(l0), Self::Int(r0)) => l0 == r0,
412 (Self::Path(l0), Self::Path(r0)) => l0 == r0,
413 (Self::Elided(_), Self::Elided(_)) => true,
414 _else => false,
415 }
416 }
417}
418impl Eq for PortIndexValue {}
419impl PartialOrd for PortIndexValue {
420 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
421 Some(self.cmp(other))
422 }
423}
424impl Ord for PortIndexValue {
425 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
426 match (self, other) {
427 (Self::Int(s), Self::Int(o)) => s.cmp(o),
428 (Self::Path(s), Self::Path(o)) => s
429 .to_token_stream()
430 .to_string()
431 .cmp(&o.to_token_stream().to_string()),
432 (Self::Elided(_), Self::Elided(_)) => std::cmp::Ordering::Equal,
433 (Self::Int(_), Self::Path(_)) => std::cmp::Ordering::Less,
434 (Self::Path(_), Self::Int(_)) => std::cmp::Ordering::Greater,
435 (_, Self::Elided(_)) => std::cmp::Ordering::Less,
436 (Self::Elided(_), _) => std::cmp::Ordering::Greater,
437 }
438 }
439}
440
441impl Display for PortIndexValue {
442 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
443 match self {
444 PortIndexValue::Int(x) => write!(f, "{}", x.to_token_stream()),
445 PortIndexValue::Path(x) => write!(f, "{}", x.to_token_stream()),
446 PortIndexValue::Elided(_) => write!(f, "[]"),
447 }
448 }
449}
450
451pub struct BuildDfirCodeOutput {
453 pub partitioned_graph: DfirGraph,
455 pub code: TokenStream,
457 pub diagnostics: Diagnostics,
459}
460
461pub fn build_dfir_code(
463 dfir_code: DfirCode,
464 root: &TokenStream,
465) -> Result<BuildDfirCodeOutput, Diagnostics> {
466 let flat_graph_builder = FlatGraphBuilder::from_dfir(dfir_code);
467
468 let FlatGraphBuilderOutput {
469 mut flat_graph,
470 uses,
471 mut diagnostics,
472 } = flat_graph_builder.build()?;
473
474 let () = match flat_graph.merge_modules() {
475 Ok(()) => (),
476 Err(d) => {
477 diagnostics.push(d);
478 return Err(diagnostics);
479 }
480 };
481
482 eliminate_extra_unions_tees(&mut flat_graph);
483
484 for (edge_id, (src, dst)) in flat_graph.edges() {
487 let _ = edge_id;
488 if matches!(flat_graph.node(src), GraphNode::Handoff { .. })
489 && matches!(flat_graph.node(dst), GraphNode::Handoff { .. })
490 {
491 let span = flat_graph.node(dst).span();
492 diagnostics.push(Diagnostic::spanned(
493 span,
494 Level::Error,
495 "Adjacent handoff/singleton operators are not allowed. \
496 Remove one or insert an operator between them.",
497 ));
498 }
499 }
500
501 if diagnostics.has_error() {
502 return Err(diagnostics);
503 }
504
505 let partitioned_graph = match partition_graph(flat_graph) {
506 Ok(partitioned_graph) => partitioned_graph,
507 Err(d) => {
508 diagnostics.push(d);
509 return Err(diagnostics);
510 }
511 };
512
513 let code =
514 partitioned_graph.as_code(root, true, quote::quote! { #( #uses )* }, &mut diagnostics)?;
515
516 Ok(BuildDfirCodeOutput {
517 partitioned_graph,
518 code,
519 diagnostics,
520 })
521}
522
523fn change_spans(tokens: TokenStream, span: Span) -> TokenStream {
525 use proc_macro2::{Group, TokenTree};
526 tokens
527 .into_iter()
528 .map(|token| match token {
529 TokenTree::Group(mut group) => {
530 group.set_span(span);
531 TokenTree::Group(Group::new(
532 group.delimiter(),
533 change_spans(group.stream(), span),
534 ))
535 }
536 TokenTree::Ident(mut ident) => {
537 ident.set_span(span.resolved_at(ident.span()));
538 TokenTree::Ident(ident)
539 }
540 TokenTree::Punct(mut punct) => {
541 punct.set_span(span);
542 TokenTree::Punct(punct)
543 }
544 TokenTree::Literal(mut literal) => {
545 literal.set_span(span);
546 TokenTree::Literal(literal)
547 }
548 })
549 .collect()
550}