syn_solidity/yul/stmt/
if.rs

1use crate::{Spanned, YulBlock, YulExpr};
2use proc_macro2::Span;
3use std::fmt;
4use syn::{
5    parse::{Parse, ParseStream},
6    Result, Token,
7};
8
9/// A Yul if statement: `if lt(a, b) { sstore(0, 1) }`.
10///
11/// Reference:
12/// <https://docs.soliditylang.org/en/latest/grammar.html#a4.SolidityParser.yulIfStatement>
13#[derive(Clone)]
14pub struct YulIf {
15    pub if_token: Token![if],
16    pub cond: YulExpr,
17    pub then_branch: Box<YulBlock>,
18}
19
20impl Parse for YulIf {
21    fn parse(input: ParseStream<'_>) -> Result<Self> {
22        Ok(Self { if_token: input.parse()?, cond: input.parse()?, then_branch: input.parse()? })
23    }
24}
25
26impl Spanned for YulIf {
27    fn span(&self) -> Span {
28        let span = self.if_token.span;
29        span.join(self.then_branch.span()).unwrap_or(span)
30    }
31
32    fn set_span(&mut self, span: Span) {
33        self.if_token.set_span(span);
34        self.cond.set_span(span);
35        self.then_branch.set_span(span);
36    }
37}
38
39impl fmt::Debug for YulIf {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        f.debug_struct("YulIf")
42            .field("cond", &self.cond)
43            .field("then_branch", &self.then_branch)
44            .finish()
45    }
46}