spydecy-codegen 0.3.1

Rust code generator for Spydecy transpiler - generates idiomatic safe Rust
Documentation
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Rust Code Generator - Sprint 4+
//!
//! Generates idiomatic Rust code from optimized Unified HIR.
//! This is the final stage of the Spydecy pipeline:
//!
//! ```text
//! Python + C → Unified HIR → Optimizer → Codegen → Rust Code
//! ```

#![warn(missing_docs, clippy::all, clippy::pedantic)]
#![deny(unsafe_code)]
#![allow(clippy::module_name_repetitions)]

use anyhow::{Context, Result};
use spydecy_hir::unified::{UnificationPattern, UnifiedHIR};

/// Rust code generator
///
/// Converts optimized `UnifiedHIR` into idiomatic Rust source code.
pub struct RustCodegen {
    /// Indentation level
    indent_level: usize,
    /// Indentation string (default: 4 spaces)
    indent: String,
}

impl RustCodegen {
    /// Create a new Rust code generator
    #[must_use]
    pub fn new() -> Self {
        Self {
            indent_level: 0,
            indent: "    ".to_owned(), // 4 spaces
        }
    }

    /// Generate Rust code from `UnifiedHIR`
    ///
    /// # Errors
    ///
    /// Returns an error if the HIR cannot be converted to Rust code
    pub fn generate(&mut self, hir: &UnifiedHIR) -> Result<String> {
        match hir {
            UnifiedHIR::Module {
                name, declarations, ..
            } => self.generate_module(name, declarations),
            UnifiedHIR::Function {
                name,
                params,
                return_type,
                body,
                ..
            } => self.generate_function(name, params, return_type, body),
            UnifiedHIR::Call {
                callee,
                args,
                cross_mapping,
                ..
            } => {
                // Check if this is an optimized pattern
                if let Some(mapping) = cross_mapping {
                    if mapping.boundary_eliminated {
                        return Ok(Self::generate_optimized_call(callee, args, mapping.pattern));
                    }
                }
                self.generate_call(callee, args)
            }
            UnifiedHIR::Variable { name, .. } => Ok(name.clone()),
            UnifiedHIR::Return { value, .. } => self.generate_return(value.as_deref()),
            UnifiedHIR::Assign { target, value, .. } => self.generate_assign(target, value),
            _ => Ok("/* Unsupported HIR node */".to_owned()),
        }
    }

    /// Generate a module
    fn generate_module(&mut self, name: &str, declarations: &[UnifiedHIR]) -> Result<String> {
        let mut output = String::new();

        // Module header
        output.push_str("// Module: ");
        output.push_str(name);
        output.push('\n');
        output.push_str("// Generated by Spydecy\n");
        output.push_str("#![allow(dead_code)]\n\n");

        // Generate all declarations
        for decl in declarations {
            let code = self.generate(decl)?;
            output.push_str(&code);
            output.push_str("\n\n");
        }

        Ok(output)
    }

    /// Generate a function
    fn generate_function(
        &mut self,
        name: &str,
        params: &[spydecy_hir::unified::UnifiedParameter],
        return_type: &spydecy_hir::types::Type,
        body: &[UnifiedHIR],
    ) -> Result<String> {
        let mut output = String::new();

        // Function signature
        output.push_str("pub fn ");
        output.push_str(name);
        output.push('(');

        // Parameters
        for (i, param) in params.iter().enumerate() {
            if i > 0 {
                output.push_str(", ");
            }
            output.push_str(&param.name);
            output.push_str(": ");
            output.push_str(&self.generate_type(&param.param_type)?);
        }

        output.push(')');

        // Return type
        if !matches!(
            return_type,
            spydecy_hir::types::Type::Rust(spydecy_hir::types::RustType::Unit)
        ) {
            output.push_str(" -> ");
            output.push_str(&self.generate_type(return_type)?);
        }

        output.push_str(" {\n");

        // Function body
        self.indent_level += 1;
        for stmt in body {
            let code = self.generate(stmt)?;
            output.push_str(&self.indent());
            output.push_str(&code);
            if !code.trim().ends_with('}') && !code.trim().is_empty() {
                output.push(';');
            }
            output.push('\n');
        }
        self.indent_level -= 1;

        output.push('}');

        Ok(output)
    }

    /// Extract the receiver variable name from arguments
    ///
    /// Returns the name of the first Variable argument, or "x" as fallback
    fn extract_receiver_name(args: &[UnifiedHIR]) -> String {
        args.first()
            .and_then(|arg| {
                if let UnifiedHIR::Variable { name, .. } = arg {
                    Some(name.clone())
                } else {
                    None
                }
            })
            .unwrap_or_else(|| "x".to_owned())
    }

