puffin/algebra/
macros.rs

1//! This module provides a DLS for writing [`Term`](crate::algebra::Term)s within Rust.
2//! See the tlspuffin crate for usage examples.
3
4#[macro_export]
5macro_rules! term {
6    //
7    // Handshake with QueryMatcher
8    // `>$req_type:expr` must be the last part of the arm, even if it is not used.
9    //
10    ((!$precomp:literal, $counter:expr) / $typ:ty $(>$req_type:expr)?) => {{
11        use $crate::algebra::dynamic_function::TypeShape;
12
13        // ignore $req_type as we are overriding it with $type
14        Term::from(term!((!$precomp, $counter) > TypeShape::of::<$typ>()))
15    }};
16    (($agent:expr, $counter:expr) / $typ:ty $(>$req_type:expr)?) => {{
17        use $crate::algebra::dynamic_function::TypeShape;
18        use $crate::algebra::Term;
19
20
21        // ignore $req_type as we are overriding it with $type
22        Term::from(term!(($agent, $counter) > TypeShape::of::<$typ>()))
23    }};
24    ((!$precomp:literal, $counter:expr) $(>$req_type:expr)?) => {{
25        use $crate::algebra::signature::Signature;
26        use $crate::algebra::Term;
27        use $crate::trace::Source;
28
29        let var = Signature::new_var($($req_type)?, Some(Source::Label(Some($precomp.into()))), None, $counter); // TODO: verify hat using here None is fine. Before a refactor it was: Some(TlsMessageType::Handshake(None))
30        Term::from(DYTerm::Variable(var))
31    }};
32    (($agent:expr, $counter:expr) $(>$req_type:expr)?) => {{
33        use $crate::algebra::signature::Signature;
34        use $crate::algebra::{DYTerm,Term};
35        use $crate::trace::Source;
36
37        let var = Signature::new_var($($req_type)?, Some(Source::Agent($agent)), None, $counter); // TODO: verify hat using here None is fine. Before a refactor it was: Some(TlsMessageType::Handshake(None))
38        Term::from(DYTerm::Variable(var))
39    }};
40
41    //
42    // Handshake TlsMessageType with `$message_type` as `TlsMessageType`
43    //
44    ((!$precomp:literal, $counter:expr) [$message_type:expr] / $typ:ty $(>$req_type:expr)?) => {{
45        use $crate::algebra::dynamic_function::TypeShape;
46
47        // ignore $req_type as we are overriding it with $type
48       Term::from(term!((!$precomp, $counter) [$message_type] > TypeShape::of::<$typ>()))
49    }};
50    (($agent:expr, $counter:expr) [$message_type:expr] / $typ:ty $(>$req_type:expr)?) => {{
51        use $crate::algebra::dynamic_function::TypeShape;
52
53        // ignore $req_type as we are overriding it with $type
54        Term::from(term!(($agent, $counter) [$message_type] > TypeShape::of::<$typ>()))
55    }};
56    // Extended with custom $type
57    ((!$precomp:literal, $counter:expr) [$message_type:expr] $(>$req_type:expr)?) => {{
58        use $crate::algebra::signature::Signature;
59        use $crate::algebra::Term;
60        use $crate::trace::Source;
61
62        let var = Signature::new_var($($req_type)?, Some(Source::Label(Some($precomp.into()))), $message_type, $counter);
63        Term::from(DYTerm::Variable(var))
64    }};
65    (($agent:expr, $counter:expr) [$message_type:expr] $(>$req_type:expr)?) => {{
66        use $crate::algebra::signature::Signature;
67        use $crate::algebra::{DYTerm,Term};
68        use $crate::trace::Source;
69
70        let var = Signature::new_var($($req_type)?, Some(Source::Agent($agent)), $message_type, $counter);
71        Term::from(DYTerm::Variable(var))
72    }};
73
74    //
75    // Function Applications
76    ($func:ident ( $( $arg:tt $( ( $($inner:tt)* ) )? ),* ) $(>$req_type:expr)?) => {{
77        use $crate::algebra::signature::Signature;
78        use $crate::algebra::{DYTerm,Term};
79
80        let func = Signature::new_function(&$func);
81        #[allow(unused_assignments, unused_variables, unused_mut)]
82        let mut i = 0;
83
84        #[allow(unused_assignments)]
85        #[allow(clippy::mixed_read_write_in_expression)]
86        let arguments = vec![$({
87            #[allow(unused)]
88            if let Some(argument) = func.shape().argument_types.get(i) {
89                i += 1;
90                Term::from($crate::term_arg!($arg $( ( $($inner)* ) )? > argument.clone()))
91            } else {
92                panic!("too many arguments specified for function {}", func)
93            }
94        }),*];
95
96        Term::from(DYTerm::Application(func, arguments))
97    }};
98    // Shorthand for constants
99    ($func:ident $(>$req_type:expr)?) => {{
100        use $crate::algebra::signature::Signature;
101        use $crate::algebra::{DYTerm,Term};
102
103
104        let func = Signature::new_function(&$func);
105        Term::from(DYTerm::Application(func, vec![]))
106    }};
107
108    //
109    // Allows to use variables which already contain a term by starting with a `@`
110    //
111    (@$e:ident $(>$req_type:expr)?) => {{
112        use $crate::algebra::{DYTerm,Term};
113
114        let subterm: &Term<_> = &$e;
115        Term::from(subterm.clone())
116    }};
117}
118
119#[macro_export]
120macro_rules! term_arg {
121    // Parenthesized nested term (unwrap the parentheses and re-parse the content).
122    // e.g. `(fn_f())`. This path is kept for backward compatibility when double parentheses were mandatory
123    ( ( $($e:tt)* ) $(>$req_type:expr)?) => {{
124        use $crate::algebra::{DYTerm,Term};
125
126        Term::from(term!($($e)* $(>$req_type)?))
127    }};
128    // Nested application without wrapping parentheses: `fn_f(...)`.
129    ( $f:ident ( $($inner:tt)* ) $(>$req_type:expr)?) => {{
130        use $crate::algebra::{DYTerm,Term};
131
132        Term::from(term!($f ( $($inner)* ) $(>$req_type)?))
133    }};
134    // Any single token tree (constant, variable, ...).
135    ($e:tt $(>$req_type:expr)?) => {{
136        Term::from(term!($e $(>$req_type)?))
137    }};
138}