stylus-proc 0.10.5

Procedural macros for stylus-sdk
Documentation
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Copyright 2022-2026, Offchain Labs, Inc.
// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/main/licenses/COPYRIGHT.md
use proc_macro2::{Span, TokenStream};
use proc_macro_error::emit_error;
use quote::{quote, ToTokens};
use syn::{
    parse::Nothing, parse_quote, parse_quote_spanned, parse_str, punctuated::Punctuated,
    spanned::Spanned, Token,
};

use super::Extension;
use crate::{
    consts::{STRUCT_SUFFIX_FOR_TRAITS_IN_EXPORT_ABI, STYLUS_CONTRACT_ADDRESS_FIELD},
    imports::{
        alloy_sol_types::SolType,
        stylus_sdk::abi::{AbiType, Router},
    },
    types::Purity,
};

/// Generate the code to call the special function (fallback, receive, or constructor) from the
/// public impl block. Emits an error if there are multiple implementations.
macro_rules! call_special {
    ($self:expr, $kind:pat, $kind_name:literal, $func:expr) => {{
        let specials: Vec<syn::Stmt> = $self
            .funcs
            .iter()
            .filter(|&func| matches!(func.kind, $kind))
            .map($func)
            .collect();
        if specials.is_empty() {
            None
        } else {
            if specials.len() > 1 {
                emit_error!(
                    concat!("multiple ", $kind_name),
                    concat!(
                        "contract can only have one #[",
                        $kind_name,
                        "] method defined"
                    )
                );
            }
            specials.first().cloned()
        }
    }};
}

pub struct PublicImpl<E: InterfaceExtension = Extension> {
    pub self_ty: syn::Type,
    pub generic_params: Punctuated<syn::GenericParam, Token![,]>,
    pub where_clause: Punctuated<syn::WherePredicate, Token![,]>,
    pub trait_: Option<syn::Path>,
    pub implements: Vec<syn::Type>,
    pub funcs: Vec<PublicFn<E::FnExt>>,
    pub associated_types: Vec<(syn::Ident, syn::Type)>,
    #[allow(dead_code)]
    pub extension: E,
}

pub struct PublicTrait<E: InterfaceExtension = Extension> {
    pub ident: syn::Ident,
    pub generic_params: Punctuated<syn::GenericParam, Token![,]>,
    pub where_clause: Punctuated<syn::WherePredicate, Token![,]>,
    pub funcs: Vec<PublicFn<E::FnExt>>,
    pub associated_types: Vec<(syn::Ident, Punctuated<syn::TypeParamBound, Token![+]>)>,
}

