cairo_lang_sierra_generator/
replace_ids.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use cairo_lang_debug::DebugWithDb;
use cairo_lang_sierra::program;
use cairo_lang_utils::{extract_matches, LookupIntern};

use crate::db::{SierraGenGroup, SierraGeneratorTypeLongId};
use crate::pre_sierra::{self, PushValue};

pub trait SierraIdReplacer {
    /// Returns a new program where all the ids are replaced.
    fn apply(
        &self,
        program: &cairo_lang_sierra::program::Program,
    ) -> cairo_lang_sierra::program::Program {
        let mut program = program.clone();
        for statement in &mut program.statements {
            if let cairo_lang_sierra::program::GenStatement::Invocation(p) = statement {
                p.libfunc_id = self.replace_libfunc_id(&p.libfunc_id);
            }
        }
        for type_declaration in &mut program.type_declarations {
            type_declaration.id = self.replace_type_id(&type_declaration.id);
            self.replace_generic_args(&mut type_declaration.long_id.generic_args);
        }
        for libfunc_declaration in &mut program.libfunc_declarations {
            libfunc_declaration.id = self.replace_libfunc_id(&libfunc_declaration.id);
            self.replace_generic_args(&mut libfunc_declaration.long_id.generic_args);
        }
        for function in &mut program.funcs {
            function.id = self.replace_function_id(&function.id);
            for param in &mut function.params {
                param.ty = self.replace_type_id(&param.ty);
            }
            for ty in &mut function.signature.ret_types {
                *ty = self.replace_type_id(ty);
            }
            for ty in &mut function.signature.param_types {
                *ty = self.replace_type_id(ty);
            }
        }
        program
    }

    // Replaces libfunc_ids
    fn replace_libfunc_id(
        &self,
        id: &cairo_lang_sierra::ids::ConcreteLibfuncId,
    ) -> cairo_lang_sierra::ids::ConcreteLibfuncId;

    // Replace type_ids
    fn replace_type_id(
        &self,
        id: &cairo_lang_sierra::ids::ConcreteTypeId,
    ) -> cairo_lang_sierra::ids::ConcreteTypeId;

    // Replace user function ids.
    fn replace_function_id(
        &self,
        sierra_id: &cairo_lang_sierra::ids::FunctionId,
    ) -> cairo_lang_sierra::ids::FunctionId;

    fn replace_generic_args(&self, generic_args: &mut Vec<program::GenericArg>) {
        for arg in generic_args {
            match arg {
                program::GenericArg::Type(id) => {
                    *id = self.replace_type_id(id);
                }
                program::GenericArg::UserFunc(id) => {
                    *id = self.replace_function_id(id);
                }
                program::GenericArg::Libfunc(id) => {
                    *id = self.replace_libfunc_id(id);
                }
                program::GenericArg::Value(_) | program::GenericArg::UserType(_) => {}
            }
        }
    }
}

