cairo_lang_executable/
plugin.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
211
use cairo_lang_defs::ids::ModuleId;
use cairo_lang_defs::patcher::{PatchBuilder, RewriteNode};
use cairo_lang_defs::plugin::{
    MacroPlugin, MacroPluginMetadata, PluginDiagnostic, PluginGeneratedFile, PluginResult,
};
use cairo_lang_semantic::db::SemanticGroup;
use cairo_lang_semantic::plugin::{AnalyzerPlugin, PluginSuite};
use cairo_lang_semantic::{GenericArgumentId, Mutability, corelib};
use cairo_lang_syntax::attribute::consts::IMPLICIT_PRECEDENCE_ATTR;
use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::helpers::{OptionWrappedGenericParamListHelper, QueryAttrs};
use cairo_lang_syntax::node::{TypedStablePtr, ast};
use indoc::formatdoc;
use itertools::Itertools;

pub const EXECUTABLE_ATTR: &str = "executable";
pub const EXECUTABLE_RAW_ATTR: &str = "executable_raw";
pub const EXECUTABLE_PREFIX: &str = "__executable_wrapper__";

/// Returns a plugin suite with the `ExecutablePlugin` and `RawExecutableAnalyzer`.
pub fn executable_plugin_suite() -> PluginSuite {
    std::mem::take(
        PluginSuite::default()
            .add_plugin::<ExecutablePlugin>()
            .add_analyzer_plugin::<RawExecutableAnalyzer>(),
    )
}

const IMPLICIT_PRECEDENCE: &[&str] = &[
    "core::pedersen::Pedersen",
    "core::RangeCheck",
    "core::integer::Bitwise",
    "core::ec::EcOp",
    "core::poseidon::Poseidon",
    "core::circuit::RangeCheck96",
    "core::circuit::AddMod",
    "core::circuit::MulMod",
];

#[derive(Debug, Default)]
#[non_exhaustive]
struct ExecutablePlugin;

