depyler-core 4.1.1

Core transpilation engine for the Depyler Python-to-Rust transpiler
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Statement and expression conversion functions for direct rules
//!
//! DEPYLER-COVERAGE-95: Extracted from direct_rules.rs to reduce file size
//! and improve testability. Contains body/statement/expression conversion.

mod body_convert;
mod expr_advanced;
mod expr_builtins;
mod expr_collections;
mod expr_index_slice;
mod expr_methods;
mod expr_methods_os;
mod expr_operators;
mod method_stmt_convert;
mod operators;
mod stdlib_calls;
mod stmt_convert;
pub(crate) use body_convert::*;
pub(crate) use method_stmt_convert::*;
#[cfg(test)]
pub(crate) use operators::*;
pub(crate) use stmt_convert::*;

use crate::direct_rules::make_ident;
use crate::hir::*;
use crate::type_mapper::TypeMapper;
use anyhow::{bail, Result};
use quote::quote;
use syn::{self, parse_quote};


/// Convert HIR expressions to Rust expressions using strategy pattern
#[allow(dead_code)]
pub(crate) fn convert_expr(expr: &HirExpr, type_mapper: &TypeMapper) -> Result<syn::Expr> {
    // Use empty vararg_functions for backward compatibility
    static EMPTY: std::sync::OnceLock<std::collections::HashSet<String>> =
        std::sync::OnceLock::new();
    convert_expr_with_context(
        expr,
        type_mapper,
        false,
        EMPTY.get_or_init(std::collections::HashSet::new),
    )
}

/// Convert HIR expressions with classmethod context and vararg tracking
pub(crate) fn convert_expr_with_context(
    expr: &HirExpr,
    type_mapper: &TypeMapper,
    is_classmethod: bool,
    vararg_functions: &std::collections::HashSet<String>,
) -> Result<syn::Expr> {
    // DEPYLER-0648: Use with_varargs to track functions that need slice wrapping
    let converter = ExprConverter::with_varargs(type_mapper, is_classmethod, vararg_functions);
    converter.convert(expr)
}

/// DEPYLER-0704: Convert HIR expressions with parameter type information for type coercion
pub(crate) fn convert_expr_with_param_types(
    expr: &HirExpr,
    type_mapper: &TypeMapper,
    is_classmethod: bool,
    vararg_functions: &std::collections::HashSet<String>,
    param_types: &std::collections::HashMap<String, Type>,
) -> Result<syn::Expr> {
    let converter = ExprConverter::with_param_types(
        type_mapper,
        is_classmethod,
        vararg_functions,
        param_types.clone(),
    );
    converter.convert(expr)
}

/// DEPYLER-0720: Convert HIR expressions with class field types for self.field coercion
pub(crate) fn convert_expr_with_class_fields(
    expr: &HirExpr,
    type_mapper: &TypeMapper,
    is_classmethod: bool,
    vararg_functions: &std::collections::HashSet<String>,
    param_types: &std::collections::HashMap<String, Type>,
    class_field_types: &std::collections::HashMap<String, Type>,
) -> Result<syn::Expr> {
    let converter = ExprConverter::with_class_fields(
        type_mapper,
        is_classmethod,
        vararg_functions,
        param_types.clone(),
        class_field_types.clone(),
    );
    converter.convert(expr)
}

/// DEPYLER-1096: Convert condition expression with truthiness coercion
/// Python allows any type in if/while conditions; Rust requires bool.
/// This function converts the expression and applies appropriate truthiness checks.
pub(crate) fn convert_condition_expr(
    expr: &HirExpr,
    type_mapper: &TypeMapper,
    is_classmethod: bool,
    vararg_functions: &std::collections::HashSet<String>,
    param_types: &std::collections::HashMap<String, Type>,
) -> Result<syn::Expr> {
    let converter = ExprConverter::with_param_types(
        type_mapper,
        is_classmethod,
        vararg_functions,
        param_types.clone(),
    );
    let rust_expr = converter.convert(expr)?;
    Ok(converter.apply_truthiness_coercion(expr, rust_expr))
}