    /// Generate an optimized call (post-boundary-elimination)
    #[allow(clippy::unnecessary_wraps)]
    fn generate_optimized_call(
        callee: &str,
        args: &[UnifiedHIR],
        pattern: UnificationPattern,
    ) -> String {
        // Helper: Extract variable name from first argument
        let receiver = Self::extract_receiver_name(args);

        // Generate idiomatic Rust based on the pattern
        match pattern {
            UnificationPattern::LenPattern => {
                // Vec::len() becomes <receiver>.len()
                format!("{receiver}.len()")
            }
            UnificationPattern::AppendPattern => {
                // Vec::push() becomes <receiver>.push(item)
                format!("{receiver}.push(item)")
            }
            UnificationPattern::DictGetPattern => {
                // HashMap::get() becomes <receiver>.get(&key)
                format!("{receiver}.get(&key)")
            }
            UnificationPattern::ReversePattern => {
                // Vec::reverse() becomes <receiver>.reverse()
                format!("{receiver}.reverse()")
            }
            UnificationPattern::ClearPattern => {
                // Vec::clear() becomes <receiver>.clear()
                format!("{receiver}.clear()")
            }
            UnificationPattern::PopPattern => {
                // Vec::pop() becomes <receiver>.pop()
                format!("{receiver}.pop()")
            }
            UnificationPattern::InsertPattern => {
                // Vec::insert() becomes <receiver>.insert(index, value)
                format!("{receiver}.insert(index, value)")
            }
            UnificationPattern::ExtendPattern => {
                // Vec::extend() becomes <receiver>.extend(iter)
                format!("{receiver}.extend(iter)")
            }
            UnificationPattern::DictPopPattern => {
                // HashMap::remove() becomes <receiver>.remove(&key)
                format!("{receiver}.remove(&key)")
            }
            UnificationPattern::DictClearPattern => {
                // HashMap::clear() becomes <receiver>.clear()
                format!("{receiver}.clear()")
            }
            UnificationPattern::DictKeysPattern => {
                // HashMap::keys() becomes <receiver>.keys()
                format!("{receiver}.keys()")
            }
            UnificationPattern::Custom => format!("{callee}()"),
        }
    }

    /// Generate a regular call
    fn generate_call(&mut self, callee: &str, args: &[UnifiedHIR]) -> Result<String> {
        let mut output = callee.to_owned();
        output.push('(');

        for (i, arg) in args.iter().enumerate() {
            if i > 0 {
                output.push_str(", ");
            }
            output.push_str(&self.generate(arg)?);
        }

        output.push(')');
        Ok(output)
    }

    /// Generate a return statement
    fn generate_return(&mut self, value: Option<&UnifiedHIR>) -> Result<String> {
        if let Some(val) = value {
            Ok(format!("return {}", self.generate(val)?))
        } else {
            Ok("return".to_owned())
        }
    }

    /// Generate an assignment
    fn generate_assign(&mut self, target: &str, value: &UnifiedHIR) -> Result<String> {
        let val_code = self.generate(value)?;
        Ok(format!("let {target} = {val_code}"))
    }

    /// Generate a type annotation
    #[allow(clippy::only_used_in_recursion, clippy::self_only_used_in_recursion)]
    fn generate_type(&self, ty: &spydecy_hir::types::Type) -> Result<String> {
        use spydecy_hir::types::{RustType, Type};

        match ty {
            Type::Rust(rust_ty) => match rust_ty {
                RustType::Int { bits, signed } => {
                    let prefix = if *signed { "i" } else { "u" };
                    let size = match bits {
                        spydecy_hir::types::IntSize::I8 => "8",
                        spydecy_hir::types::IntSize::I16 => "16",
                        spydecy_hir::types::IntSize::I32 => "32",
                        spydecy_hir::types::IntSize::I64 => "64",
                        spydecy_hir::types::IntSize::I128 => "128",
                        spydecy_hir::types::IntSize::ISize => "size",
                    };
                    Ok(format!("{prefix}{size}"))
                }
                RustType::Bool => Ok("bool".to_owned()),
                RustType::String => Ok("String".to_owned()),
                RustType::Str => Ok("&str".to_owned()),
                RustType::Vec(inner) => Ok(format!("Vec<{}>", self.generate_type(inner)?)),
                RustType::Option(inner) => Ok(format!("Option<{}>", self.generate_type(inner)?)),
                RustType::Unit => Ok("()".to_owned()),
                RustType::Reference { mutable, inner } => {
                    let mut_str = if *mutable { "mut " } else { "" };
                    Ok(format!("&{mut_str}{}", self.generate_type(inner)?))
                }
                _ => Ok("/* complex type */".to_owned()),
            },
            Type::Unknown => Ok("/* infer */".to_owned()),
            _ => Ok("/* non-rust type */".to_owned()),
        }
    }