impl MacroPlugin for ExecutablePlugin {
    fn generate_code(
        &self,
        db: &dyn SyntaxGroup,
        item_ast: ast::ModuleItem,
        _metadata: &MacroPluginMetadata<'_>,
    ) -> PluginResult {
        let ast::ModuleItem::FreeFunction(item) = item_ast else {
            return PluginResult::default();
        };
        if !item.has_attr(db, EXECUTABLE_ATTR) {
            return PluginResult::default();
        }
        let mut diagnostics = vec![];
        let mut builder = PatchBuilder::new(db, &item);
        let declaration = item.declaration(db);
        let generics = declaration.generic_params(db);
        if !generics.is_empty(db) {
            diagnostics.push(PluginDiagnostic::error(
                &generics,
                "Executable functions cannot have generic params.".to_string(),
            ));
            return PluginResult { code: None, diagnostics, remove_original_item: false };
        }
        let name = declaration.name(db);
        let implicits_precedence =
            RewriteNode::Text(format!("#[{IMPLICIT_PRECEDENCE_ATTR}({})]", {
                IMPLICIT_PRECEDENCE.iter().join(", ")
            }));
        builder.add_modified(RewriteNode::interpolate_patched(&formatdoc! {"

                $implicit_precedence$
                #[{EXECUTABLE_RAW_ATTR}]
                fn {EXECUTABLE_PREFIX}$function_name$(mut input: Span<felt252>, ref output: Array<felt252>) {{\n
            "},
            &[
                ("implicit_precedence".into(), implicits_precedence,),
                ("function_name".into(), RewriteNode::from_ast(&name))
            ].into()
        ));
        let params = declaration.signature(db).parameters(db).elements(db);
        for (param_idx, param) in params.iter().enumerate() {
            builder.add_modified(
                RewriteNode::Text(format!(
                    "    let __param{EXECUTABLE_PREFIX}{param_idx} = Serde::deserialize(ref \
                     input).expect('Failed to deserialize param #{param_idx}');\n"
                ))
                .mapped(db, param),
            );
        }
        builder.add_str(
            "    assert(core::array::SpanTrait::is_empty(input), 'Input too long for params.');\n",
        );
        builder.add_modified(RewriteNode::interpolate_patched(
            "    let __result = @$function_name$(\n",
            &[("function_name".into(), RewriteNode::from_ast(&name))].into(),
        ));
        for (param_idx, param) in params.iter().enumerate() {
            builder.add_modified(
                RewriteNode::Text(format!("        __param{EXECUTABLE_PREFIX}{param_idx},\n"))
                    .mapped(db, param),
            );
        }
        builder.add_str("    );\n");
        let mut serialize_node = RewriteNode::text("    Serde::serialize(__result, ref output);\n");
        if let ast::OptionReturnTypeClause::ReturnTypeClause(clause) =
            declaration.signature(db).ret_ty(db)
        {
            serialize_node = serialize_node.mapped(db, &clause);
        }
        builder.add_modified(serialize_node);
        builder.add_str("}\n");
        let (content, code_mappings) = builder.build();
        PluginResult {
            code: Some(PluginGeneratedFile {
                name: "executable".into(),
                content,
                code_mappings,
                aux_data: None,
                diagnostics_note: Default::default(),
            }),
            diagnostics,
            remove_original_item: false,
        }
    }

    fn declared_attributes(&self) -> Vec<String> {
        vec![EXECUTABLE_ATTR.to_string(), EXECUTABLE_RAW_ATTR.to_string()]
    }

    fn executable_attributes(&self) -> Vec<String> {
        vec![EXECUTABLE_RAW_ATTR.to_string()]
    }
}

/// Plugin to add diagnostics on bad `#[executable_raw]` annotations.
#[derive(Default, Debug)]
struct RawExecutableAnalyzer;

impl AnalyzerPlugin for RawExecutableAnalyzer {
    fn diagnostics(&self, db: &dyn SemanticGroup, module_id: ModuleId) -> Vec<PluginDiagnostic> {
        let syntax_db = db.upcast();
        let mut diagnostics = vec![];
        let Ok(free_functions) = db.module_free_functions(module_id) else {
            return diagnostics;
        };
        for (id, item) in free_functions.iter() {
            if !item.has_attr(syntax_db, EXECUTABLE_RAW_ATTR) {
                continue;
            }
            let Ok(signature) = db.free_function_signature(*id) else {
                continue;
            };
            if signature.return_type != corelib::unit_ty(db) {
                diagnostics.push(PluginDiagnostic::error(
                    &signature.stable_ptr.lookup(syntax_db).ret_ty(syntax_db),
                    "Invalid return type for `#[executable_raw]` function, expected `()`."
                        .to_string(),
                ));
            }
            let [input, output] = &signature.params[..] else {
                diagnostics.push(PluginDiagnostic::error(
                    &signature.stable_ptr.lookup(syntax_db).parameters(syntax_db),
                    "Invalid number of params for `#[executable_raw]` function, expected 2."
                        .to_string(),
                ));
                continue;
            };
            if input.ty
                != corelib::get_core_ty_by_name(db, "Span".into(), vec![GenericArgumentId::Type(
                    corelib::core_felt252_ty(db),
                )])
            {
                diagnostics.push(PluginDiagnostic::error(
                    input.stable_ptr.untyped(),
                    "Invalid first param type for `#[executable_raw]` function, expected \
                     `Span<felt252>`."
                        .to_string(),
                ));
            }
            if input.mutability == Mutability::Reference {
                diagnostics.push(PluginDiagnostic::error(
                    input.stable_ptr.untyped(),
                    "Invalid first param mutability for `#[executable_raw]` function, got \
                     unexpected `ref`."
                        .to_string(),
                ));
            }
            if output.ty != corelib::core_array_felt252_ty(db) {
                diagnostics.push(PluginDiagnostic::error(
                    output.stable_ptr.untyped(),
                    "Invalid second param type for `#[executable_raw]` function, expected \
                     `Array<felt252>`."
                        .to_string(),
                ));
            }
            if output.mutability != Mutability::Reference {
                diagnostics.push(PluginDiagnostic::error(
                    output.stable_ptr.untyped(),
                    "Invalid second param mutability for `#[executable_raw]` function, expected \
                     `ref`."
                        .to_string(),
                ));
            }
        }
        diagnostics
    }
}