macros/
enum_helpers.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
use proc_macro2::{Span, TokenStream};
use quote::quote;
use std::vec::Vec;
use syn::{parse2, Data, DataEnum, DeriveInput, Fields, Ident, Type};

use self::EnumMethodKind::*;

/// We initially used the convert-case crate, but it converts names like "I32"
/// to "i_32", while we want to get "i32". We thus reimplemented our own converter
/// (which removes one dependency at the same time).
fn to_snake_case(s: &str) -> String {
    let mut snake_case = String::new();

    // We need to keep track of whether the last treated character was
    // lowercase (or not) to prevent this kind of transformations:
    // "VARIANT" -> "v_a_r_i_a_n_t"
    // Note that if we remember whether the last character was uppercase instead,
    // we get things like this:
    // "I32" -> "I3_2"
    let mut last_is_lowercase = false;

    for c in s.chars() {
        if c.is_uppercase() {
            if last_is_lowercase {
                snake_case.push('_');
            }
            last_is_lowercase = false;
            snake_case.push(c.to_lowercase().next().unwrap());
        } else {
            last_is_lowercase = true;
            snake_case.push(c);
        }
    }

    snake_case
}

struct MatchPattern {
    /// The variant name, as a string.
    variant_name: String,
    /// The match pattern.
    /// For instance: `List::Cons(x0, x1)`
    pattern: TokenStream,
    /// The number of arguments in the match pattern (including anonymous
    /// arguments).
    num_args: usize,
    /// The variables we introduced in the match pattern.
    /// `["x0", "x1"]` if the pattern is `List::Cons(hd, tl)`.
    pattern_vars: Vec<Ident>,
    /// The types of the variables introduced in the match pattern
    arg_types: Vec<Type>,
}

/// Generate matching patterns for an enumeration.
fn generate_variant_match_patterns(enum_name: &Ident, data: &DataEnum) -> Vec<MatchPattern> {
    let mut patterns: Vec<MatchPattern> = vec![];
    for variant in &data.variants {
        // Compute the pattern (without the variant constructor), the list
        // of introduced arguments and the list of field types.
        let fields = &variant.fields;
        let num_vars = fields.len();
        let vars: Vec<Ident> = (0..num_vars)
            .map(|i| Ident::new(&format!("_x{i}"), Span::mixed_site()))
            .collect();
        let vartypes: Vec<_> = fields.iter().map(|f| f.ty.clone()).collect();

        let pattern_vars = match fields {
            Fields::Named(_) => {
                let field_names: Vec<_> = fields.iter().map(|f| &f.ident).collect();
                quote!({ #(#field_names : #vars,)* })
            }
            Fields::Unnamed(_) => {
                quote!((#(#vars,)*))
            }
            Fields::Unit => quote!(),
        };
        let variant_name = &variant.ident;
        let pattern = quote!(#enum_name :: #variant_name #pattern_vars);

        patterns.push(MatchPattern {
            variant_name: variant.ident.to_string(),
            pattern,
            num_args: num_vars,
            pattern_vars: vars,
            arg_types: vartypes,
        });
    }

    patterns
}

/// Macro to derive a function `fn variant_name(&self) -> String` printing the
/// constructor of an enumeration. Only works on enumerations, of course.
pub fn derive_variant_name(item: TokenStream) -> TokenStream {
    // Parse the input
    let ast: DeriveInput = parse2(item).unwrap();

    // Generate the code for the matches
    let Data::Enum(data) = &ast.data else {
        panic!("VariantName macro can only be called on enums");
    };
    let patterns = generate_variant_match_patterns(&ast.ident, data);
    let match_branches: Vec<TokenStream> = patterns
        .into_iter()
        .map(|mp| {
            let pattern = &mp.pattern;
            let name = &mp.variant_name;
            quote!( #pattern => #name )
        })
        .collect();

    let adt_name = &ast.ident;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    quote!(
        impl #impl_generics #adt_name #ty_generics #where_clause {
            pub fn variant_name(&self) -> &'static str {
                match self {
                    #(#match_branches,)*
                }
            }
        }
    )
}

/// Macro to derive a function `fn variant_index_arity(&self) -> (u32, usize)`
/// the pair (variant index, variant arity).
/// Only works on enumerations, of course.
pub fn derive_variant_index_arity(item: TokenStream) -> TokenStream {
    // Parse the input
    let ast: DeriveInput = parse2(item).unwrap();

    // Generate the code for the matches
    let Data::Enum(data) = &ast.data else {
        panic!("VariantIndex macro can only be called on enums");
    };
    let patterns = generate_variant_match_patterns(&ast.ident, data);
    let match_branches: Vec<TokenStream> = patterns
        .into_iter()
        .enumerate()
        .map(|(i, mp)| {
            let pattern = &mp.pattern;
            let i = i as u32;
            let arity = mp.num_args;
            quote!( #pattern => (#i, #arity) )
        })
        .collect();

    let adt_name = &ast.ident;
    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    quote!(
        impl #impl_generics #adt_name #ty_generics #where_clause {
            pub fn variant_index_arity(&self) -> (u32, usize) {
                match self {
                    #(#match_branches,)*
                }
            }
        }
    )
}

#[derive(PartialEq, Eq)]
pub enum EnumMethodKind {
    EnumIsA,
    EnumAsGetters,
    EnumAsMutGetters,
    EnumToGetters,
}

impl EnumMethodKind {
    /// We have to write this by hand: we can't use the macros defined above on
    /// the declarations of this file...
    fn variant_name(&self) -> &'static str {
        match self {
            EnumIsA => "EnumIsA",
            EnumAsGetters => "EnumAsGetters",
            EnumAsMutGetters => "EnumAsMutGetters",
            EnumToGetters => "EnumToGetters",
        }
    }
}

/// Generic helper for `EnumIsA` and `EnumAsGetters`.
/// This generates one function per variant.
pub fn derive_enum_variant_method(item: TokenStream, method_kind: EnumMethodKind) -> TokenStream {
    // Parse the input
    let ast: DeriveInput = parse2(item).unwrap();

    // Generate the code
    let adt_name = &ast.ident;

    // Generate the code for all the functions in the impl block
    let Data::Enum(data) = &ast.data else {
        panic!(
            "{} macro can only be called on enums",
            method_kind.variant_name()
        );
    };
    let patterns = generate_variant_match_patterns(&ast.ident, data);
    let methods: Vec<TokenStream> = patterns
        .into_iter()
        .map(|mp| {
            let pattern = &mp.pattern;
            let name_prefix = match method_kind {
                EnumIsA => "is_",
                EnumAsGetters | EnumAsMutGetters => "as_",
                EnumToGetters => "to_",
            };
            let name_suffix = match method_kind {
                EnumAsMutGetters => "_mut",
                _ => "",
            };
            let ref_kind = match method_kind {
                EnumAsGetters | EnumIsA => quote!(&),
                EnumAsMutGetters => quote!(&mut),
                EnumToGetters => quote!(),
            };
            // TODO: write our own to_snake_case function:
            // names like "i32" become "i_32" with this one.
            let variant_name = to_snake_case(&mp.variant_name);
            let method_name = format!("{name_prefix}{variant_name}{name_suffix}");
            let method_name = Ident::new(&method_name, Span::call_site());
            match method_kind {
                EnumIsA => {
                    quote!(
                        pub fn #method_name(#ref_kind self) -> bool {
                            #[allow(unreachable)]
                            match self {
                                #pattern => true,
                                _ => false,
                            }
                        }
                    )
                }
                EnumAsGetters | EnumAsMutGetters | EnumToGetters => {
                    let vars = &mp.pattern_vars;
                    let tys = &mp.arg_types;
                    quote!(
                        pub fn #method_name(#ref_kind self) -> Option<( #(#ref_kind #tys),* )> {
                            #[allow(unreachable)]
                            match self {
                                #pattern => Some(( #(#vars),* )),
                                _ => None,
                            }
                        }
                    )
                }
            }
        })
        .collect();

    let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
    quote!(
        impl #impl_generics #adt_name #ty_generics #where_clause {
            #(#methods)*
        }
    )
}

