about summary refs log tree commit diff
path: root/library/stdarch/crates/stdarch-gen-arm/src/expression.rs
blob: d5644ef27d4b1f926162233d36c096fc41e5e9ce (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
use itertools::Itertools;
use proc_macro2::{Literal, Punct, Spacing, TokenStream};
use quote::{ToTokens, TokenStreamExt, format_ident, quote};
use regex::Regex;
use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};
use std::fmt;
use std::str::FromStr;
use std::sync::LazyLock;

use crate::intrinsic::Intrinsic;
use crate::wildstring::WildStringPart;
use crate::{
    context::{self, Context, VariableType},
    intrinsic::{Argument, LLVMLink, StaticDefinition},
    matching::{MatchKindValues, MatchSizeValues},
    typekinds::{BaseType, BaseTypeKind, TypeKind},
    wildcards::Wildcard,
    wildstring::WildString,
};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum IdentifierType {
    Variable,
    Symbol,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum LetVariant {
    Basic(WildString, Box<Expression>),
    WithType(WildString, TypeKind, Box<Expression>),
    MutWithType(WildString, TypeKind, Box<Expression>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FnCall(
    /// Function pointer
    pub Box<Expression>,
    /// Function arguments
    pub Vec<Expression>,
    /// Function turbofish arguments
    #[serde(default)]
    pub Vec<Expression>,
    /// Function requires unsafe wrapper
    #[serde(default)]
    pub bool,
);

impl FnCall {
    pub fn new_expression(fn_ptr: Expression, arguments: Vec<Expression>) -> Expression {
        FnCall(Box::new(fn_ptr), arguments, Vec::new(), false).into()
    }

    pub fn new_unsafe_expression(fn_ptr: Expression, arguments: Vec<Expression>) -> Expression {
        FnCall(Box::new(fn_ptr), arguments, Vec::new(), true).into()
    }

    pub fn is_llvm_link_call(&self, llvm_link_name: &str) -> bool {
        self.is_expected_call(llvm_link_name)
    }

    pub fn is_target_feature_call(&self) -> bool {
        self.is_expected_call("target_feature")
    }

    pub fn is_expected_call(&self, fn_call_name: &str) -> bool {
        if let Expression::Identifier(fn_name, IdentifierType::Symbol) = self.0.as_ref() {
            fn_name.to_string() == fn_call_name
        } else {
            false
        }
    }

    pub fn pre_build(&mut self, ctx: &mut Context) -> context::Result {
        self.0.pre_build(ctx)?;
        self.1
            .iter_mut()
            .chain(self.2.iter_mut())
            .try_for_each(|ex| ex.pre_build(ctx))
    }

    pub fn build(&mut self, intrinsic: &Intrinsic, ctx: &mut Context) -> context::Result {
        self.0.build(intrinsic, ctx)?;
        self.1
            .iter_mut()
            .chain(self.2.iter_mut())
            .try_for_each(|ex| ex.build(intrinsic, ctx))
    }
}

impl ToTokens for FnCall {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let FnCall(fn_ptr, arguments, turbofish, _requires_unsafe_wrapper) = self;

        fn_ptr.to_tokens(tokens);

        if !turbofish.is_empty() {
            tokens.append_all(quote! {::<#(#turbofish),*>});
        }

        tokens.append_all(quote! { (#(#arguments),*) })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(remote = "Self", deny_unknown_fields)]
pub enum Expression {
    /// (Re)Defines a variable
    Let(LetVariant),
    /// Performs a variable assignment operation
    Assign(String, Box<Expression>),
    /// Performs a macro call
    MacroCall(String, String),
    /// Performs a function call
    FnCall(FnCall),
    /// Performs a method call. The following:
    /// `MethodCall: ["$object", "to_string", []]`
    /// is tokenized as:
    /// `object.to_string()`.
    MethodCall(Box<Expression>, String, Vec<Expression>),
    /// Symbol identifier name, prepend with a `$` to treat it as a scope variable
    /// which engages variable tracking and enables inference.
    /// E.g. `my_function_name` for a generic symbol or `$my_variable` for
    /// a variable.
    Identifier(WildString, IdentifierType),
    /// Constant signed integer number expression
    IntConstant(i32),
    /// Constant floating point number expression
    FloatConstant(f32),
    /// Constant boolean expression, either `true` or `false`
    BoolConstant(bool),
    /// Array expression
    Array(Vec<Expression>),

    // complex expressions
    /// Makes an LLVM link.
    ///
    /// It stores the link's function name in the wildcard `{llvm_link}`, for use in
    /// subsequent expressions.
    LLVMLink(LLVMLink),
    /// Casts the given expression to the specified (unchecked) type
    CastAs(Box<Expression>, String),
    /// Returns the LLVM `undef` symbol
    SvUndef,
    /// Multiplication
    Multiply(Box<Expression>, Box<Expression>),
    /// Xor
    Xor(Box<Expression>, Box<Expression>),
    /// Converts the specified constant to the specified type's kind
    ConvertConst(TypeKind, i32),
    /// Yields the given type in the Rust representation
    Type(TypeKind),

    MatchSize(TypeKind, MatchSizeValues<Box<Expression>>),
    MatchKind(TypeKind, MatchKindValues<Box<Expression>>),
}

impl Expression {
    pub fn pre_build(&mut self, ctx: &mut Context) -> context::Result {
        match self {
            Self::FnCall(fn_call) => fn_call.pre_build(ctx),
            Self::MethodCall(cl_ptr_ex, _, arg_exs) => {
                cl_ptr_ex.pre_build(ctx)?;
                arg_exs.iter_mut().try_for_each(|ex| ex.pre_build(ctx))
            }
            Self::Let(
                LetVariant::Basic(_, ex)
                | LetVariant::WithType(_, _, ex)
                | LetVariant::MutWithType(_, _, ex),
            ) => ex.pre_build(ctx),
            Self::CastAs(ex, _) => ex.pre_build(ctx),
            Self::Multiply(lhs, rhs) | Self::Xor(lhs, rhs) => {
                lhs.pre_build(ctx)?;
                rhs.pre_build(ctx)
            }
            Self::MatchSize(match_ty, values) => {
                *self = *values.get(match_ty, ctx.local)?.to_owned();
                self.pre_build(ctx)
            }
            Self::MatchKind(match_ty, values) => {
                *self = *values.get(match_ty, ctx.local)?.to_owned();
                self.pre_build(ctx)
            }
            _ => Ok(()),
        }
    }

    pub fn build(&mut self, intrinsic: &Intrinsic, ctx: &mut Context) -> context::Result {
        match self {
            Self::LLVMLink(link) => link.build_and_save(ctx),
            Self::Identifier(identifier, id_type) => {
                identifier.build_acle(ctx.local)?;

                if let IdentifierType::Variable = id_type {
                    ctx.local
                        .variables
                        .get(&identifier.to_string())
                        .map(|_| ())
                        .ok_or_else(|| format!("invalid variable {identifier} being referenced"))
                } else {
                    Ok(())
                }
            }
            Self::FnCall(fn_call) => {
                fn_call.build(intrinsic, ctx)?;

                #[allow(clippy::collapsible_if)]
                if let Some(llvm_link_name) = ctx.local.substitutions.get(&Wildcard::LLVMLink) {
                    if fn_call.is_llvm_link_call(llvm_link_name) {
                        *self = intrinsic
                            .llvm_link()
                            .expect("got LLVMLink wildcard without a LLVM link in `compose`")
                            .apply_conversions_to_call(fn_call.clone(), ctx)?
                    }
                }

                Ok(())
            }
            Self::MethodCall(cl_ptr_ex, _, arg_exs) => {
                cl_ptr_ex.build(intrinsic, ctx)?;
                arg_exs
                    .iter_mut()
                    .try_for_each(|ex| ex.build(intrinsic, ctx))
            }
            Self::Let(variant) => {
                let (var_name, ex, ty) = match variant {
                    LetVariant::Basic(var_name, ex) => (var_name, ex, None),
                    LetVariant::WithType(var_name, ty, ex)
                    | LetVariant::MutWithType(var_name, ty, ex) => {
                        if let Some(w) = ty.wildcard() {
                            ty.populate_wildcard(ctx.local.provide_type_wildcard(w)?)?;
                        }
                        (var_name, ex, Some(ty.to_owned()))
                    }
                };

                var_name.build_acle(ctx.local)?;
                ctx.local.variables.insert(
                    var_name.to_string(),
                    (
                        ty.unwrap_or_else(|| TypeKind::Custom("unknown".to_string())),
                        VariableType::Internal,
                    ),
                );
                ex.build(intrinsic, ctx)
            }
            Self::CastAs(ex, _) => ex.build(intrinsic, ctx),
            Self::Multiply(lhs, rhs) | Self::Xor(lhs, rhs) => {
                lhs.build(intrinsic, ctx)?;
                rhs.build(intrinsic, ctx)
            }
            Self::ConvertConst(ty, num) => {
                if let Some(w) = ty.wildcard() {
                    *ty = ctx.local.provide_type_wildcard(w)?
                }

                if let Some(BaseType::Sized(BaseTypeKind::Float, _)) = ty.base() {
                    *self = Expression::FloatConstant(*num as f32)
                } else {
                    *self = Expression::IntConstant(*num)
                }
                Ok(())
            }
            Self::Type(ty) => {
                if let Some(w) = ty.wildcard() {
                    *ty = ctx.local.provide_type_wildcard(w)?
                }

                Ok(())
            }
            _ => Ok(()),
        }
    }

    /// True if the expression requires an `unsafe` context in a safe function.
    ///
    /// The classification is somewhat fuzzy, based on actual usage (e.g. empirical function names)
    /// rather than a full parse. This is a reasonable approach because mistakes here will usually
    /// be caught at build time:
    ///
    ///  - Missing an `unsafe` is a build error.
    ///  - An unnecessary `unsafe` is a warning, made into an error by the CI's `-D warnings`.
    ///
    /// This **panics** if it encounters an expression that shouldn't appear in a safe function at
    /// all (such as `SvUndef`).
    pub fn requires_unsafe_wrapper(&self, ctx_fn: &str) -> bool {
        match self {
            // The call will need to be unsafe, but the declaration does not.
            Self::LLVMLink(..) => false,
            // Identifiers, literals and type names are never unsafe.
            Self::Identifier(..) => false,
            Self::IntConstant(..) => false,
            Self::FloatConstant(..) => false,
            Self::BoolConstant(..) => false,
            Self::Type(..) => false,
            Self::ConvertConst(..) => false,
            // Nested structures that aren't inherently unsafe, but could contain other expressions
            // that might be.
            Self::Assign(_var, exp) => exp.requires_unsafe_wrapper(ctx_fn),
            Self::Let(
                LetVariant::Basic(_, exp)
                | LetVariant::WithType(_, _, exp)
                | LetVariant::MutWithType(_, _, exp),
            ) => exp.requires_unsafe_wrapper(ctx_fn),
            Self::Array(exps) => exps.iter().any(|exp| exp.requires_unsafe_wrapper(ctx_fn)),
            Self::Multiply(lhs, rhs) | Self::Xor(lhs, rhs) => {
                lhs.requires_unsafe_wrapper(ctx_fn) || rhs.requires_unsafe_wrapper(ctx_fn)
            }
            Self::CastAs(exp, _ty) => exp.requires_unsafe_wrapper(ctx_fn),
            // Functions and macros can be unsafe, but can also contain other expressions.
            Self::FnCall(FnCall(fn_exp, args, turbo_args, requires_unsafe_wrapper)) => {
                let fn_name = fn_exp.to_string();
                fn_exp.requires_unsafe_wrapper(ctx_fn)
                    || fn_name.starts_with("_sv")
                    || fn_name.starts_with("simd_")
                    || fn_name.ends_with("transmute")
                    || args.iter().any(|exp| exp.requires_unsafe_wrapper(ctx_fn))
                    || turbo_args
                        .iter()
                        .any(|exp| exp.requires_unsafe_wrapper(ctx_fn))
                    || *requires_unsafe_wrapper
            }
            Self::MethodCall(exp, fn_name, args) => match fn_name.as_str() {
                // `as_signed` and `as_unsigned` are unsafe because they're trait methods with
                // target features to allow use on feature-dependent types (such as SVE vectors).
                // We can safely wrap them here.
                "as_signed" => true,
                "as_unsigned" => true,
                _ => {
                    exp.requires_unsafe_wrapper(ctx_fn)
                        || args.iter().any(|exp| exp.requires_unsafe_wrapper(ctx_fn))
                }
            },
            // We only use macros to check const generics (using static assertions).
            Self::MacroCall(_name, _args) => false,
            // Materialising uninitialised values is always unsafe, and we avoid it in safe
            // functions.
            Self::SvUndef => panic!("Refusing to wrap unsafe SvUndef in safe function '{ctx_fn}'."),
            // Variants that aren't tokenised. We shouldn't encounter these here.
            Self::MatchKind(..) => {
                unimplemented!("The unsafety of {self:?} cannot be determined in '{ctx_fn}'.")
            }
            Self::MatchSize(..) => {
                unimplemented!("The unsafety of {self:?} cannot be determined in '{ctx_fn}'.")
            }
        }
    }

    /// Determine if an expression is a `static_assert<...>` function call.
    pub fn is_static_assert(&self) -> bool {
        match self {
            Expression::FnCall(fn_call) => match fn_call.0.as_ref() {
                Expression::Identifier(wild_string, _) => {
                    if let WildStringPart::String(function_name) = &wild_string.0[0] {
                        function_name.starts_with("static_assert")
                    } else {
                        false
                    }
                }
                _ => panic!("Badly defined function call: {fn_call:?}"),
            },
            _ => false,
        }
    }

    /// Determine if an espression is a LLVM binding
    pub fn is_llvm_link(&self) -> bool {
        matches!(self, Expression::LLVMLink(_))
    }
}

impl FromStr for Expression {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        static MACRO_RE: LazyLock<Regex> =
            LazyLock::new(|| Regex::new(r"^(?P<name>[\w\d_]+)!\((?P<ex>.*?)\);?$").unwrap());

        if s == "SvUndef" {
            Ok(Expression::SvUndef)
        } else if MACRO_RE.is_match(s) {
            let c = MACRO_RE.captures(s).unwrap();
            let ex = c["ex"].to_string();
            let _: TokenStream = ex
                .parse()
                .map_err(|e| format!("could not parse macro call expression: {e:#?}"))?;
            Ok(Expression::MacroCall(c["name"].to_string(), ex))
        } else {
            let (s, id_type) = if let Some(varname) = s.strip_prefix('$') {
                (varname, IdentifierType::Variable)
            } else {
                (s, IdentifierType::Symbol)
            };
            let identifier = s.trim().parse()?;
            Ok(Expression::Identifier(identifier, id_type))
        }
    }
}

impl From<FnCall> for Expression {
    fn from(fn_call: FnCall) -> Self {
        Expression::FnCall(fn_call)
    }
}

impl From<WildString> for Expression {
    fn from(ws: WildString) -> Self {
        Expression::Identifier(ws, IdentifierType::Symbol)
    }
}

impl From<&Argument> for Expression {
    fn from(a: &Argument) -> Self {
        Expression::Identifier(a.name.to_owned(), IdentifierType::Variable)
    }
}

impl TryFrom<&StaticDefinition> for Expression {
    type Error = String;

    fn try_from(sd: &StaticDefinition) -> Result<Self, Self::Error> {
        match sd {
            StaticDefinition::Constant(imm) => Ok(imm.into()),
            StaticDefinition::Generic(t) => t.parse(),
        }
    }
}

impl fmt::Display for Expression {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Identifier(identifier, kind) => {
                write!(
                    f,
                    "{}{identifier}",
                    matches!(kind, IdentifierType::Variable)
                        .then_some("$")
                        .unwrap_or_default()
                )
            }
            Self::MacroCall(name, expression) => {
                write!(f, "{name}!({expression})")
            }
            _ => Err(fmt::Error),
        }
    }
}

impl ToTokens for Expression {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Let(LetVariant::Basic(var_name, exp)) => {
                let var_ident = format_ident!("{}", var_name.to_string());
                tokens.append_all(quote! { let #var_ident = #exp })
            }
            Self::Let(LetVariant::WithType(var_name, ty, exp)) => {
                let var_ident = format_ident!("{}", var_name.to_string());
                tokens.append_all(quote! { let #var_ident: #ty = #exp })
            }
            Self::Let(LetVariant::MutWithType(var_name, ty, exp)) => {
                let var_ident = format_ident!("{}", var_name.to_string());
                tokens.append_all(quote! { let mut #var_ident: #ty = #exp })
            }
            Self::Assign(var_name, exp) => {
                /* If we are dereferencing a variable to assign a value \
                 * the 'format_ident!' macro does not like the asterix */
                let var_name_str: &str;

                if let Some(ch) = var_name.chars().nth(0) {
                    /* Manually append the asterix and split out the rest of
                     * the variable name */
                    if ch == '*' {
                        tokens.append(Punct::new('*', Spacing::Alone));
                        var_name_str = &var_name[1..var_name.len()];
                    } else {
                        var_name_str = var_name.as_str();
                    }
                } else {
                    /* Should not be reached as you cannot have a variable
                     * without a name */
                    panic!("Invalid variable name, must be at least one character")
                }

                let var_ident = format_ident!("{}", var_name_str);
                tokens.append_all(quote! { #var_ident = #exp })
            }
            Self::MacroCall(name, ex) => {
                let name = format_ident!("{name}");
                let ex: TokenStream = ex.parse().unwrap();
                tokens.append_all(quote! { #name!(#ex) })
            }
            Self::FnCall(fn_call) => fn_call.to_tokens(tokens),
            Self::MethodCall(exp, fn_name, args) => {
                let fn_ident = format_ident!("{}", fn_name);
                tokens.append_all(quote! { #exp.#fn_ident(#(#args),*) })
            }
            Self::Identifier(identifier, _) => {
                assert!(
                    !identifier.has_wildcards(),
                    "expression {self:#?} was not built before calling to_tokens"
                );
                identifier
                    .to_string()
                    .parse::<TokenStream>()
                    .unwrap_or_else(|_| panic!("invalid syntax: {self:?}"))
                    .to_tokens(tokens);
            }
            Self::IntConstant(n) => tokens.append(Literal::i32_unsuffixed(*n)),
            Self::FloatConstant(n) => tokens.append(Literal::f32_unsuffixed(*n)),
            Self::BoolConstant(true) => tokens.append(format_ident!("true")),
            Self::BoolConstant(false) => tokens.append(format_ident!("false")),
            Self::Array(vec) => tokens.append_all(quote! { [ #(#vec),* ] }),
            Self::LLVMLink(link) => link.to_tokens(tokens),
            Self::CastAs(ex, ty) => {
                let ty: TokenStream = ty.parse().expect("invalid syntax");
                tokens.append_all(quote! { #ex as #ty })
            }
            Self::SvUndef => tokens.append_all(quote! { simd_reinterpret(()) }),
            Self::Multiply(lhs, rhs) => tokens.append_all(quote! { #lhs * #rhs }),
            Self::Xor(lhs, rhs) => tokens.append_all(quote! { #lhs ^ #rhs }),
            Self::Type(ty) => ty.to_tokens(tokens),
            _ => unreachable!("{self:?} cannot be converted to tokens."),
        }
    }
}

impl Serialize for Expression {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match self {
            Self::IntConstant(v) => serializer.serialize_i32(*v),
            Self::FloatConstant(v) => serializer.serialize_f32(*v),
            Self::BoolConstant(v) => serializer.serialize_bool(*v),
            Self::Identifier(..) => serializer.serialize_str(&self.to_string()),
            Self::MacroCall(..) => serializer.serialize_str(&self.to_string()),
            _ => Expression::serialize(self, serializer),
        }
    }
}

impl<'de> Deserialize<'de> for Expression {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct CustomExpressionVisitor;

        impl<'de> Visitor<'de> for CustomExpressionVisitor {
            type Value = Expression;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("integer, float, boolean, string or map")
            }

            fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(Expression::BoolConstant(v))
            }

            fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(Expression::IntConstant(v as i32))
            }

            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(Expression::IntConstant(v as i32))
            }

            fn visit_f64<E>(self, v: f64) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                Ok(Expression::FloatConstant(v as f32))
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: de::Error,
            {
                FromStr::from_str(value).map_err(de::Error::custom)
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: de::SeqAccess<'de>,
            {
                let arr = std::iter::from_fn(|| seq.next_element::<Self::Value>().transpose())
                    .try_collect()?;
                Ok(Expression::Array(arr))
            }

            fn visit_map<M>(self, map: M) -> Result<Expression, M::Error>
            where
                M: MapAccess<'de>,
            {
                // `MapAccessDeserializer` is a wrapper that turns a `MapAccess`
                // into a `Deserializer`, allowing it to be used as the input to T's
                // `Deserialize` implementation. T then deserializes itself using
                // the entries from the map visitor.
                Expression::deserialize(de::value::MapAccessDeserializer::new(map))
            }
        }

        deserializer.deserialize_any(CustomExpressionVisitor)
    }
}