/// DEPYLER-99MODE: Convert condition expression with class field types and truthiness coercion
/// Same as convert_condition_expr but also has access to class field types for accurate
/// truthiness checks on self.* attributes (e.g., `if self.items:` → `if !self.items.is_empty()`)
pub(crate) fn convert_condition_expr_with_class_fields(
    expr: &HirExpr,
    type_mapper: &TypeMapper,
    is_classmethod: bool,
    vararg_functions: &std::collections::HashSet<String>,
    param_types: &std::collections::HashMap<String, Type>,
    class_field_types: &std::collections::HashMap<String, Type>,
) -> Result<syn::Expr> {
    let converter = ExprConverter::with_class_fields(
        type_mapper,
        is_classmethod,
        vararg_functions,
        param_types.clone(),
        class_field_types.clone(),
    );
    let rust_expr = converter.convert(expr)?;
    Ok(converter.apply_truthiness_coercion(expr, rust_expr))
}

/// Expression converter using strategy pattern to reduce complexity
pub(crate) struct ExprConverter<'a> {
    #[allow(dead_code)]
    type_mapper: &'a TypeMapper,
    is_classmethod: bool,
    /// DEPYLER-0648: Track functions that have vararg parameters (*args in Python)
    /// Call sites need to wrap arguments in &[...] slices
    vararg_functions: &'a std::collections::HashSet<String>,
    /// DEPYLER-0704: Parameter types for type coercion in binary operations
    param_types: std::collections::HashMap<String, Type>,
    /// DEPYLER-0720: Class field types for self.field attribute access
    /// Maps field name -> Type, used to determine if self.X is float for int-to-float coercion
    class_field_types: std::collections::HashMap<String, Type>,
}

impl<'a> ExprConverter<'a> {
    #[allow(dead_code)]
    pub(crate) fn new(type_mapper: &'a TypeMapper) -> Self {
        // Use empty static HashSet for backwards compatibility
        static EMPTY: std::sync::OnceLock<std::collections::HashSet<String>> =
            std::sync::OnceLock::new();
        Self {
            type_mapper,
            is_classmethod: false,
            vararg_functions: EMPTY.get_or_init(std::collections::HashSet::new),
            param_types: std::collections::HashMap::new(),
            class_field_types: std::collections::HashMap::new(),
        }
    }

    #[allow(dead_code)]
    fn with_classmethod(type_mapper: &'a TypeMapper, is_classmethod: bool) -> Self {
        // Use empty static HashSet for backwards compatibility
        static EMPTY: std::sync::OnceLock<std::collections::HashSet<String>> =
            std::sync::OnceLock::new();
        Self {
            type_mapper,
            is_classmethod,
            vararg_functions: EMPTY.get_or_init(std::collections::HashSet::new),
            param_types: std::collections::HashMap::new(),
            class_field_types: std::collections::HashMap::new(),
        }
    }

    /// DEPYLER-0648: Create converter with vararg function tracking
    fn with_varargs(
        type_mapper: &'a TypeMapper,
        is_classmethod: bool,
        vararg_functions: &'a std::collections::HashSet<String>,
    ) -> Self {
        Self {
            type_mapper,
            is_classmethod,
            vararg_functions,
            param_types: std::collections::HashMap::new(),
            class_field_types: std::collections::HashMap::new(),
        }
    }

    /// DEPYLER-0704: Create converter with parameter types for type coercion
    fn with_param_types(
        type_mapper: &'a TypeMapper,
        is_classmethod: bool,
        vararg_functions: &'a std::collections::HashSet<String>,
        param_types: std::collections::HashMap<String, Type>,
    ) -> Self {
        Self {
            type_mapper,
            is_classmethod,
            vararg_functions,
            param_types,
            class_field_types: std::collections::HashMap::new(),
        }
    }

