fic/ast/channel.rs
1//! Channel declaration AST nodes for the Fiducia language.
2//!
3//! Represents `Chan`, `Async Chan`, and `Sync Chan` declarations from `.fis` files,
4//! including optional `via buf<Type>` buffer annotations for async channels.
5
6use serde::Serialize;
7use std::fmt::{self, Display, Formatter};
8use std::ops::Range;
9
10use super::types::{ArraySize, Type};
11
12/// Backing memory buffer for an async channel (`via buf<Type>`).
13#[derive(Debug, Clone, PartialEq, Serialize)]
14pub struct ChannelBuffer {
15 /// Name of the `MemoryRegion` that backs this channel.
16 pub region: String,
17 /// The slot type, `Σ(c)`: what the channel carries. Every buffer
18 /// names one; a raw buffer is `via buf<byte>`.
19 pub payload: Type,
20 /// The ring's capacity, `via buf<T, N>`; without one, as many slots as
21 /// fit in the region.
22 pub capacity: Option<ArraySize>,
23}
24
25/// Channel declaration connecting two protection domains.
26///
27/// ## `Chan` Declaration (`.fis`)
28///
29/// Declares a communication channel between two protection domains. A
30/// *signalling* channel carries no value (`c!`, `c?`); a *message-passing*
31/// channel carries values of a type (`c!e`, `c?x`): a buffered channel, or a
32/// `Sync Chan<T>`, whose call passes the value in the message registers.
33///
34/// | Form | Syntax | Direction | Kind |
35/// |------|--------|-----------|------|
36/// | Async bidirectional | `[Async] Chan name = sender <-> receiver;` | both ways | signalling |
37/// | Async unidirectional | `[Async] Chan name = sender --> receiver;` | sender → receiver only | signalling |
38/// | Sync unidirectional | `Sync Chan name = sender --> receiver;` | sender → receiver only | a protected procedure call, signalling |
39/// | Sync with a value | `Sync Chan<T> name = sender --> receiver;` | sender → receiver only | a protected procedure call carrying a `T` |
40/// | Buffered | `[Async] Chan name = sender --> receiver via region<T>;` | sender → receiver only | message passing, a FIFO of `T` |
41///
42/// The `Async` keyword is optional for async channels; `Chan` without `Sync` is always async.
43/// Only an async `-->` channel has a buffer (`via region<T>`); `<->` and `Sync` carry none,
44/// and only `Sync` takes `<T>`.
45/// The trailing `;` is optional.
46///
47/// ```text
48/// Chan link = client <-> server; // signalling, both ways
49/// Chan tx = producer --> consumer; // signalling, one way
50/// Async Chan out = sender --> recv; // explicit async keyword
51/// Sync Chan rpc = caller --> handler; // signalling, synchronous
52/// Chan pkt = producer --> consumer via buf<Packet>; // buffered
53/// ```
54///
55/// Language reference: [Channel Declarations](https://fiducia-lang.github.io/docs/lang-spec/system/channels.html).
56#[derive(Debug, Clone, Serialize)]
57pub struct ChannelDecl {
58 /// Channel kind (sync/async, uni/bidirectional).
59 pub kind: ChannelKind,
60 /// Channel name.
61 pub name: String,
62 /// Sender (left) endpoint.
63 ///
64 /// For `SyncUni`/`AsyncUni`, this PD initiates the communication.
65 /// For `AsyncBi`, this is the left-hand PD in the declaration.
66 pub sender: String,
67 /// Receiver (right) endpoint.
68 ///
69 /// For `SyncUni`/`AsyncUni`, this PD receives the communication.
70 /// For `AsyncBi`, this is the right-hand PD in the declaration.
71 pub receiver: String,
72 /// Optional backing memory buffer (`via buf` / `via buf<Type>`).
73 ///
74 /// `None` — signal only, no shared memory.
75 pub buffer: Option<ChannelBuffer>,
76 /// The value a `Sync Chan<T>` carries, in the message registers; `None`
77 /// for every other channel, whose value (if any) is its buffer's.
78 pub payload: Option<Type>,
79 /// Byte offset of `sender` identifier in the source.
80 pub sender_offset: usize,
81 /// Byte offset of `receiver` identifier in the source.
82 pub receiver_offset: usize,
83 /// Byte span of the entire declaration in the source.
84 pub span: Range<usize>,
85}
86
87/// The kind of inter-PD channel.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
89pub enum ChannelKind {
90 /// Synchronous unidirectional: `Sync Chan a --> b`
91 SyncUni,
92 /// Asynchronous unidirectional: `[Async] Chan a --> b`
93 AsyncUni,
94 /// Asynchronous bidirectional: `[Async] Chan a <-> b`
95 AsyncBi,
96}
97
98impl Display for ChannelKind {
99 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
100 match self {
101 ChannelKind::SyncUni => write!(f, "synchronous unidirectional"),
102 ChannelKind::AsyncUni => write!(f, "asynchronous unidirectional"),
103 ChannelKind::AsyncBi => write!(f, "asynchronous bidirectional"),
104 }
105 }
106}
107
108impl Display for ChannelDecl {
109 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
110 write!(
111 f,
112 "kind: {}, {} → {}",
113 self.kind, self.sender, self.receiver
114 )
115 }
116}