#[test]
fn test_snake_case() {
    let s = to_snake_case("ConstantValue");
    println!("{}", s);
    assert!(s == "constant_value");
}

#[test]
fn test_generics() {
    let s = quote!(
        enum Foo<T: Clone>
        where
            T: Copy,
        {
            Variant1(T),
            Variant2 { x: u32 },
            Variant3,
        }
    );
    assert_tokens_eq::assert_tokens_eq!(
        derive_variant_index_arity(s.clone()),
        quote! {
            impl<T: Clone,> Foo<T,>
            where
                T: Copy,
            {
                pub fn variant_index_arity(&self) -> (u32, usize) {
                    match self {
                        Foo::Variant1(_x0) => (0u32, 1usize),
                        Foo::Variant2 { x: _x0 } => (1u32, 1usize),
                        Foo::Variant3 => (2u32, 0usize),
                    }
                }
            }
        }
    );
    assert_tokens_eq::assert_tokens_eq!(
        derive_enum_variant_method(s, EnumAsMutGetters),
        quote! {
            impl<T: Clone,> Foo<T,>
            where
                T: Copy,
            {
                pub fn as_variant1_mut(&mut self) -> (&mut T) {
                    #[allow(unreachable)]
                    match self {
                        Foo::Variant1(_x0) => (_x0),
                        _ => unreachable!("Foo::as_variant1_mut: Not the proper variant"),
                    }
                }
                pub fn as_variant2_mut(&mut self) -> (&mut u32) {
                    #[allow(unreachable)]
                    match self {
                        Foo::Variant2 { x: _x0 } => (_x0),
                        _ => unreachable!("Foo::as_variant2_mut: Not the proper variant"),
                    }
                }
                pub fn as_variant3_mut(&mut self) -> () {
                    #[allow(unreachable)]
                    match self {
                        Foo::Variant3 => (),
                        _ => unreachable!("Foo::as_variant3_mut: Not the proper variant"),
                    }
                }
            }
        }
    );
}