    /// DEPYLER-0720: Create converter with class field types for self.field access
    fn with_class_fields(
        type_mapper: &'a TypeMapper,
        is_classmethod: bool,
        vararg_functions: &'a std::collections::HashSet<String>,
        param_types: std::collections::HashMap<String, Type>,
        class_field_types: std::collections::HashMap<String, Type>,
    ) -> Self {
        Self {
            type_mapper,
            is_classmethod,
            vararg_functions,
            param_types,
            class_field_types,
        }
    }

    /// DEPYLER-0704: Check if expression returns a float type
    /// DEPYLER-0720: Extended to check class field types for self.field patterns
    fn expr_returns_float_direct(&self, expr: &HirExpr) -> bool {
        match expr {
            HirExpr::Literal(Literal::Float(_)) => true,
            HirExpr::Var(name) => {
                // Check param_types
                matches!(self.param_types.get(name), Some(Type::Float))
            }
            // DEPYLER-0720: Check class field types for self.field attribute access
            HirExpr::Attribute { value, attr } => {
                // Check if this is self.field pattern where field is a float
                if matches!(value.as_ref(), HirExpr::Var(name) if name == "self")
                    && matches!(self.class_field_types.get(attr), Some(Type::Float))
                {
                    return true;
                }
                false
            }
            HirExpr::Binary { left, right, .. } => {
                // Binary with float operand returns float
                self.expr_returns_float_direct(left) || self.expr_returns_float_direct(right)
            }
            HirExpr::MethodCall { method, .. } => {
                // Common float-returning methods
                matches!(
                    method.as_str(),
                    "mean" | "sum" | "std" | "norm" | "variance"
                )
            }
            _ => false,
        }
    }

    /// DEPYLER-0704: Check if expression is an integer type
    fn is_int_expr(&self, expr: &HirExpr) -> bool {
        match expr {
            HirExpr::Literal(Literal::Int(_)) => true,
            HirExpr::Var(name) => {
                // Check param_types
                matches!(self.param_types.get(name), Some(Type::Int))
            }
            _ => false,
        }
    }

    /// DEPYLER-1100: Infer the element type of an iterable expression.
    /// Used to propagate types into generator expression loop variables.
    /// Returns Some(Type::Float) if iterating over floats, None if unknown.
    fn infer_iterable_element_type(&self, iter_expr: &HirExpr) -> Option<Type> {
        match iter_expr {
            // Direct variable reference - check if it's a known float collection
            HirExpr::Var(name) => {
                // Check if it's Vec<float>, List<float>, etc. in param types
                if let Some(Type::List(elem_type) | Type::Set(elem_type)) =
                    self.param_types.get(name)
                {
                    return Some((**elem_type).clone());
                }
                // Check class fields
                if let Some(Type::List(elem_type) | Type::Set(elem_type)) =
                    self.class_field_types.get(name)
                {
                    return Some((**elem_type).clone());
                }
                None
            }
            // Method call like data.values(), items.keys()
            HirExpr::MethodCall { object, method, .. } => {
                match method.as_str() {
                    "values" | "items" | "keys" => {
                        // Recursively check the dict type
                        // For now, just check if the base is a known dict
                        if let HirExpr::Var(name) = object.as_ref() {
                            if let Some(Type::Dict(_, val_type)) = self.param_types.get(name) {
                                if method == "values" {
                                    return Some((**val_type).clone());
                                }
                            }
                        }
                    }
                    _ => {}
                }
                None
            }
            // Attribute access like self.data
            HirExpr::Attribute { value, attr } => {
                if matches!(value.as_ref(), HirExpr::Var(name) if name == "self") {
                    if let Some(Type::List(elem_type) | Type::Set(elem_type)) =
                        self.class_field_types.get(attr)
                    {
                        return Some((**elem_type).clone());
                    }
                }
                None
            }
            _ => None,
        }
    }

