alloy_sol_macro_expander/expand/
ty.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
327
328
329
330
//! [`Type`] expansion.

use super::{ExpCtxt, ExternCrates};
use ast::{Item, Parameters, Spanned, Type, TypeArray};
use proc_macro2::{Ident, Literal, TokenStream};
use proc_macro_error2::abort;
use quote::{quote_spanned, ToTokens};
use std::{fmt, num::NonZeroU16};

/// Expands a single [`Type`] recursively to its `alloy_sol_types::sol_data`
/// equivalent.
pub fn expand_type(ty: &Type, crates: &ExternCrates) -> TokenStream {
    let mut tokens = TokenStream::new();
    rec_expand_type(ty, crates, &mut tokens);
    tokens
}

/// Expands a single [`Type`] recursively to its Rust type equivalent.
///
/// This is the same as `<#expand_type(ty) as SolType>::RustType`, but generates
/// nicer code for documentation and IDE/LSP support when the type is not
/// ambiguous.
pub fn expand_rust_type(ty: &Type, crates: &ExternCrates) -> TokenStream {
    let mut tokens = TokenStream::new();
    rec_expand_rust_type(ty, crates, &mut tokens);
    tokens
}

/// The [`expand_type`] recursive implementation.
pub(super) fn rec_expand_type(ty: &Type, crates: &ExternCrates, tokens: &mut TokenStream) {
    let alloy_sol_types = &crates.sol_types;
    let tts = match *ty {
        Type::Address(span, _) => quote_spanned! {span=> #alloy_sol_types::sol_data::Address },
        Type::Bool(span) => quote_spanned! {span=> #alloy_sol_types::sol_data::Bool },
        Type::String(span) => quote_spanned! {span=> #alloy_sol_types::sol_data::String },
        Type::Bytes(span) => quote_spanned! {span=> #alloy_sol_types::sol_data::Bytes },

        Type::FixedBytes(span, size) => {
            assert!(size.get() <= 32);
            let size = Literal::u16_unsuffixed(size.get());
            quote_spanned! {span=> #alloy_sol_types::sol_data::FixedBytes<#size> }
        }
        Type::Int(span, size) | Type::Uint(span, size) => {
            let name = match ty {
                Type::Int(..) => "Int",
                Type::Uint(..) => "Uint",
                _ => unreachable!(),
            };
            let name = Ident::new(name, span);

            let size = size.map_or(256, NonZeroU16::get);
            assert!(size <= 256 && size % 8 == 0);
            let size = Literal::u16_unsuffixed(size);

            quote_spanned! {span=> #alloy_sol_types::sol_data::#name<#size> }
        }

        Type::Tuple(ref tuple) => {
            return tuple.paren_token.surround(tokens, |tokens| {
                for pair in tuple.types.pairs() {
                    let (ty, comma) = pair.into_tuple();
                    rec_expand_type(ty, crates, tokens);
                    comma.to_tokens(tokens);
                }
            })
        }
        Type::Array(ref array) => {
            let ty = expand_type(&array.ty, crates);
            let span = array.span();
            if let Some(size) = array.size() {
                quote_spanned! {span=> #alloy_sol_types::sol_data::FixedArray<#ty, #size> }
            } else {
                quote_spanned! {span=> #alloy_sol_types::sol_data::Array<#ty> }
            }
        }
        Type::Function(ref function) => quote_spanned! {function.span()=>
            #alloy_sol_types::sol_data::Function
        },
        Type::Mapping(ref mapping) => quote_spanned! {mapping.span()=>
            ::core::compile_error!("Mapping types are not supported here")
        },

        Type::Custom(ref custom) => {
            let segments = custom.iter();
            quote_spanned! {custom.span()=> #(#segments)::* }
        }
    };
    tokens.extend(tts);
}

// IMPORTANT: Keep in sync with `sol-types/src/types/data_type.rs`
/// The [`expand_rust_type`] recursive implementation.
pub(super) fn rec_expand_rust_type(ty: &Type, crates: &ExternCrates, tokens: &mut TokenStream) {
    let alloy_sol_types = &crates.sol_types;
    let tts = match *ty {
        Type::Address(span, _) => quote_spanned! {span=> #alloy_sol_types::private::Address },
        Type::Bool(span) => return Ident::new("bool", span).to_tokens(tokens),
        Type::String(span) => quote_spanned! {span=> #alloy_sol_types::private::String },
        Type::Bytes(span) => quote_spanned! {span=> #alloy_sol_types::private::Bytes },

        Type::FixedBytes(span, size) => {
            assert!(size.get() <= 32);
            let size = Literal::u16_unsuffixed(size.get());
            quote_spanned! {span=> #alloy_sol_types::private::FixedBytes<#size> }
        }
        Type::Int(span, size) | Type::Uint(span, size) => {
            let size = size.map_or(256, NonZeroU16::get);
            let primitive = matches!(size, 8 | 16 | 32 | 64 | 128);
            if primitive {
                let prefix = match ty {
                    Type::Int(..) => "i",
                    Type::Uint(..) => "u",
                    _ => unreachable!(),
                };
                return Ident::new(&format!("{prefix}{size}"), span).to_tokens(tokens);
            }
            let prefix = match ty {
                Type::Int(..) => "I",
                Type::Uint(..) => "U",
                _ => unreachable!(),
            };
            let name = Ident::new(&format!("{prefix}{size}"), span);
            quote_spanned! {span=> #alloy_sol_types::private::primitives::aliases::#name }
        }

        Type::Tuple(ref tuple) => {
            return tuple.paren_token.surround(tokens, |tokens| {
                for pair in tuple.types.pairs() {
                    let (ty, comma) = pair.into_tuple();
                    rec_expand_rust_type(ty, crates, tokens);
                    comma.to_tokens(tokens);
                }
            })
        }
        Type::Array(ref array) => {
            let ty = expand_rust_type(&array.ty, crates);
            let span = array.span();
            if let Some(size) = array.size() {
                quote_spanned! {span=> [#ty; #size] }
            } else {
                quote_spanned! {span=> #alloy_sol_types::private::Vec<#ty> }
            }
        }
        Type::Function(ref function) => quote_spanned! {function.span()=>
            #alloy_sol_types::private::Function
        },
        Type::Mapping(ref mapping) => quote_spanned! {mapping.span()=>
            ::core::compile_error!("Mapping types are not supported here")
        },

        // Exhaustive fallback to `SolType::RustType`
        Type::Custom(_) => {
            let span = ty.span();
            let ty = expand_type(ty, crates);
            quote_spanned! {span=> <#ty as #alloy_sol_types::SolType>::RustType }
        }
    };
    tokens.extend(tts);
}

/// Calculates the base ABI-encoded size of the given parameters in bytes.
///
/// See [`type_base_data_size`] for more information.
pub(super) fn params_base_data_size<P>(cx: &ExpCtxt<'_>, params: &Parameters<P>) -> usize {
    params.iter().map(|param| type_base_data_size(cx, &param.ty)).sum()
}

/// Recursively calculates the base ABI-encoded size of the given parameter
/// in bytes.
///
/// That is, the minimum number of bytes required to encode `self` without
/// any dynamic data.
pub(super) fn type_base_data_size(cx: &ExpCtxt<'_>, ty: &Type) -> usize {
    match ty {
        // static types: 1 word
        Type::Address(..)
        | Type::Bool(_)
        | Type::Int(..)
        | Type::Uint(..)
        | Type::FixedBytes(..)
        | Type::Function(_) => 32,

        // dynamic types: 1 offset word, 1 length word
        Type::String(_) | Type::Bytes(_) | Type::Array(TypeArray { size: None, .. }) => 64,

        // fixed array: size * encoded size
        Type::Array(a @ TypeArray { ty: inner, size: Some(_), .. }) => {
            type_base_data_size(cx, inner) * a.size().unwrap()
        }

        // tuple: sum of encoded sizes
        Type::Tuple(tuple) => tuple.types.iter().map(|ty| type_base_data_size(cx, ty)).sum(),

        Type::Custom(name) => match cx.try_item(name) {
            Some(Item::Contract(_)) | Some(Item::Enum(_)) => 32,
            Some(Item::Error(error)) => {
                error.parameters.types().map(|ty| type_base_data_size(cx, ty)).sum()
            }
            Some(Item::Event(event)) => {
                event.parameters.iter().map(|p| type_base_data_size(cx, &p.ty)).sum()
            }
            Some(Item::Struct(strukt)) => {
                strukt.fields.types().map(|ty| type_base_data_size(cx, ty)).sum()
            }
            Some(Item::Udt(udt)) => type_base_data_size(cx, &udt.ty),
            Some(item) => abort!(item.span(), "Invalid type in struct field: {:?}", item),
            None => 0,
        },

        // not applicable
        Type::Mapping(_) => 0,
    }
}

const MAX_SUPPORTED_ARRAY_LEN: usize = 32;
const MAX_SUPPORTED_TUPLE_LEN: usize = 12;

/// Returns whether the given type can derive the [`Default`] trait.
pub(super) fn can_derive_default(cx: &ExpCtxt<'_>, ty: &Type) -> bool {
    match ty {
        Type::Array(a) => {
            a.size().map_or(true, |sz| sz <= MAX_SUPPORTED_ARRAY_LEN)
                && can_derive_default(cx, &a.ty)
        }
        Type::Tuple(tuple) => {
            if tuple.types.len() > MAX_SUPPORTED_TUPLE_LEN {
                false
            } else {
                tuple.types.iter().all(|ty| can_derive_default(cx, ty))
            }
        }

        Type::Custom(name) => match cx.try_item(name) {
            Some(Item::Contract(_)) | Some(Item::Enum(_)) => false,
            Some(Item::Error(error)) => {
                error.parameters.types().all(|ty| can_derive_default(cx, ty))
            }
            Some(Item::Event(event)) => {
                event.parameters.iter().all(|p| can_derive_default(cx, &p.ty))
            }
            Some(Item::Struct(strukt)) => {
                strukt.fields.types().all(|ty| can_derive_default(cx, ty))
            }
            Some(Item::Udt(udt)) => can_derive_default(cx, &udt.ty),
            Some(item) => abort!(item.span(), "Invalid type in struct field: {:?}", item),
            _ => false,
        },

        _ => true,
    }
}

/// Returns whether the given type can derive the builtin traits listed in
/// `ExprCtxt::derives`, minus `Default`.
pub(super) fn can_derive_builtin_traits(cx: &ExpCtxt<'_>, ty: &Type) -> bool {
    match ty {
        Type::Array(a) => can_derive_builtin_traits(cx, &a.ty),
        Type::Tuple(tuple) => {
            if tuple.types.len() > MAX_SUPPORTED_TUPLE_LEN {
                false
            } else {
                tuple.types.iter().all(|ty| can_derive_builtin_traits(cx, ty))
            }
        }

        Type::Custom(name) => match cx.try_item(name) {
            Some(Item::Contract(_)) | Some(Item::Enum(_)) => true,
            Some(Item::Error(error)) => {
                error.parameters.types().all(|ty| can_derive_builtin_traits(cx, ty))
            }
            Some(Item::Event(event)) => {
                event.parameters.iter().all(|p| can_derive_builtin_traits(cx, &p.ty))
            }
            Some(Item::Struct(strukt)) => {
                strukt.fields.types().all(|ty| can_derive_builtin_traits(cx, ty))
            }
            Some(Item::Udt(udt)) => can_derive_builtin_traits(cx, &udt.ty),
            Some(item) => abort!(item.span(), "Invalid type in struct field: {:?}", item),
            _ => false,
        },

        _ => true,
    }
}

/// Implements [`fmt::Display`] which formats a [`Type`] to its canonical
/// representation. This is then used in function, error, and event selector
/// generation.
pub(super) struct TypePrinter<'ast> {
    cx: &'ast ExpCtxt<'ast>,
    ty: &'ast Type,
}

impl<'ast> TypePrinter<'ast> {
    pub(super) fn new(cx: &'ast ExpCtxt<'ast>, ty: &'ast Type) -> Self {
        Self { cx, ty }
    }
}

impl fmt::Display for TypePrinter<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.ty {
            Type::Int(_, None) => f.write_str("int256"),
            Type::Uint(_, None) => f.write_str("uint256"),

            Type::Array(array) => {
                Self::new(self.cx, &array.ty).fmt(f)?;
                f.write_str("[")?;
                if let Some(size) = array.size() {
                    size.fmt(f)?;
                }
                f.write_str("]")
            }
            Type::Tuple(tuple) => {
                f.write_str("(")?;
                for (i, ty) in tuple.types.iter().enumerate() {
                    if i > 0 {
                        f.write_str(",")?;
                    }
                    Self::new(self.cx, ty).fmt(f)?;
                }
                f.write_str(")")
            }

            Type::Custom(name) => Self::new(self.cx, self.cx.custom_type(name)).fmt(f),

            ty => ty.fmt(f),
        }
    }
}