syn_solidity/expr/
type.rs

1use crate::{kw, Spanned, Type};
2use proc_macro2::Span;
3use std::fmt;
4use syn::{
5    parenthesized,
6    parse::{Parse, ParseStream},
7    token::Paren,
8    Result, Token,
9};
10
11/// A `type()` expression: `type(uint256)`
12#[derive(Clone)]
13pub struct ExprTypeCall {
14    pub type_token: Token![type],
15    pub paren_token: Paren,
16    pub ty: Type,
17}
18
19impl fmt::Debug for ExprTypeCall {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        f.debug_struct("ExprTypeCall").field("ty", &self.ty).finish()
22    }
23}
24
25impl Parse for ExprTypeCall {
26    fn parse(input: ParseStream<'_>) -> Result<Self> {
27        let content;
28        Ok(Self {
29            type_token: input.parse()?,
30            paren_token: parenthesized!(content in input),
31            ty: content.parse()?,
32        })
33    }
34}
35
36impl Spanned for ExprTypeCall {
37    fn span(&self) -> Span {
38        let span = self.type_token.span;
39        span.join(self.paren_token.span.join()).unwrap_or(span)
40    }
41
42    fn set_span(&mut self, span: Span) {
43        self.type_token.span = span;
44        self.paren_token = Paren(span);
45    }
46}
47
48/// A `new` expression: `new Contract`.
49///
50/// i.e. a contract creation or the allocation of a dynamic memory array.
51#[derive(Clone)]
52pub struct ExprNew {
53    pub new_token: kw::new,
54    pub ty: Type,
55}
56
57impl fmt::Debug for ExprNew {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.debug_struct("ExprNew").field("ty", &self.ty).finish()
60    }
61}
62
63impl Parse for ExprNew {
64    fn parse(input: ParseStream<'_>) -> Result<Self> {
65        Ok(Self { new_token: input.parse()?, ty: input.parse()? })
66    }
67}
68
69impl Spanned for ExprNew {
70    fn span(&self) -> Span {
71        let span = self.new_token.span;
72        span.join(self.ty.span()).unwrap_or(span)
73    }
74
75    fn set_span(&mut self, span: Span) {
76        self.new_token.span = span;
77        self.ty.set_span(span);
78    }
79}