fn get_default_output(ty: &syn::Type) -> (TokenStream, TokenStream) {
    (
        quote! {
            Result<<<#ty as #AbiType>::SolType as #SolType>::RustType, stylus_sdk::stylus_core::calls::errors::Error>
        },
        quote! {
            Ok(<<#ty as #AbiType>::SolType as #SolType>::abi_decode_validate(&call_result)?)
        },
    )
}

fn get_client_funcs<E: InterfaceExtension>(
    funcs: &[PublicFn<E::FnExt>],
    public: bool,
) -> (Vec<proc_macro2::TokenStream>, Vec<proc_macro2::TokenStream>) {
    let (client_funcs_definitions, client_funcs_declarations): (
            Vec<proc_macro2::TokenStream>,
            Vec<proc_macro2::TokenStream>,
        ) = funcs
        .iter()
        .map(|func| {
            let func_name = func.name.clone();

            let (context, call) = func.purity.get_context_and_call();

            let inputs = func.inputs.iter().map(|input| {
                let name = input.name.clone();
                let ty = input.ty.clone();
                quote! { #name: #ty }
            });
            let inputs_names = func.inputs.iter().map(|input| {
                input.name.clone()
            });
            let inputs_types = func.inputs.iter().map(|input| {
                let ty = input.ty.clone();
                quote! { #ty }
            });

            let (output_type, output_decoding) = get_output_type_and_decoding(&func.output);

            let function_selector = func.function_selector();

            let funcs_visibility = if public { quote! { pub } } else { quote! {} };

            let signature = quote! {
                #funcs_visibility fn #func_name(
                    &self,
                    host: &impl stylus_sdk::stylus_core::host::Host,
                    context: impl #context,
                    #(#inputs,)*
                ) -> #output_type
            };

            let definition = quote! {
                #signature {
                    let inputs = <<(#(#inputs_types,)*) as #AbiType>::SolType as #SolType>::abi_encode_params(&(#(#inputs_names,)*));
                    use stylus_sdk::function_selector;
                    let mut calldata = Vec::from(#function_selector);
                    calldata.extend(inputs);
                    let call_result = #call(host, context, self.#STYLUS_CONTRACT_ADDRESS_FIELD, &calldata)?;
                    #output_decoding
                }
            };
            let declaration = quote! {
                #signature;
            };
            (definition, declaration)
        })
        .unzip();
    (client_funcs_definitions, client_funcs_declarations)
}

fn get_output_type_and_decoding(output: &syn::ReturnType) -> (TokenStream, TokenStream) {
    match output {
        syn::ReturnType::Default => (
            quote! { Result<(), stylus_sdk::stylus_core::calls::errors::Error> },
            quote! { Ok(()) },
        ),
        syn::ReturnType::Type(_, ty) => {
            // Check if it's a path type (like Result<T, E> or ArbResult)
            let type_path = match ty.as_ref() {
                syn::Type::Path(type_path) => type_path,
                _ => return get_default_output(ty),
            };

            // Check if the path is "Result" or "ArbResult"
            let segment = match type_path.path.segments.last() {
                Some(segment) => segment,
                None => {
                    emit_error!(ty.span(), "Expected a type path with segments, found none");
                    return get_default_output(ty);
                }
            };
            match segment.ident.to_string().as_str() {
                "ArbResult" => (
                    quote! {
                        stylus_sdk::ArbResult
                    },
                    quote! {
                        let decoded = <<Vec<u8> as #AbiType>::SolType as #SolType>::abi_decode_validate(&call_result);
                        match decoded {
                            Ok(decoded) => Ok(decoded),
                            Err(err) => Err("unable to decode to Vec<u8>".into()),
                        }
                    },
                ),
                "Result" => {
                    // Extract the generic arguments
                    let args = match &segment.arguments {
                        syn::PathArguments::AngleBracketed(args) => args,
                        _ => {
                            emit_error!(
                                ty.span(),
                                "Expected Result to have generic arguments, found none"
                            );
                            return get_default_output(ty);
                        }
                    };

                    // Get the first generic argument (T in Result<T, E>)
                    if args.args.is_empty() {
                        emit_error!(
                            ty.span(),
                            "Expected Result to have at least one generic argument"
                        );
                        return get_default_output(ty);
                    }
                    if let syn::GenericArgument::Type(ok_type) = &args.args[0] {
                        get_default_output(ok_type)
                    } else {
                        emit_error!(
                            ty.span(),
                            "Expected Result to have a type as the first generic argument"
                        );
                        get_default_output(ty)
                    }
                }
                _ => get_default_output(ty),
            }
        }
    }
}

impl PublicTrait {
    pub fn contract_client_gen(&self) -> proc_macro2::TokenStream {
        let (_, client_funcs_declarations) = get_client_funcs::<Extension>(&self.funcs, false);

        let associated_types_declarations: Vec<proc_macro2::TokenStream> = self
            .associated_types
            .iter()
            .map(|(name, original_bounds)| {
                if original_bounds.is_empty() {
                    quote! { type #name: #AbiType; }
                } else {
                    quote! { type #name: #original_bounds + #AbiType; }
                }
            })
            .collect();

        let ident = &self.ident;

        let generic_params = if self.generic_params.is_empty() {
            quote! {}
        } else {
            let generic_params = &self.generic_params;
            quote! { <#generic_params> }
        };

        let where_clause = if self.where_clause.is_empty() {
            quote! {}
        } else {
            let where_clause = &self.where_clause;
            quote! { where #where_clause }
        };

        let output = quote! {
            #[cfg(feature = "contract-client-gen")]
            pub trait #ident #generic_params #where_clause {
                #(#associated_types_declarations)*
                #(#client_funcs_declarations)*
            }
        };
        output
    }
}

/// Generate pairwise compile-time selector collision checks for a set of functions.
///
/// Each check is emitted as a separate `const _: () = { ... }` item so the compiler
/// reports all collisions independently rather than stopping at the first.
///
/// Uses `const` assertions because selector values are computed by the `function_selector!`
/// declarative macro after proc-macro expansion — the proc macro has no access to concrete
/// selector bytes at the time it runs. The `function_selector!` macro expands to a
/// const-evaluable keccak computation, making the `const` assertion viable.
///
/// Generates n*(n-1)/2 const assertion items (one per pair), where n is the number
/// of regular (`FnKind::Function`) entries in the input — fallback, receive, and
/// constructor route separately and are excluded.
///
/// **Not checked:** collisions across separate `#[public]`-annotated items (proc macros
/// cannot share state across invocations), and collisions with methods inherited via
/// `#[implements]` (only trait type paths are available — the inherited trait's function
/// signatures are not accessible during proc-macro expansion).
/// See the `#[public]` macro documentation in `lib.rs` for user-facing details.
pub(super) fn selector_collision_checks<E: FnExtension>(funcs: &[PublicFn<E>]) -> Vec<syn::Item> {
    let functions = regular_functions(funcs);

    // Collect per-function data used in the pairwise comparison below.
    let func_data: Vec<_> = functions
        .iter()
        .map(|f| {
            (
                f.selector_const(),
                f.selector_name(),
                f.display_label(),
                f.name.span(),
            )
        })
        .collect();
    let n = func_data.len();
    let mut checks: Vec<syn::Item> = Vec::with_capacity(n * n.saturating_sub(1) / 2);
    for (i, (const_a, sel_a, label_a, _)) in func_data.iter().enumerate() {
        for (const_b, sel_b, label_b, span) in &func_data[i + 1..] {
            let msg = format!(
                "Stylus SDK: ABI selector collision: {label_a} and {label_b} produce the same \
                 4-byte selector. Use #[selector(name = \"...\")] to assign a distinct name, \
                 or rename one",
            );
            // The cfg gate is intentional: `contract-client-gen` generates call stubs for
            // external contracts whose ABI the user does not control. Blocking compilation
            // over a selector collision in a foreign interface would be unhelpful — the
            // collision only matters for routing, which is not emitted in contract-client-gen mode.
            checks.push(parse_quote_spanned! { *span =>
                #[cfg(not(feature = "contract-client-gen"))]
                const _: () = {
                    use stylus_sdk::function_selector;
                    #const_a
                    #const_b
                    assert!(#sel_a != #sel_b, #msg);
                };
            });
        }
    }
    checks
}

/// Returns only `FnKind::Function` entries, excluding fallback, receive, and constructor.
fn regular_functions<E: FnExtension>(funcs: &[PublicFn<E>]) -> Vec<&PublicFn<E>> {
    funcs
        .iter()
        .filter(|f| matches!(f.kind, FnKind::Function))
        .collect()
}

impl PublicImpl {
    pub fn impl_router(&self) -> syn::ItemImpl {
        let Self {
            self_ty,
            generic_params,
            where_clause,
            ..
        } = self;
        let functions = regular_functions(&self.funcs);
        let selector_consts = functions.iter().map(|f| f.selector_const());
        let selector_arms: Vec<_> = functions.iter().map(|f| f.selector_arm()).collect();

        let fallback = call_special!(
            self,
            FnKind::Fallback { .. },
            "fallback",
            PublicFn::call_fallback
        );
        let fallback = fallback.unwrap_or_else(|| parse_quote!({ None }));

        let receive = call_special!(self, FnKind::Receive, "receive", PublicFn::call_receive);
        let receive = receive.unwrap_or_else(|| parse_quote!({ None }));

        let call_constructor = call_special!(
            self,
            FnKind::Constructor,
            "constructor",
            PublicFn::call_constructor
        );
        let constructor = call_constructor.unwrap_or_else(|| parse_quote!({ None }));

        let implements_routes = self.implements_routes();

        // Determine trait dynamic interface with associated types
        let iface = match &self.trait_ {
            Some(trait_) => {
                // If trait_ is something like foo::MyTrait<u32, u256>, trait_path_without_generics
                // will be foo::MyTrait
                let trait_path_without_generics = {
                    let mut path = trait_.clone();
                    if let Some(last_segment) = path.segments.last_mut() {
                        last_segment.arguments = syn::PathArguments::None;
                    }
                    path
                };

                // Extract generic arguments from trait_ if present (e.g., "u32, u256" from the
                // previous example)
                let generic_args: Vec<proc_macro2::TokenStream> =
                    if let Some(last_segment) = trait_.segments.last() {
                        if let syn::PathArguments::AngleBracketed(args) = &last_segment.arguments {
                            args.args.iter().map(|arg| quote::quote! { #arg }).collect()
                        } else {
                            // No generic arguments
                            Vec::new()
                        }
                    } else {
                        Vec::new()
                    };

                let associated_types: Vec<proc_macro2::TokenStream> = self
                    .associated_types
                    .iter()
                    .map(|(name, value)| quote::quote! { #name = #value })
                    .collect();

                let combined_types = if !generic_args.is_empty() && !associated_types.is_empty() {
                    quote! { < #(#generic_args),* , #(#associated_types),* > }
                } else if !generic_args.is_empty() {
                    quote! { < #(#generic_args),* > }
                } else if !associated_types.is_empty() {
                    quote! { < #(#associated_types),* > }
                } else {
                    quote! {}
                };

                &parse_quote! { dyn #trait_path_without_generics  #combined_types }
            }
            None => self_ty,
        };

        parse_quote! {
            #[cfg(not(feature = "contract-client-gen"))]
            impl<S, #generic_params> #Router<S, #iface> for #self_ty
            where
                S: stylus_sdk::stylus_core::storage::TopLevelStorage + core::borrow::BorrowMut<Self> + stylus_sdk::stylus_core::ValueDenier + stylus_sdk::stylus_core::ConstructorGuard,
                #where_clause
            {
                type Storage = Self;

                #[inline(always)]
                #[deny(unreachable_patterns)]
                fn route(storage: &mut S, selector: u32, input: &[u8]) -> Option<stylus_sdk::ArbResult> {
                    use stylus_sdk::function_selector;
                    use stylus_sdk::abi::{internal, internal::EncodableReturnType};
                    use alloc::vec;

                    #(#selector_consts)*
                    match selector {
                        #(#selector_arms)*
                        _ => {
                            #(#implements_routes)*
                            None
                        }
                    }
                }

                #[inline(always)]
                fn fallback(storage: &mut S, input: &[u8]) -> Option<stylus_sdk::ArbResult> {
                    #fallback
                }

                #[inline(always)]
                fn receive(storage: &mut S) -> Option<Result<(), Vec<u8>>> {
                    #receive
                }

                #[inline(always)]
                fn constructor(storage: &mut S, input: &[u8]) -> Option<stylus_sdk::ArbResult> {
                    #constructor
                }
            }
        }
    }

    fn implements_routes(&self) -> impl Iterator<Item = syn::ExprIf> + '_ {
        let self_ty = self.self_ty.clone();
        self.implements.iter().map(move |ty| {
            parse_quote! {
                if let Some(result) = <#self_ty as #Router<S, dyn #ty>>::route(storage, selector, input) {
                    return Some(result);
                }
            }
        })
    }

    // For each trait T tagged as #[public], a struct TStylusAbiStruct is generated
    // when the "export-abi" feature is enabled. This struct will later be bounded
    // to the GenerateAbi trait, to then be able to output the solidity ABI related to
    // the trait T.
    pub fn struct_for_export_abi(&self) -> proc_macro2::TokenStream {
        if self.trait_.is_none() {
            return quote! {};
        }

        let trait_name = self
            .trait_
            .as_ref()
            .unwrap()
            .segments
            .last()
            .unwrap()
            .ident
            .to_string();
        let ident = syn::Ident::new(
            &format!("{trait_name}{STRUCT_SUFFIX_FOR_TRAITS_IN_EXPORT_ABI}"),
            Span::call_site(),
        );
        quote! {
            #[cfg(feature = "export-abi")]
            pub struct #ident;
        }
    }

    pub fn print_from_args_fn(&self) -> proc_macro2::TokenStream {
        if self.trait_.is_some() {
            return quote! {};
        }
        if !self.generic_params.is_empty() {
            return quote! {};
        }

        // if self represents a `impl MyStruct { ... }`, that can be tagged with a #implements
        // attribute, then we want to generate print_from_args.
        let self_ty = &self.self_ty;
        let implements = self.implements.iter().map(|ty| {
            let in_type_name = match ty {
                syn::Type::Path(path) => path.path.segments.last().unwrap().ident.to_string(),
                _ => todo!(),
            };
            let out_type_name = format!("{in_type_name}{STRUCT_SUFFIX_FOR_TRAITS_IN_EXPORT_ABI}");
            let ty: syn::Type =
                parse_str(&out_type_name).expect("Failed to parse string into a syn::Type");
            ty
        });
        quote! {
            #[cfg(feature = "export-abi")]
            pub fn print_from_args() {
                stylus_sdk::abi::export::handle_license_and_pragma();
                stylus_sdk::abi::export::print_from_args::<#self_ty>();
                #(stylus_sdk::abi::export::print_from_args::<#implements>();)*
            }
        }
    }

    pub fn contract_client_gen(&self) -> proc_macro2::TokenStream {
        let (client_funcs_definitions, _) =
            get_client_funcs::<Extension>(&self.funcs, self.trait_.is_none());

        let associated_types_definitions: Vec<proc_macro2::TokenStream> = self
            .associated_types
            .iter()
            .map(|(name, value)| {
                let definition = quote::quote! { type #name = #value; };
                definition
            })
            .collect();

        let struct_path = self.self_ty.clone();

        let output = if let Some(trait_path) = &self.trait_ {
            quote! {
                #[cfg(feature = "contract-client-gen")]
                #[allow(non_snake_case)]
                impl #trait_path for #struct_path {
                    #(#associated_types_definitions)*
                    #(#client_funcs_definitions)*
                }
            }
        } else {
            let generic_params = &self.generic_params;
            let where_clause = &self.where_clause;
            // If the impl does not implement a trait, we just output the functions directly,
            // and also add a constructor for the contract client
            quote! {
                #[cfg(feature = "contract-client-gen")]
                #[allow(clippy::needless_update)]
                #[allow(non_snake_case)]
                impl<#generic_params> #struct_path #where_clause {
                    pub fn new(address: stylus_sdk::alloy_primitives::Address) -> Self {
                        Self {
                            #STYLUS_CONTRACT_ADDRESS_FIELD: address,
                            ..Default::default()
                        }
                    }

                    #(#client_funcs_definitions)*
                }
            }
        };
        output
    }
}

#[derive(Debug)]
pub enum FnKind {
    Function,
    Fallback { with_args: bool },
    Receive,
    Constructor,
}

pub struct PublicFn<E: FnExtension> {
    pub name: syn::Ident,
    pub sol_name: syn_solidity::SolIdent,
    pub purity: Purity,
    pub inferred_purity: Purity,
    pub kind: FnKind,

    pub has_self: bool,
    pub inputs: Vec<PublicFnArg<E::FnArgExt>>,
    pub input_span: Span,
    pub output: syn::ReturnType,
    pub output_span: Span,

    #[allow(dead_code)]
    pub extension: E,
}

impl<E: FnExtension> PublicFn<E> {
    /// Returns a display label for error messages, including the ABI name when it differs
    /// from the Rust function name.
    pub(super) fn display_label(&self) -> String {
        let fn_name = self.name.to_string();
        let sol_name = self.sol_name.as_string();
        if fn_name == sol_name {
            format!("`{fn_name}`")
        } else {
            format!("`{fn_name}` (ABI name `{sol_name}`)")
        }
    }

    pub fn function_selector(&self) -> syn::Expr {
        let sol_name = syn::LitStr::new(&self.sol_name.as_string(), self.sol_name.span());
        let arg_types = self.arg_types();
        parse_quote! {
            function_selector!(#sol_name #(, #arg_types )*)
        }
    }

    pub fn selector_name(&self) -> syn::Ident {
        syn::Ident::new(&format!("__SELECTOR_{}", self.name), self.name.span())
    }

    /// Returns the `const` item declaring this function's 4-byte ABI selector.
    ///
    /// **Precondition:** must only be called on `FnKind::Function` entries.
    /// Use `regular_functions()` to filter before calling.
    pub fn selector_const(&self) -> syn::ItemConst {
        debug_assert!(matches!(self.kind, FnKind::Function));
        let name = self.selector_name();
        let function_selector = self.function_selector();
        parse_quote! {
            #[allow(non_upper_case_globals)]
            const #name: u32 = u32::from_be_bytes(#function_selector);
        }
    }

    /// Returns the `match` arm that routes this function's selector to its handler.
    ///
    /// **Precondition:** must only be called on `FnKind::Function` entries.
    /// Use `regular_functions()` to filter before calling.
    fn selector_arm(&self) -> syn::Arm {
        debug_assert!(matches!(self.kind, FnKind::Function));
        let name = &self.name;
        let constant = self.selector_name();
        let deny_value = self.deny_value();
        let decode_inputs = self.decode_inputs();
        let storage_arg = self.storage_arg();
        let expand_args = self.expand_args();
        let encode_output = self.encode_output();
        parse_quote! {
            #[allow(non_upper_case_globals)]
            #constant => {
                #deny_value
                let args = match <#decode_inputs as #SolType>::abi_decode_params_validate(input) {
                    Ok(args) => args,
                    Err(err) => {
                        internal::failed_to_decode_arguments(err);
                        return Some(Err(Vec::new()));
                    }
                };
                let result = Self::#name(#storage_arg #(#expand_args, )* );
                Some(#encode_output)
            }
        }
    }

    fn decode_inputs(&self) -> syn::Type {
        let arg_types = self.arg_types();
        parse_quote_spanned! {
            self.input_span => <(#( #arg_types, )*) as #AbiType>::SolType
        }
    }

    fn arg_types(&self) -> impl Iterator<Item = &syn::Type> {
        self.inputs.iter().map(|arg| &arg.ty)
    }

    fn storage_arg(&self) -> TokenStream {
        if self.inferred_purity == Purity::Pure {
            quote!()
        } else if self.has_self {
            quote! { core::borrow::BorrowMut::borrow_mut(storage), }
        } else {
            quote! { storage, }
        }
    }

    fn expand_args(&self) -> impl Iterator<Item = syn::Expr> + '_ {
        self.arg_types().enumerate().map(|(index, ty)| {
            let index = syn::Index {
                index: index as u32,
                span: ty.span(),
            };
            parse_quote! { args.#index }
        })
    }

    fn encode_output(&self) -> syn::Expr {
        parse_quote_spanned! {
            self.output_span => EncodableReturnType::encode(result)
        }
    }

    fn deny_value(&self) -> Option<syn::ExprIf> {
        if self.purity == Purity::Payable {
            None
        } else {
            let name = self.name.to_string();
            Some(parse_quote! {
                if let Err(err) = storage.deny_value(#name) {
                    return Some(Err(err));
                }
            })
        }
    }

    fn call_fallback(&self) -> syn::Stmt {
        let deny_value = self.deny_value();
        let name = &self.name;
        let storage_arg = self.storage_arg();
        let call: syn::Stmt = if matches!(self.kind, FnKind::Fallback { with_args: false }) {
            parse_quote! {
                return Some({
                    if let Err(err) = Self::#name(#storage_arg) {
                        Err(err)
                    } else {
                        Ok(Vec::new())
                    }
                });
            }
        } else {
            parse_quote! {
                return Some(Self::#name(#storage_arg input));
            }
        };
        parse_quote!({
            #deny_value
            #call
        })
    }

    fn call_receive(&self) -> syn::Stmt {
        let name = &self.name;
        let storage_arg = self.storage_arg();
        parse_quote! {
            return Some(Self::#name(#storage_arg));
        }
    }

    fn call_constructor(&self) -> syn::Stmt {
        let deny_value = self.deny_value();
        let name = &self.name;
        let decode_inputs = self.decode_inputs();
        let storage_arg = self.storage_arg();
        let expand_args = self.expand_args();
        let encode_output = self.encode_output();
        parse_quote!({
            use stylus_sdk::abi::{internal, internal::EncodableReturnType};
            #deny_value
            if let Err(e) = storage.check_constructor_slot() {
                return Some(Err(e));
            }
            let args = match <#decode_inputs as #SolType>::abi_decode_params_validate(input) {
                Ok(args) => args,
                Err(err) => {
                    internal::failed_to_decode_arguments(err);
                    return Some(Err(Vec::new()));
                }
            };
            let result = Self::#name(#storage_arg #(#expand_args, )* );
            Some(#encode_output)
        })
    }
}

pub struct PublicFnArg<E: FnArgExtension> {
    pub ty: syn::Type,
    pub name: syn::Ident,
    #[allow(dead_code)]
    pub extension: E,
}

pub trait InterfaceExtension: Sized {
    type FnExt: FnExtension;
    type Ast: ToTokens;

    fn build(node: &syn::ItemImpl) -> Self;
    fn codegen(iface: &PublicImpl<Self>) -> Self::Ast;
}

pub trait FnExtension {
    type FnArgExt: FnArgExtension;

    fn build(node: &syn::ImplItemFn) -> Self;
}

pub trait FnArgExtension {
    fn build(node: &syn::FnArg) -> Self;
}

impl InterfaceExtension for () {
    type FnExt = ();
    type Ast = Nothing;

    fn build(_node: &syn::ItemImpl) -> Self {}

    fn codegen(_iface: &PublicImpl<Self>) -> Self::Ast {
        Nothing
    }
}

impl FnExtension for () {
    type FnArgExt = ();

    fn build(_node: &syn::ImplItemFn) -> Self {}
}

impl FnArgExtension for () {
    fn build(_node: &syn::FnArg) -> Self {}
}