/// Replaces `cairo_lang_sierra::ids::{ConcreteLibfuncId, ConcreteTypeId, FunctionId}` with a dummy
/// ids whose debug string is the string representing the expanded information about the id.
/// For Libfuncs and Types - that would be recursively opening their generic arguments, for
/// functions - that would be getting their original name. For example, while the original debug
/// string may be `[6]`, the resulting debug string may be:
///  - For libfuncs: `felt252_const<2>` or `unbox<Box<Box<felt252>>>`.
///  - For types: `felt252` or `Box<Box<felt252>>`.
///  - For user functions: `test::foo`.
pub struct DebugReplacer<'a> {
    pub db: &'a dyn SierraGenGroup,
}
impl SierraIdReplacer for DebugReplacer<'_> {
    fn replace_libfunc_id(
        &self,
        id: &cairo_lang_sierra::ids::ConcreteLibfuncId,
    ) -> cairo_lang_sierra::ids::ConcreteLibfuncId {
        let mut long_id = id.lookup_intern(self.db);
        self.replace_generic_args(&mut long_id.generic_args);
        cairo_lang_sierra::ids::ConcreteLibfuncId {
            id: id.id,
            debug_name: Some(long_id.to_string().into()),
        }
    }

    fn replace_type_id(
        &self,
        id: &cairo_lang_sierra::ids::ConcreteTypeId,
    ) -> cairo_lang_sierra::ids::ConcreteTypeId {
        match id.lookup_intern(self.db) {
            SierraGeneratorTypeLongId::Phantom(ty)
            | SierraGeneratorTypeLongId::CycleBreaker(ty) => ty.format(self.db.upcast()).into(),
            SierraGeneratorTypeLongId::Regular(long_id) => {
                let mut long_id = long_id.as_ref().clone();
                self.replace_generic_args(&mut long_id.generic_args);
                if long_id.generic_id == "Enum".into() || long_id.generic_id == "Struct".into() {
                    long_id.generic_id =
                        extract_matches!(&long_id.generic_args[0], program::GenericArg::UserType)
                            .to_string()
                            .into();
                    if long_id.generic_id == "Tuple".into() {
                        long_id.generic_args = long_id.generic_args.into_iter().skip(1).collect();
                        if long_id.generic_args.is_empty() {
                            long_id.generic_id = "Unit".into();
                        }
                    } else {
                        long_id.generic_args.clear();
                    }
                }
                cairo_lang_sierra::ids::ConcreteTypeId {
                    id: id.id,
                    debug_name: Some(long_id.to_string().into()),
                }
            }
        }
    }

    /// Helper for [replace_sierra_ids] and [replace_sierra_ids_in_program] replacing function ids.
    fn replace_function_id(
        &self,
        sierra_id: &cairo_lang_sierra::ids::FunctionId,
    ) -> cairo_lang_sierra::ids::FunctionId {
        let semantic_id = sierra_id.lookup_intern(self.db);
        cairo_lang_sierra::ids::FunctionId {
            id: sierra_id.id,
            debug_name: Some(
                format!("{:?}", semantic_id.lookup_intern(self.db).debug(self.db.upcast())).into(),
            ),
        }
    }
}

impl DebugReplacer<'_> {
    /// Enriches the function entries with their full function name. Required for tests and cairo
    /// running.
    pub fn enrich_function_names(&self, program: &mut cairo_lang_sierra::program::Program) {
        for function in &mut program.funcs {
            function.id = self.replace_function_id(&function.id);
        }
    }
}

pub fn replace_sierra_ids(
    db: &dyn SierraGenGroup,
    statement: &pre_sierra::StatementWithLocation,
) -> pre_sierra::StatementWithLocation {
    let replacer = DebugReplacer { db };
    match &statement.statement {
        pre_sierra::Statement::Sierra(cairo_lang_sierra::program::GenStatement::Invocation(p)) => {
            pre_sierra::StatementWithLocation {
                statement: pre_sierra::Statement::Sierra(
                    cairo_lang_sierra::program::GenStatement::Invocation(
                        cairo_lang_sierra::program::GenInvocation {
                            libfunc_id: replacer.replace_libfunc_id(&p.libfunc_id),
                            ..p.clone()
                        },
                    ),
                ),
                ..statement.clone()
            }
        }
        pre_sierra::Statement::PushValues(values) => pre_sierra::StatementWithLocation {
            statement: pre_sierra::Statement::PushValues(
                values
                    .iter()
                    .map(|value| PushValue {
                        ty: replacer.replace_type_id(&value.ty),
                        ..value.clone()
                    })
                    .collect(),
            ),
            ..statement.clone()
        },
        _ => statement.clone(),
    }
}

/// Replaces `cairo_lang_sierra::ids::{ConcreteLibfuncId, ConcreteTypeId, FunctionId}` with a dummy
/// ids whose debug string is the string representing the expanded information about the id.
/// For Libfuncs and Types - that would be recursively opening their generic arguments, for
/// functions - that would be getting their original name. For example, while the original debug
/// string may be `[6]`, the resulting debug string may be:
///  - For libfuncs: `felt252_const<2>` or `unbox<Box<Box<felt252>>>`.
///  - For types: `felt252` or `Box<Box<felt252>>`.
///  - For user functions: `test::foo`.
///
/// Similar to [replace_sierra_ids] except that it acts on [cairo_lang_sierra::program::Program].
pub fn replace_sierra_ids_in_program(
    db: &dyn SierraGenGroup,
    program: &cairo_lang_sierra::program::Program,
) -> cairo_lang_sierra::program::Program {
    DebugReplacer { db }.apply(program)
}