    /// DEPYLER-1100: Create a new converter with additional loop variable types.
    /// Used when converting generator expressions to propagate element types.
    fn with_additional_param(&self, var_name: String, var_type: Type) -> Self {
        let mut new_params = self.param_types.clone();
        new_params.insert(var_name, var_type);
        Self {
            type_mapper: self.type_mapper,
            is_classmethod: self.is_classmethod,
            vararg_functions: self.vararg_functions,
            param_types: new_params,
            class_field_types: self.class_field_types.clone(),
        }
    }

    pub(crate) fn convert(&self, expr: &HirExpr) -> Result<syn::Expr> {
        match expr {
            HirExpr::Literal(lit) => self.convert_literal(lit),
            HirExpr::Var(name) => self.convert_variable(name),
            HirExpr::Binary { op, left, right } => self.convert_binary(*op, left, right),
            HirExpr::Unary { op, operand } => self.convert_unary(*op, operand),
            HirExpr::Call { func, args, .. } => self.convert_call(func, args),
            HirExpr::Index { base, index } => self.convert_index(base, index),
            // DEPYLER-0596: Add Slice support for string slicing with negative indices
            HirExpr::Slice {
                base,
                start,
                stop,
                step,
            } => self.convert_slice(base, start, stop, step),
            HirExpr::List(elts) => self.convert_list(elts),
            HirExpr::Dict(items) => self.convert_dict(items),
            HirExpr::Tuple(elts) => self.convert_tuple(elts),
            HirExpr::Set(elts) => self.convert_set(elts),
            HirExpr::FrozenSet(elts) => self.convert_frozenset(elts),
            HirExpr::Lambda { params, body } => self.convert_lambda(params, body),
            HirExpr::MethodCall {
                object,
                method,
                args,
                ..
            } => self.convert_method_call(object, method, args),
            // DEPYLER-0188: Dynamic function call (e.g., handlers[name](args))
            HirExpr::DynamicCall { callee, args, .. } => self.convert_dynamic_call(callee, args),
            HirExpr::ListComp {
                element,
                generators,
            } => {
                // DEPYLER-0504: Legacy path - only support single generator for now
                if generators.len() != 1 {
                    bail!("Multiple generators not supported in direct rules path");
                }
                let gen = &generators[0];
                let condition = if gen.conditions.is_empty() {
                    None
                } else if gen.conditions.len() == 1 {
                    Some(Box::new(gen.conditions[0].clone()))
                } else {
                    bail!("Multiple conditions in generator not supported in direct rules path");
                };
                self.convert_list_comp(element, &gen.target, &gen.iter, &condition)
            }
            HirExpr::SetComp {
                element,
                generators,
            } => {
                // DEPYLER-0504: Legacy path - only support single generator for now
                if generators.len() != 1 {
                    bail!("Multiple generators not supported in direct rules path");
                }
                let gen = &generators[0];
                let condition = if gen.conditions.is_empty() {
                    None
                } else if gen.conditions.len() == 1 {
                    Some(Box::new(gen.conditions[0].clone()))
                } else {
                    bail!("Multiple conditions in generator not supported in direct rules path");
                };
                self.convert_set_comp(element, &gen.target, &gen.iter, &condition)
            }
            HirExpr::DictComp {
                key,
                value,
                generators,
            } => {
                // DEPYLER-0504: Legacy path - only support single generator for now
                if generators.len() != 1 {
                    bail!("Multiple generators not supported in direct rules path");
                }
                let gen = &generators[0];
                let condition = if gen.conditions.is_empty() {
                    None
                } else if gen.conditions.len() == 1 {
                    Some(Box::new(gen.conditions[0].clone()))
                } else {
                    bail!("Multiple conditions in generator not supported in direct rules path");
                };
                self.convert_dict_comp(key, value, &gen.target, &gen.iter, &condition)
            }
            HirExpr::Attribute { value, attr } => self.convert_attribute(value, attr),
            HirExpr::Await { value } => self.convert_await(value),
            // DEPYLER-0513: F-string support for class methods
            HirExpr::FString { parts } => self.convert_fstring(parts),
            // DEPYLER-0764: IfExpr (ternary operator) support for class methods
            // Python: a if cond else b → Rust: if cond { a } else { b }
            HirExpr::IfExpr { test, body, orelse } => {
                let test_expr = self.convert(test)?;
                let body_expr = self.convert(body)?;
                let orelse_expr = self.convert(orelse)?;
                Ok(parse_quote! { if #test_expr { #body_expr } else { #orelse_expr } })
            }
            // DEPYLER-0764: GeneratorExp support for class methods
            // Python: (x for x in items) → Rust: items.iter().map(|x| x)
            // DEPYLER-1100: Propagate element type to loop variable for proper literal coercion
            HirExpr::GeneratorExp {
                element,
                generators,
            } => {
                // Only support single generator for direct rules path
                if generators.len() != 1 {
                    bail!("Multiple generators not supported in direct rules path");
                }
                let gen = &generators[0];
                let iter_expr = self.convert(&gen.iter)?;
                let target_ident = make_ident(&gen.target);

                // DEPYLER-1100: Infer element type and create converter with loop variable typed
                let element_type = self.infer_iterable_element_type(&gen.iter);
                let inner_converter = if let Some(elem_type) = element_type {
                    self.with_additional_param(gen.target.clone(), elem_type)
                } else {
                    // No type info - use self
                    Self {
                        type_mapper: self.type_mapper,
                        is_classmethod: self.is_classmethod,
                        vararg_functions: self.vararg_functions,
                        param_types: self.param_types.clone(),
                        class_field_types: self.class_field_types.clone(),
                    }
                };

                let element_expr = inner_converter.convert(element)?;

                // Handle conditions
                if gen.conditions.is_empty() {
                    Ok(parse_quote! { #iter_expr.iter().map(|#target_ident| #element_expr) })
                } else if gen.conditions.len() == 1 {
                    let cond_expr = inner_converter.convert(&gen.conditions[0])?;
                    Ok(parse_quote! {
                        #iter_expr.iter()
                            .filter(|#target_ident| #cond_expr)
                            .map(|#target_ident| #element_expr)
                    })
                } else {
                    bail!("Multiple conditions in generator not supported in direct rules path");
                }
            }
            // DEPYLER-0764: SortByKey support for sorted() with key parameter
            HirExpr::SortByKey {
                iterable,
                key_params,
                key_body,
                reverse_expr,
            } => {
                let iter_expr = self.convert(iterable)?;
                let key_body_expr = self.convert(key_body)?;

                // Build the key lambda parameter(s)
                let key_param = if key_params.len() == 1 {
                    let p = make_ident(&key_params[0]);
                    quote! { #p }
                } else {
                    let params: Vec<_> = key_params.iter().map(|p| make_ident(p)).collect();
                    quote! { (#(#params),*) }
                };

                // Check if reversed
                let is_reversed = match reverse_expr {
                    Some(boxed) => matches!(boxed.as_ref(), HirExpr::Literal(Literal::Bool(true))),
                    _ => false,
                };

                if is_reversed {
                    Ok(parse_quote! {
                        {
                            let mut v: Vec<_> = #iter_expr.into_iter().collect();
                            v.sort_by_key(|#key_param| std::cmp::Reverse(#key_body_expr));
                            v
                        }
                    })
                } else {
                    Ok(parse_quote! {
                        {
                            let mut v: Vec<_> = #iter_expr.into_iter().collect();
                            v.sort_by_key(|#key_param| #key_body_expr);
                            v
                        }
                    })
                }
            }
            _ => bail!("Expression type not yet supported: {:?}", expr),
        }
    }

}

#[cfg(test)]
#[allow(non_snake_case)]
mod tests;