syn_solidity/expr/
type.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
use crate::{kw, Spanned, Type};
use proc_macro2::Span;
use std::fmt;
use syn::{
    parenthesized,
    parse::{Parse, ParseStream},
    token::Paren,
    Result, Token,
};

/// A `type()` expression: `type(uint256)`
#[derive(Clone)]
pub struct ExprTypeCall {
    pub type_token: Token![type],
    pub paren_token: Paren,
    pub ty: Type,
}

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

impl Parse for ExprTypeCall {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            type_token: input.parse()?,
            paren_token: parenthesized!(content in input),
            ty: content.parse()?,
        })
    }
}

impl Spanned for ExprTypeCall {
    fn span(&self) -> Span {
        let span = self.type_token.span;
        span.join(self.paren_token.span.join()).unwrap_or(span)
    }

    fn set_span(&mut self, span: Span) {
        self.type_token.span = span;
        self.paren_token = Paren(span);
    }
}

/// A `new` expression: `new Contract`.
///
/// i.e. a contract creation or the allocation of a dynamic memory array.
#[derive(Clone)]
pub struct ExprNew {
    pub new_token: kw::new,
    pub ty: Type,
}

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

impl Parse for ExprNew {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        Ok(Self { new_token: input.parse()?, ty: input.parse()? })
    }
}

impl Spanned for ExprNew {
    fn span(&self) -> Span {
        let span = self.new_token.span;
        span.join(self.ty.span()).unwrap_or(span)
    }

    fn set_span(&mut self, span: Span) {
        self.new_token.span = span;
        self.ty.set_span(span);
    }
}