Skip to main content

fic/ast/
process.rs

1//! Process AST nodes for the Fiducia language.
2//!
3//! A *process* is the orchestration layer of a Fiducia protection
4//! domain — a CSP-style algebraic expression composed from events,
5//! guards, choices, and sequential composition. Following `CoreFi`
6//! paper §4.2:
7//!
8//! ```text
9//! Process ρ ::= Ω                       divergence
10//!            | Skip                     successful termination
11//!            | evnt → ρ                 event prefixing
12//!            | [b: B] ρ                 guarded process
13//!            | ρ₁ □ ρ₂                  external choice
14//!            | (ρ₁ □ ρ₂) default ρd     default external choice
15//!            | ρ₁ ; ρ₂                  sequential composition
16//!            | μX. ρ                    recursion (modeled as named
17//!                                       processes that recurse by
18//!                                       name)
19//! ```
20//!
21//! Processes appear in `.fi` files as flat declarations with the `proc`
22//! keyword:
23//!
24//! ```text
25//! proc Notified()      { irq?159 -> Handle_eth(); Notified(); }
26//! proc Handle_eth()    { handle_irq -> Dispatch(); }
27//! proc Dispatch()      { [rx] receive -> Skip [] [!rx] cleanup -> Skip; }
28//! proc Reply(pkt: int) { [pass] o2d!pkt -> Skip [] [!pass] handle_ping -> Skip; }
29//! ```
30//!
31//! Every process referenced (`Handle_eth`, `Dispatch`) is declared in
32//! the same file; `receive`, `cleanup`, `handle_irq`, `handle_ping`
33//! are computation events declared as `evt` declarations, and `irq`,
34//! `o2d` are channel names from the surrounding `.fis`.
35//!
36//! Language reference: [Process Definitions](https://fiducia-lang.github.io/docs/lang-spec/items/process-blocks.html).
37
38use serde::Serialize;
39use std::ops::Range;
40
41use super::event::Event;
42use super::expr::Expr;
43use super::statement::VarDecl;
44use super::types::Type;
45
46/// One `platform.entry` pair from a process's `@bind(...)` annotation.
47///
48/// Maps the process to a platform's kernel entry point (e.g.
49/// `microkit.notified`). `fic` only *records* these — resolving them to a
50/// concrete entry point, and rejecting unknown or conflicting targets, is the
51/// project layer's job (`fire`).
52#[derive(Debug, Clone, Serialize, PartialEq)]
53pub struct BindAnnotation {
54    /// Platform namespace (e.g. `microkit`, `cheri`).
55    pub platform: String,
56    /// Entry point name within the platform (e.g. `notified`).
57    pub entry: String,
58    /// Byte span of the `platform.entry` pair in the source.
59    pub span: Range<usize>,
60}
61
62/// A named process declaration: `@bind(...)?  proc Name(params) { body }`.
63#[derive(Debug, Clone, Serialize)]
64pub struct ProcessDecl {
65    /// Process name (`UpperCamelCase` by convention).
66    pub name: String,
67    /// Parameter list. By convention processes carry at most a
68    /// single parameter, but the AST allows any number.
69    pub params: Vec<(Type, String)>,
70    /// Process body — a single process expression. Sequential
71    /// composition (`;`) inside the body is captured by [`Process::Seq`].
72    pub body: Process,
73    /// Platform entry-point bindings from a single `@bind(...)` annotation
74    /// (empty if absent). A process with no binding is *internal* — not
75    /// directly callable by the kernel. `fic` records these only; resolving
76    /// them is deferred to the project layer (`fire`).
77    pub binds: Vec<BindAnnotation>,
78    /// Byte span of the `proc ... { ... }` declaration. A preceding
79    /// `@bind(...)` annotation is not included; like the other declaration
80    /// spans, whitespace consumed before the keyword is.
81    pub span: Range<usize>,
82}
83
84/// A process expression.
85#[derive(Debug, Clone, Serialize)]
86pub enum Process {
87    /// Successful termination.
88    Skip,
89    /// Divergence (Ω): infinite-loop terminal.
90    Div,
91    /// `evnt -> ρ` — event prefix.
92    Prefix(Event, Box<Process>),
93    /// `ρ₁ ; ρ₂ ; ... ; ρₙ` — sequential composition.
94    Seq(Vec<Process>),
95    /// `ρ₁ [] ρ₂ [] ... ρₙ` — external choice, with an optional
96    /// `default` arm taken when no branch's guard succeeds.
97    Choice {
98        /// The guarded branches.
99        branches: Vec<Process>,
100        /// The fallback when no branch's guard succeeds. `None` means
101        /// the choice has no default arm.
102        default: Option<Box<Process>>,
103    },
104    /// `[guard] ρ` — guarded process.
105    Guard(Expr, Box<Process>),
106    /// `Name(args)?` — reference to another process, by name.
107    ProcessRef {
108        /// Target name.
109        name: String,
110        /// Argument identifiers (will become `Vec<Expr>` with the expression grammar).
111        params: Vec<String>,
112        /// Byte span of the reference, for diagnostics.
113        span: Range<usize>,
114    },
115    /// A local variable declaration inside a process body: `ident ':' Type ';'`.
116    /// Assignments and external calls are not permitted in process bodies —
117    /// side effects belong exclusively in `evt` declarations.
118    Decl(VarDecl),
119}
120
121impl Process {
122    /// Every [`Process::Decl`] in the body, in source order.
123    pub fn decls(&self) -> Vec<&VarDecl> {
124        let mut out = Vec::new();
125        self.push_decls(&mut out);
126        out
127    }
128
129    fn push_decls<'a>(&'a self, out: &mut Vec<&'a VarDecl>) {
130        match self {
131            Process::Decl(v) => out.push(v),
132            Process::Prefix(_, rest) | Process::Guard(_, rest) => rest.push_decls(out),
133            Process::Seq(parts) => {
134                for q in parts {
135                    q.push_decls(out);
136                }
137            }
138            Process::Choice { branches, default } => {
139                for b in branches {
140                    b.push_decls(out);
141                }
142                if let Some(d) = default {
143                    d.push_decls(out);
144                }
145            }
146            Process::Skip | Process::Div | Process::ProcessRef { .. } => {}
147        }
148    }
149}