syn_solidity/item/
udt.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
use crate::{kw, SolIdent, Spanned, Type};
use proc_macro2::Span;
use std::{
    fmt,
    hash::{Hash, Hasher},
};
use syn::{
    parse::{Parse, ParseStream},
    Attribute, Result, Token,
};

/// A user-defined value type definition: `type Foo is uint256;`.
///
/// Solidity reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.userDefinedValueTypeDefinition>
#[derive(Clone)]
pub struct ItemUdt {
    pub attrs: Vec<Attribute>,
    pub type_token: Token![type],
    pub name: SolIdent,
    pub is_token: kw::is,
    pub ty: Type,
    pub semi_token: Token![;],
}

impl fmt::Display for ItemUdt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "type {} is {};", self.name, self.ty)
    }
}

impl fmt::Debug for ItemUdt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ItemUdt")
            .field("attrs", &self.attrs)
            .field("name", &self.name)
            .field("ty", &self.ty)
            .finish()
    }
}

impl PartialEq for ItemUdt {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name && self.ty == other.ty
    }
}

impl Eq for ItemUdt {}

impl Hash for ItemUdt {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.ty.hash(state);
    }
}

impl Parse for ItemUdt {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let this = Self {
            attrs: input.call(Attribute::parse_outer)?,
            type_token: input.parse()?,
            name: input.parse()?,
            is_token: input.parse()?,
            ty: input.parse()?,
            semi_token: input.parse()?,
        };

        // Solidity doesn't allow this, and it would cause ambiguity later on
        if this.ty.has_custom() {
            return Err(syn::Error::new(
                this.ty.span(),
                "the underlying type for a user-defined value type has to be an elementary value type",
            ));
        }

        Ok(this)
    }
}

impl Spanned for ItemUdt {
    fn span(&self) -> Span {
        self.name.span()
    }

    fn set_span(&mut self, span: Span) {
        self.type_token.span = span;
        self.name.set_span(span);
        self.is_token.span = span;
        self.ty.set_span(span);
        self.semi_token.span = span;
    }
}