circomspect_program_structure/abstract_syntax_tree/
ast_shortcuts.rs

1use super::ast::*;
2use super::expression_builders::*;
3use super::statement_builders::*;
4use crate::ast::{Access, Expression, VariableType};
5use num_bigint::BigInt;
6
7#[derive(Clone)]
8pub struct Symbol {
9    pub name: String,
10    pub is_array: Vec<Expression>,
11    pub init: Option<Expression>,
12}
13
14pub struct TupleInit {
15    pub tuple_init: (AssignOp, Expression),
16}
17
18pub fn assign_with_op_shortcut(
19    op: ExpressionInfixOpcode,
20    meta: Meta,
21    variable: (String, Vec<Access>),
22    rhe: Expression,
23) -> Statement {
24    let (var, access) = variable;
25    let variable = build_variable(meta.clone(), var.clone(), access.clone());
26    let infix = build_infix(meta.clone(), variable, op, rhe);
27    build_substitution(meta, var, access, AssignOp::AssignVar, infix)
28}
29
30pub fn plusplus(meta: Meta, variable: (String, Vec<Access>)) -> Statement {
31    let one = build_number(meta.clone(), BigInt::from(1));
32    assign_with_op_shortcut(ExpressionInfixOpcode::Add, meta, variable, one)
33}
34
35pub fn subsub(meta: Meta, variable: (String, Vec<Access>)) -> Statement {
36    let one = build_number(meta.clone(), BigInt::from(1));
37    assign_with_op_shortcut(ExpressionInfixOpcode::Sub, meta, variable, one)
38}
39
40pub fn for_into_while(
41    meta: Meta,
42    init: Statement,
43    cond: Expression,
44    step: Statement,
45    body: Statement,
46) -> Statement {
47    let while_body = build_block(body.get_meta().clone(), vec![body, step]);
48    let while_statement = build_while_block(meta.clone(), cond, while_body);
49    build_block(meta, vec![init, while_statement])
50}
51
52pub fn split_declaration_into_single_nodes(
53    meta: Meta,
54    xtype: VariableType,
55    symbols: Vec<Symbol>,
56    op: AssignOp,
57) -> Statement {
58    // use crate::ast_shortcuts::VariableType::Var;
59    let mut initializations = Vec::new();
60
61    for symbol in symbols {
62        let with_meta = meta.clone();
63        let has_type = xtype.clone();
64        let name = symbol.name.clone();
65        let dimensions = symbol.is_array;
66        let possible_init = symbol.init;
67        let single_declaration = build_declaration(with_meta, has_type, name, dimensions.clone());
68        initializations.push(single_declaration);
69
70        if let Option::Some(init) = possible_init {
71            let substitution = build_substitution(meta.clone(), symbol.name, vec![], op, init);
72            initializations.push(substitution);
73        }
74        // If the variable is not initialized it is default initialized to 0 by
75        // Circom.  We remove this because we don't want this assignment to be
76        // flagged as an unused assignment by the side-effect analysis.
77        // else if xtype == Var {
78        //     let mut value = Expression::Number(meta.clone(), BigInt::from(0));
79        //     for dim_expr in dimensions.iter().rev() {
80        //         value = build_uniform_array(meta.clone(), value, dim_expr.clone());
81        //     }
82
83        //     let substitution = build_substitution(meta.clone(), symbol.name, vec![], op, value);
84        //     initializations.push(substitution);
85        // }
86    }
87    build_initialization_block(meta, xtype, initializations)
88}
89
90pub fn split_declaration_into_single_nodes_and_multi_substitution(
91    meta: Meta,
92    xtype: VariableType,
93    symbols: Vec<Symbol>,
94    init: Option<TupleInit>,
95) -> Statement {
96    let mut initializations = Vec::new();
97    let mut values = Vec::new();
98    for symbol in symbols {
99        let with_meta = meta.clone();
100        let has_type = xtype.clone();
101        let name = symbol.name.clone();
102        let dimensions = symbol.is_array;
103        debug_assert!(symbol.init.is_none());
104        let single_declaration =
105            build_declaration(with_meta.clone(), has_type, name.clone(), dimensions.clone());
106        initializations.push(single_declaration);
107        // Circom default initializes local arrays to 0. We remove this because
108        // we don't want these assignments to be flagged as unused assignments
109        // by the side-effect analysis.
110        // if xtype == Var && init.is_none() {
111        //     let mut value = Expression::Number(meta.clone(), BigInt::from(0));
112        //     for dim_expr in dimensions.iter().rev() {
113        //         value = build_uniform_array(meta.clone(), value, dim_expr.clone());
114        //     }
115
116        //     let substitution =
117        //         build_substitution(meta.clone(), symbol.name, vec![], AssignOp::AssignVar, value);
118        //     initializations.push(substitution);
119        // }
120        values.push(Expression::Variable { meta: with_meta.clone(), name, access: Vec::new() })
121    }
122    if let Some(tuple) = init {
123        let (op, expression) = tuple.tuple_init;
124        let multi_sub = build_multi_substitution(
125            meta.clone(),
126            build_tuple(meta.clone(), values),
127            op,
128            expression,
129        );
130        initializations.push(multi_sub);
131    }
132    build_initialization_block(meta, xtype, initializations)
133}