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
use crate::{utils::DebugPunctuated, Spanned, YulEVMBuiltIn, YulExpr, YulIdent};
use proc_macro2::Span;
use std::fmt;
use syn::{
    parenthesized,
    parse::{discouraged::Speculative, Parse, ParseStream, Result},
    punctuated::Punctuated,
    token::Paren,
    Token,
};

/// Yul function call.
///
/// Solidity Reference:
/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.yulFunctionCall>
#[derive(Clone)]
pub struct YulFnCall {
    pub function_type: FnType,
    pub paren_token: Paren,
    pub arguments: Punctuated<YulExpr, Token![,]>,
}

impl Parse for YulFnCall {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let content;
        Ok(Self {
            function_type: input.parse()?,
            paren_token: parenthesized!(content in input),
            arguments: content.parse_terminated(YulExpr::parse, Token![,])?,
        })
    }
}

impl Spanned for YulFnCall {
    fn span(&self) -> Span {
        let span = self.function_type.span();
        span.join(self.arguments.span()).unwrap_or(span)
    }

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

impl fmt::Debug for YulFnCall {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("YulFnCall")
            .field("function_type", &self.function_type)
            .field("arguments", DebugPunctuated::new(&self.arguments))
            .finish()
    }
}

/// What type of function is called.
#[derive(Clone)]
pub enum FnType {
    /// When calling a self defined function
    Custom(YulIdent),

    /// When calling a built in evm opcode
    EVMOpcode(YulEVMBuiltIn),
}

impl Parse for FnType {
    fn parse(input: ParseStream<'_>) -> Result<Self> {
        let speculative_parse = input.fork();

        if let Ok(evm_builtin) = speculative_parse.parse::<YulEVMBuiltIn>() {
            input.advance_to(&speculative_parse);
            Ok(Self::EVMOpcode(evm_builtin))
        } else {
            Ok(Self::Custom(input.parse::<YulIdent>()?))
        }
    }
}

impl Spanned for FnType {
    fn span(&self) -> Span {
        match self {
            Self::Custom(custom) => custom.span(),
            Self::EVMOpcode(opcode) => opcode.span(),
        }
    }

    fn set_span(&mut self, span: Span) {
        match self {
            Self::Custom(custom) => custom.set_span(span),
            Self::EVMOpcode(opcode) => opcode.set_span(span),
        }
    }
}

impl fmt::Debug for FnType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("FnType::")?;
        match self {
            Self::Custom(custom) => custom.fmt(f),
            Self::EVMOpcode(opcode) => opcode.fmt(f),
        }
    }
}