    /// Get current indentation string
    fn indent(&self) -> String {
        self.indent.repeat(self.indent_level)
    }
}

impl Default for RustCodegen {
    fn default() -> Self {
        Self::new()
    }
}

/// Generate Rust code from `UnifiedHIR` (convenience function)
///
/// # Errors
///
/// Returns an error if code generation fails
pub fn generate_rust(hir: &UnifiedHIR) -> Result<String> {
    let mut codegen = RustCodegen::new();
    codegen
        .generate(hir)
        .context("Failed to generate Rust code")
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use spydecy_hir::{
        metadata::Metadata,
        types::{IntSize, RustType, Type},
        unified::{CrossMapping, UnificationPattern},
        Language, NodeId,
    };

    #[test]
    fn test_generate_len_pattern() {
        // Create UnifiedHIR for optimized len() call
        let hir = UnifiedHIR::Call {
            id: NodeId::new(1),
            target_language: Language::Rust,
            callee: "Vec::len".to_owned(),
            args: vec![],
            inferred_type: Type::Rust(RustType::Int {
                bits: IntSize::ISize,
                signed: false,
            }),
            source_language: Language::Python,
            cross_mapping: Some(CrossMapping {
                python_node: None,
                c_node: None,
                pattern: UnificationPattern::LenPattern,
                boundary_eliminated: true,
            }),
            meta: Metadata::new(),
        };

        let code = generate_rust(&hir).expect("Should generate code");
        assert_eq!(code.trim(), "x.len()");
    }

    #[test]
    fn test_generate_append_pattern() {
        let hir = UnifiedHIR::Call {
            id: NodeId::new(1),
            target_language: Language::Rust,
            callee: "Vec::push".to_owned(),
            args: vec![],
            inferred_type: Type::Rust(RustType::Unit),
            source_language: Language::Python,
            cross_mapping: Some(CrossMapping {
                python_node: None,
                c_node: None,
                pattern: UnificationPattern::AppendPattern,
                boundary_eliminated: true,
            }),
            meta: Metadata::new(),
        };

        let code = generate_rust(&hir).expect("Should generate code");
        assert_eq!(code.trim(), "x.push(item)");
    }

    #[test]
    fn test_generate_dict_get_pattern() {
        let hir = UnifiedHIR::Call {
            id: NodeId::new(1),
            target_language: Language::Rust,
            callee: "HashMap::get".to_owned(),
            args: vec![UnifiedHIR::Variable {
                id: NodeId::new(2),
                name: "map".to_owned(),
                var_type: Type::Unknown,
                source_language: Language::Python,
                meta: Metadata::new(),
            }],
            inferred_type: Type::Unknown,
            source_language: Language::Python,
            cross_mapping: Some(CrossMapping {
                python_node: None,
                c_node: None,
                pattern: UnificationPattern::DictGetPattern,
                boundary_eliminated: true,
            }),
            meta: Metadata::new(),
        };

        let code = generate_rust(&hir).expect("Should generate code");
        assert_eq!(code.trim(), "map.get(&key)");
    }

    #[test]
    fn test_generate_variable() {
        let hir = UnifiedHIR::Variable {
            id: NodeId::new(1),
            name: "my_var".to_owned(),
            var_type: Type::Unknown,
            source_language: Language::Rust,
            meta: Metadata::new(),
        };

        let code = generate_rust(&hir).expect("Should generate code");
        assert_eq!(code, "my_var");
    }

    #[test]
    fn test_generate_type_int() {
        let codegen = RustCodegen::new();
        let ty = Type::Rust(RustType::Int {
            bits: IntSize::I32,
            signed: true,
        });

        let code = codegen.generate_type(&ty).expect("Should generate type");
        assert_eq!(code, "i32");
    }

    #[test]
    fn test_generate_type_vec() {
        let codegen = RustCodegen::new();
        let ty = Type::Rust(RustType::Vec(Box::new(Type::Rust(RustType::Int {
            bits: IntSize::I32,
            signed: true,
        }))));

        let code = codegen.generate_type(&ty).expect("Should generate type");
        assert_eq!(code, "Vec<i32>");
    }
}