Skip to main content

alef_backend_csharp/gen_bindings/
mod.rs

1use alef_codegen::shared::binding_fields;
2use alef_core::backend::{Backend, BuildConfig, BuildDependency, Capabilities, GeneratedFile};
3use alef_core::config::{AdapterPattern, Language, ResolvedCrateConfig, resolve_output_dir};
4use alef_core::hash::{self, CommentStyle};
5use alef_core::ir::{ApiSurface, FieldDef, TypeRef};
6use heck::ToPascalCase;
7use std::collections::{HashMap, HashSet};
8use std::path::PathBuf;
9
10/// Metadata for a streaming adapter, used to drive emission of an
11/// `IAsyncEnumerable<Item>` method over the FFI iterator-handle protocol
12/// (`_start` / `_next` / `_free`).
13#[derive(Debug, Clone)]
14pub(super) struct StreamingMethodMeta {
15    /// Owner type (e.g. `DefaultClient`). Retained for future routing decisions even when the
16    /// current emitter derives the receiver type from the enclosing class.
17    #[allow(dead_code)]
18    pub owner_type: String,
19    pub item_type: String,
20}
21
22pub(super) mod enums;
23pub(super) mod errors;
24pub(super) mod functions;
25pub(super) mod methods;
26pub(super) mod types;
27
28pub struct CsharpBackend;
29
30impl CsharpBackend {
31    // lib_name comes from config.ffi_lib_name()
32}
33
34fn effective_exclude_types(config: &ResolvedCrateConfig) -> HashSet<String> {
35    let mut exclude_types: HashSet<String> = config
36        .ffi
37        .as_ref()
38        .map(|ffi| ffi.exclude_types.iter().cloned().collect())
39        .unwrap_or_default();
40    if let Some(csharp) = &config.csharp {
41        exclude_types.extend(csharp.exclude_types.iter().cloned());
42    }
43    exclude_types
44}
45
46fn references_excluded_type(ty: &TypeRef, exclude_types: &HashSet<String>) -> bool {
47    exclude_types.iter().any(|name| ty.references_named(name))
48}
49
50fn signature_references_excluded_type(
51    params: &[alef_core::ir::ParamDef],
52    return_type: &TypeRef,
53    exclude_types: &HashSet<String>,
54) -> bool {
55    references_excluded_type(return_type, exclude_types)
56        || params
57            .iter()
58            .any(|param| references_excluded_type(&param.ty, exclude_types))
59}
60
61fn api_without_excluded_types(api: &ApiSurface, exclude_types: &HashSet<String>) -> ApiSurface {
62    let mut filtered = api.clone();
63    filtered.types.retain(|typ| !exclude_types.contains(&typ.name));
64    for typ in &mut filtered.types {
65        typ.fields
66            .retain(|field| !references_excluded_type(&field.ty, exclude_types));
67        typ.methods
68            .retain(|method| !signature_references_excluded_type(&method.params, &method.return_type, exclude_types));
69    }
70    filtered
71        .enums
72        .retain(|enum_def| !exclude_types.contains(&enum_def.name));
73    for enum_def in &mut filtered.enums {
74        for variant in &mut enum_def.variants {
75            variant
76                .fields
77                .retain(|field| !references_excluded_type(&field.ty, exclude_types));
78        }
79    }
80    filtered
81        .functions
82        .retain(|func| !signature_references_excluded_type(&func.params, &func.return_type, exclude_types));
83    filtered.errors.retain(|error| !exclude_types.contains(&error.name));
84    filtered
85}
86
87impl Backend for CsharpBackend {
88    fn name(&self) -> &str {
89        "csharp"
90    }
91
92    fn language(&self) -> Language {
93        Language::Csharp
94    }
95
96    fn capabilities(&self) -> Capabilities {
97        Capabilities {
98            supports_async: true,
99            supports_classes: true,
100            supports_enums: true,
101            supports_option: true,
102            supports_result: true,
103            ..Capabilities::default()
104        }
105    }
106
107    fn generate_bindings(&self, api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
108        let exclude_types = effective_exclude_types(config);
109        let filtered_api;
110        let api = if exclude_types.is_empty() {
111            api
112        } else {
113            filtered_api = api_without_excluded_types(api, &exclude_types);
114            &filtered_api
115        };
116        let namespace = config.csharp_namespace();
117        let prefix = config.ffi_prefix();
118        let lib_name = config.ffi_lib_name();
119
120        // Collect bridge param names and type aliases from trait_bridges config so we can strip
121        // them from generated function signatures and emit ConvertWithVisitor instead.
122        let bridge_param_names: HashSet<String> = config
123            .trait_bridges
124            .iter()
125            .filter_map(|b| b.param_name.clone())
126            .collect();
127        let bridge_type_aliases: HashSet<String> = config
128            .trait_bridges
129            .iter()
130            .filter_map(|b| b.type_alias.clone())
131            .collect();
132        // Only emit ConvertWithVisitor method if visitor_callbacks is explicitly enabled in FFI config
133        let has_visitor_callbacks = config.ffi.as_ref().map(|f| f.visitor_callbacks).unwrap_or(false);
134        let bridge_associated_types = config.bridge_associated_types();
135
136        // Streaming adapter methods are emitted via the iterator-handle FFI protocol
137        // (`{prefix}_{owner}_{name}_start` / `_next` / `_free`) — not as direct P/Invoke calls
138        // of the callback-based variant. The set is still used to skip the default
139        // method-emission path; the parallel meta map drives the `IAsyncEnumerable` emitters.
140        let streaming_methods: HashSet<String> = config
141            .adapters
142            .iter()
143            .filter(|a| matches!(a.pattern, AdapterPattern::Streaming))
144            .map(|a| a.name.clone())
145            .collect();
146        let streaming_methods_meta: HashMap<String, StreamingMethodMeta> = config
147            .adapters
148            .iter()
149            .filter(|a| matches!(a.pattern, AdapterPattern::Streaming))
150            .filter_map(|a| {
151                let owner_type = a.owner_type.clone()?;
152                let item_type = a.item_type.clone()?;
153                Some((a.name.clone(), StreamingMethodMeta { owner_type, item_type }))
154            })
155            .collect();
156
157        // Functions explicitly excluded from C# bindings (e.g., not present in the C FFI layer).
158        let mut exclude_functions: HashSet<String> = config
159            .csharp
160            .as_ref()
161            .map(|c| c.exclude_functions.iter().cloned().collect())
162            .unwrap_or_default();
163        if let Some(ffi) = &config.ffi {
164            exclude_functions.extend(ffi.exclude_functions.iter().cloned());
165        }
166
167        let output_dir = resolve_output_dir(config.output_paths.get("csharp"), &config.name, "packages/csharp/");
168
169        let base_path = PathBuf::from(&output_dir).join(namespace.replace('.', "/"));
170
171        let mut files = Vec::new();
172
173        // Fallback generic exception class name (used by GetLastError and as base for typed errors)
174        let exception_class_name = format!("{}Exception", api.crate_name.to_pascal_case());
175
176        // 1. Generate NativeMethods.cs
177        files.push(GeneratedFile {
178            path: base_path.join("NativeMethods.cs"),
179            content: strip_trailing_whitespace(&functions::gen_native_methods(
180                api,
181                &namespace,
182                &lib_name,
183                &prefix,
184                &bridge_param_names,
185                &bridge_type_aliases,
186                has_visitor_callbacks,
187                &config.trait_bridges,
188                &streaming_methods,
189                &streaming_methods_meta,
190                &exclude_functions,
191            )),
192            generated_header: true,
193        });
194
195        // 2. Generate error types from thiserror enums (if any), otherwise generic exception
196        if !api.errors.is_empty() {
197            for error in &api.errors {
198                let error_files =
199                    alef_codegen::error_gen::gen_csharp_error_types(error, &namespace, Some(&exception_class_name));
200                for (class_name, content) in error_files {
201                    files.push(GeneratedFile {
202                        path: base_path.join(format!("{}.cs", class_name)),
203                        content: strip_trailing_whitespace(&content),
204                        generated_header: false, // already has header
205                    });
206                }
207            }
208        }
209
210        // Fallback generic exception class (always generated for GetLastError)
211        if api.errors.is_empty()
212            || !api
213                .errors
214                .iter()
215                .any(|e| format!("{}Exception", e.name) == exception_class_name)
216        {
217            files.push(GeneratedFile {
218                path: base_path.join(format!("{}.cs", exception_class_name)),
219                content: strip_trailing_whitespace(&errors::gen_exception_class(&namespace, &exception_class_name)),
220                generated_header: true,
221            });
222        }
223
224        // 3. Generate main wrapper class
225        let base_class_name = api.crate_name.to_pascal_case();
226        let wrapper_class_name = if namespace == base_class_name {
227            format!("{}Lib", base_class_name)
228        } else {
229            base_class_name
230        };
231        files.push(GeneratedFile {
232            path: base_path.join(format!("{}.cs", wrapper_class_name)),
233            content: strip_trailing_whitespace(&methods::gen_wrapper_class(
234                api,
235                &namespace,
236                &wrapper_class_name,
237                &exception_class_name,
238                &prefix,
239                &bridge_param_names,
240                &bridge_type_aliases,
241                has_visitor_callbacks,
242                &streaming_methods,
243                &streaming_methods_meta,
244                &exclude_functions,
245                &config.trait_bridges,
246            )),
247            generated_header: true,
248        });
249
250        // 3b. Generate visitor support files when a bridge is configured.
251        if has_visitor_callbacks {
252            // Look up the visitor trait def from the IR via TraitBridgeConfig.trait_name,
253            // mirroring the Go backend's pattern so that gen_visitor_files is IR-driven.
254            let visitor_bridge_cfg = config
255                .trait_bridges
256                .iter()
257                .find(|b| b.bind_via == alef_core::config::BridgeBinding::OptionsField);
258            let trait_map: std::collections::HashMap<&str, &alef_core::ir::TypeDef> = api
259                .types
260                .iter()
261                .filter(|t| t.is_trait)
262                .map(|t| (t.name.as_str(), t))
263                .collect();
264            let visitor_trait = visitor_bridge_cfg.and_then(|b| trait_map.get(b.trait_name.as_str()).copied());
265
266            if let Some(trait_def) = visitor_trait {
267                for (filename, content) in crate::gen_visitor::gen_visitor_files(&namespace, trait_def) {
268                    files.push(GeneratedFile {
269                        path: base_path.join(filename),
270                        content: strip_trailing_whitespace(&content),
271                        generated_header: true,
272                    });
273                }
274            } else {
275                // Trait not in IR (e.g. parsed separately); fall back to a minimal placeholder.
276                let placeholder = alef_core::ir::TypeDef {
277                    name: String::new(),
278                    rust_path: String::new(),
279                    original_rust_path: String::new(),
280                    fields: vec![],
281                    methods: vec![],
282                    is_opaque: false,
283                    is_clone: false,
284                    is_copy: false,
285                    is_trait: true,
286                    has_default: false,
287                    has_stripped_cfg_fields: false,
288                    is_return_type: false,
289                    serde_rename_all: None,
290                    has_serde: false,
291                    super_traits: vec![],
292                    doc: String::new(),
293                    cfg: None,
294                    binding_excluded: false,
295                    binding_exclusion_reason: None,
296                };
297                for (filename, content) in crate::gen_visitor::gen_visitor_files(&namespace, &placeholder) {
298                    files.push(GeneratedFile {
299                        path: base_path.join(filename),
300                        content: strip_trailing_whitespace(&content),
301                        generated_header: true,
302                    });
303                }
304            }
305            // IVisitor.cs and VisitorCallbacks.cs were removed from gen_visitor_files() in favour
306            // of the HtmlVisitorBridge path in TraitBridges.cs.  Delete any stale copies left
307            // over from earlier generator runs.
308            delete_superseded_visitor_files(&base_path)?;
309        } else {
310            // When visitor_callbacks is disabled, delete stale files from prior runs
311            // to prevent CS8632 warnings (nullable context not enabled).
312            delete_stale_visitor_files(&base_path)?;
313        }
314
315        // 3c. Generate trait bridge classes when configured.
316        if !config.trait_bridges.is_empty() {
317            let trait_defs: Vec<_> = api.types.iter().filter(|t| t.is_trait).collect();
318            let bridges: Vec<_> = config
319                .trait_bridges
320                .iter()
321                .filter_map(|cfg| {
322                    let trait_name = cfg.trait_name.clone();
323                    trait_defs
324                        .iter()
325                        .find(|t| t.name == trait_name)
326                        .map(|trait_def| (trait_name, cfg, *trait_def))
327                })
328                .collect();
329
330            if !bridges.is_empty() {
331                // Collect visible type names (non-trait types that have C# bindings).
332                // Includes both `api.types` and `api.enums` so trait-bridge method signatures
333                // can reference enum types (e.g. `VisitResult`) without falling back to `string`.
334                let visible_type_names: HashSet<&str> = api
335                    .types
336                    .iter()
337                    .filter(|t| !t.is_trait)
338                    .map(|t| t.name.as_str())
339                    .chain(api.enums.iter().map(|e| e.name.as_str()))
340                    .collect();
341                let (filename, content) =
342                    crate::trait_bridge::gen_trait_bridges_file(&namespace, &prefix, &bridges, &visible_type_names);
343                files.push(GeneratedFile {
344                    path: base_path.join(filename),
345                    content: strip_trailing_whitespace(&content),
346                    generated_header: true,
347                });
348            }
349        }
350
351        // Collect enum names so record generation can distinguish enum fields from class fields.
352        let enum_names: HashSet<String> = api.enums.iter().map(|e| e.name.to_pascal_case()).collect();
353
354        // Collect all opaque type names (pascal-cased) so methods on one opaque type that
355        // return another opaque type are wrapped correctly rather than JSON-serialized.
356        let all_opaque_type_names: HashSet<String> = api
357            .types
358            .iter()
359            .filter(|t| t.is_opaque)
360            .map(|t| t.name.to_pascal_case())
361            .collect();
362
363        // 4. Generate opaque handle classes
364        for typ in api.types.iter().filter(|typ| !typ.is_trait) {
365            if typ.is_opaque {
366                let type_filename = typ.name.to_pascal_case();
367                files.push(GeneratedFile {
368                    path: base_path.join(format!("{}.cs", type_filename)),
369                    content: strip_trailing_whitespace(&types::gen_opaque_handle(
370                        typ,
371                        &namespace,
372                        &exception_class_name,
373                        &enum_names,
374                        &streaming_methods,
375                        &streaming_methods_meta,
376                        &all_opaque_type_names,
377                    )),
378                    generated_header: true,
379                });
380            }
381        }
382
383        // Untagged unions with data variants now emit as JsonElement-wrapper classes
384        // (see gen_untagged_wrapper). The set is intentionally empty so record fields
385        // keep their wrapper-class type instead of being downcast to JsonElement.
386        let complex_enums: HashSet<String> = HashSet::new();
387
388        // Collect enums that require a custom JsonConverter (non-standard serialized names only).
389        // Tagged unions are generated as abstract records with [JsonPolymorphic] and do NOT need
390        // a custom converter — the attribute on the type itself handles polymorphic deserialization.
391        // When a property has a custom-converter enum as its type, emit a property-level
392        // [JsonConverter] attribute so the custom converter wins over the global JsonStringEnumConverter.
393        let custom_converter_enums: HashSet<String> = api
394            .enums
395            .iter()
396            .filter(|e| {
397                // Skip tagged unions — they use [JsonPolymorphic] instead
398                let is_tagged_union = e.serde_tag.is_some() && e.variants.iter().any(|v| !v.fields.is_empty());
399                if is_tagged_union {
400                    return false;
401                }
402                // Enums whose `serde_rename_all` is something other than snake_case
403                // (e.g. "kebab-case" for `FilePurpose::FineTune` → `"fine-tune"`)
404                // need a custom converter — `JsonStringEnumConverter(SnakeCaseLower)`
405                // would write `"fine_tune"` instead.
406                let rename_all_differs = matches!(
407                    e.serde_rename_all.as_deref(),
408                    Some("kebab-case") | Some("SCREAMING-KEBAB-CASE") | Some("camelCase") | Some("PascalCase")
409                );
410                if rename_all_differs {
411                    return true;
412                }
413                // Enums with non-standard variant names need a custom converter
414                e.variants.iter().any(|v| {
415                    if let Some(ref rename) = v.serde_rename {
416                        let snake = enums::apply_rename_all(&v.name, e.serde_rename_all.as_deref());
417                        rename != &snake
418                    } else {
419                        false
420                    }
421                })
422            })
423            .map(|e| e.name.to_pascal_case())
424            .collect();
425
426        // Resolve the language-level serde rename_all strategy (always wins over IR type-level).
427        let lang_rename_all = config.serde_rename_all_for_language(Language::Csharp);
428
429        // 5. Generate record types (structs)
430        for typ in api.types.iter().filter(|typ| !typ.is_trait) {
431            if !typ.is_opaque {
432                // Skip types where all fields are unnamed tuple positions — they have no
433                // meaningful properties to expose in C#.
434                let has_visible_fields = binding_fields(&typ.fields).next().is_some();
435                let has_named_fields = binding_fields(&typ.fields).any(|f| !is_tuple_field(f));
436                if has_visible_fields && !has_named_fields {
437                    continue;
438                }
439                // Skip types that gen_visitor handles with richer visitor-specific versions
440                if has_visitor_callbacks && bridge_associated_types.contains(typ.name.as_str()) {
441                    continue;
442                }
443
444                let type_filename = typ.name.to_pascal_case();
445                files.push(GeneratedFile {
446                    path: base_path.join(format!("{}.cs", type_filename)),
447                    content: strip_trailing_whitespace(&types::gen_record_type(
448                        typ,
449                        &namespace,
450                        &enum_names,
451                        &complex_enums,
452                        &custom_converter_enums,
453                        &lang_rename_all,
454                        &bridge_type_aliases,
455                        &exception_class_name,
456                    )),
457                    generated_header: true,
458                });
459            }
460        }
461
462        // 6. Generate enums
463        for enum_def in &api.enums {
464            // Skip enums that gen_visitor handles with richer visitor-specific versions
465            if has_visitor_callbacks && bridge_associated_types.contains(enum_def.name.as_str()) {
466                continue;
467            }
468            let enum_filename = enum_def.name.to_pascal_case();
469            files.push(GeneratedFile {
470                path: base_path.join(format!("{}.cs", enum_filename)),
471                content: strip_trailing_whitespace(&enums::gen_enum(enum_def, &namespace)),
472                generated_header: true,
473            });
474        }
475
476        // 7. Generate ByteArrayToIntArrayConverter if any non-opaque type has non-optional Bytes fields.
477        // Non-optional byte[] fields must be serialized as JSON int arrays, not base64 strings.
478        let needs_byte_array_converter = api
479            .types
480            .iter()
481            .any(|t| !t.is_opaque && t.fields.iter().any(|f| !f.optional && matches!(f.ty, TypeRef::Bytes)));
482        if needs_byte_array_converter {
483            files.push(GeneratedFile {
484                path: base_path.join("ByteArrayToIntArrayConverter.cs"),
485                content: types::gen_byte_array_to_int_array_converter(&namespace),
486                generated_header: true,
487            });
488        }
489
490        // Build adapter body map (consumed by generators via body substitution)
491        let _adapter_bodies = alef_adapters::build_adapter_bodies(config, Language::Csharp)?;
492
493        // 8. Generate Directory.Build.props at the package root (always overwritten).
494        // This file enables Nullable=enable and latest LangVersion for all C# projects
495        // in the packages/csharp hierarchy without requiring per-csproj configuration.
496        files.push(GeneratedFile {
497            path: PathBuf::from("packages/csharp/Directory.Build.props"),
498            content: gen_directory_build_props(),
499            generated_header: true,
500        });
501
502        Ok(files)
503    }
504
505    /// C# wrapper class is already the public API.
506    /// The `gen_wrapper_class` (generated in `generate_bindings`) provides high-level public methods
507    /// that wrap NativeMethods (P/Invoke), marshal types, and handle errors.
508    /// No additional facade is needed.
509    fn generate_public_api(
510        &self,
511        _api: &ApiSurface,
512        _config: &ResolvedCrateConfig,
513    ) -> anyhow::Result<Vec<GeneratedFile>> {
514        // C#'s wrapper class IS the public API — no additional wrapper needed.
515        Ok(vec![])
516    }
517
518    fn build_config(&self) -> Option<BuildConfig> {
519        Some(BuildConfig {
520            tool: "dotnet",
521            crate_suffix: "",
522            build_dep: BuildDependency::Ffi,
523            post_build: vec![],
524        })
525    }
526}
527
528/// Returns true if a field is a tuple struct positional field (e.g., `_0`, `_1`, `0`, `1`).
529pub(super) fn is_tuple_field(field: &FieldDef) -> bool {
530    (field.name.starts_with('_') && field.name[1..].chars().all(|c| c.is_ascii_digit()))
531        || field.name.chars().next().is_none_or(|c| c.is_ascii_digit())
532}
533
534/// Strip trailing whitespace from every line and ensure the file ends with a single newline.
535pub(super) fn strip_trailing_whitespace(content: &str) -> String {
536    let mut result: String = content
537        .lines()
538        .map(|line| line.trim_end())
539        .collect::<Vec<_>>()
540        .join("\n");
541    if !result.ends_with('\n') {
542        result.push('\n');
543    }
544    result
545}
546
547/// Generate C# file header with hash and nullable-enable pragma.
548pub(super) fn csharp_file_header() -> String {
549    let mut out = hash::header(CommentStyle::DoubleSlash);
550    out.push_str("#nullable enable\n\n");
551    out
552}
553
554/// Generate Directory.Build.props with Nullable=enable and LangVersion=latest.
555/// This is auto-generated (overwritten on each build) so it doesn't require user maintenance.
556fn gen_directory_build_props() -> String {
557    "<!-- auto-generated by alef (generate_bindings) -->\n\
558<Project>\n  \
559<PropertyGroup>\n    \
560<Nullable>enable</Nullable>\n    \
561<LangVersion>latest</LangVersion>\n    \
562<TreatWarningsAsErrors>true</TreatWarningsAsErrors>\n  \
563</PropertyGroup>\n\
564</Project>\n"
565        .to_string()
566}
567
568/// Delete `IVisitor.cs` and `VisitorCallbacks.cs` when visitor_callbacks is enabled but the
569/// modern `HtmlVisitorBridge` / `TraitBridges.cs` path supersedes them.
570/// These files are no longer emitted by `gen_visitor_files()` but may exist on disk from older
571/// generator runs.
572fn delete_superseded_visitor_files(base_path: &std::path::Path) -> anyhow::Result<()> {
573    let superseded = ["IVisitor.cs", "VisitorCallbacks.cs"];
574    for filename in superseded {
575        let path = base_path.join(filename);
576        if path.exists() {
577            std::fs::remove_file(&path)
578                .map_err(|e| anyhow::anyhow!("Failed to delete superseded visitor file {}: {}", path.display(), e))?;
579        }
580    }
581    Ok(())
582}
583
584/// Delete stale visitor-related files when visitor_callbacks is disabled.
585/// When visitor_callbacks transitions from true → false, these files remain on disk
586/// and cause CS8632 warnings (nullable context not enabled in these files).
587fn delete_stale_visitor_files(base_path: &std::path::Path) -> anyhow::Result<()> {
588    let stale_files = vec!["IVisitor.cs", "VisitorCallbacks.cs", "NodeContext.cs", "VisitResult.cs"];
589
590    for filename in stale_files {
591        let path = base_path.join(filename);
592        if path.exists() {
593            std::fs::remove_file(&path)
594                .map_err(|e| anyhow::anyhow!("Failed to delete stale visitor file {}: {}", path.display(), e))?;
595        }
596    }
597
598    Ok(())
599}
600
601// ---------------------------------------------------------------------------
602// Helpers: P/Invoke return type mapping
603// ---------------------------------------------------------------------------
604
605use alef_core::ir::PrimitiveType;
606
607/// Returns the C# type to use in a `[DllImport]` declaration for the given return type.
608///
609/// Key differences from the high-level `csharp_type`:
610/// - Bool is marshalled as `int` (C FFI convention) — the wrapper compares != 0.
611/// - String / Named / Vec / Map / Path / Json / Bytes all come back as `IntPtr`.
612/// - Numeric primitives use their natural C# types (`nuint`, `int`, etc.).
613pub(super) fn pinvoke_return_type(ty: &TypeRef) -> &'static str {
614    match ty {
615        TypeRef::Unit => "void",
616        // Bool over FFI is a C int (0/1).
617        TypeRef::Primitive(PrimitiveType::Bool) => "int",
618        // Numeric primitives — use their real C# types.
619        TypeRef::Primitive(PrimitiveType::U8) => "byte",
620        TypeRef::Primitive(PrimitiveType::U16) => "ushort",
621        TypeRef::Primitive(PrimitiveType::U32) => "uint",
622        TypeRef::Primitive(PrimitiveType::U64) => "ulong",
623        TypeRef::Primitive(PrimitiveType::I8) => "sbyte",
624        TypeRef::Primitive(PrimitiveType::I16) => "short",
625        TypeRef::Primitive(PrimitiveType::I32) => "int",
626        TypeRef::Primitive(PrimitiveType::I64) => "long",
627        TypeRef::Primitive(PrimitiveType::F32) => "float",
628        TypeRef::Primitive(PrimitiveType::F64) => "double",
629        TypeRef::Primitive(PrimitiveType::Usize) => "ulong",
630        TypeRef::Primitive(PrimitiveType::Isize) => "long",
631        // Duration as u64
632        TypeRef::Duration => "ulong",
633        // Everything else is a pointer that needs manual marshalling.
634        TypeRef::String
635        | TypeRef::Char
636        | TypeRef::Bytes
637        | TypeRef::Optional(_)
638        | TypeRef::Vec(_)
639        | TypeRef::Map(_, _)
640        | TypeRef::Named(_)
641        | TypeRef::Path
642        | TypeRef::Json => "IntPtr",
643    }
644}
645
646/// Returns the C# type to use for a parameter in a `[DllImport]` declaration.
647///
648/// Managed reference types (Named structs, Vec, Map, Bytes, Optional of Named, etc.)
649/// cannot be directly marshalled by P/Invoke.  They must be passed as `IntPtr` (opaque
650/// handle or JSON-string pointer).  Primitive types and plain strings use their natural
651/// types.
652pub(super) fn pinvoke_param_type(ty: &TypeRef) -> &'static str {
653    match ty {
654        TypeRef::String | TypeRef::Char | TypeRef::Path | TypeRef::Json => "string",
655        // Managed objects — pass as opaque IntPtr (serialised to handle before call)
656        TypeRef::Named(_) | TypeRef::Vec(_) | TypeRef::Map(_, _) | TypeRef::Bytes | TypeRef::Optional(_) => "IntPtr",
657        TypeRef::Unit => "void",
658        TypeRef::Primitive(PrimitiveType::Bool) => "int",
659        TypeRef::Primitive(PrimitiveType::U8) => "byte",
660        TypeRef::Primitive(PrimitiveType::U16) => "ushort",
661        TypeRef::Primitive(PrimitiveType::U32) => "uint",
662        TypeRef::Primitive(PrimitiveType::U64) => "ulong",
663        TypeRef::Primitive(PrimitiveType::I8) => "sbyte",
664        TypeRef::Primitive(PrimitiveType::I16) => "short",
665        TypeRef::Primitive(PrimitiveType::I32) => "int",
666        TypeRef::Primitive(PrimitiveType::I64) => "long",
667        TypeRef::Primitive(PrimitiveType::F32) => "float",
668        TypeRef::Primitive(PrimitiveType::F64) => "double",
669        TypeRef::Primitive(PrimitiveType::Usize) => "ulong",
670        TypeRef::Primitive(PrimitiveType::Isize) => "long",
671        TypeRef::Duration => "ulong",
672    }
673}
674
675/// Returns true if a parameter should be hidden from the public API because it is a
676/// trait-bridge param (e.g. the FFI visitor handle).
677pub(super) fn is_bridge_param(
678    param: &alef_core::ir::ParamDef,
679    bridge_param_names: &HashSet<String>,
680    bridge_type_aliases: &HashSet<String>,
681) -> bool {
682    bridge_param_names.contains(&param.name)
683        || matches!(&param.ty, alef_core::ir::TypeRef::Named(n) if bridge_type_aliases.contains(n))
684}
685
686/// Does the return type need IntPtr→string marshalling in the wrapper?
687pub(super) fn returns_string(ty: &TypeRef) -> bool {
688    matches!(ty, TypeRef::String | TypeRef::Char | TypeRef::Path | TypeRef::Json)
689}
690
691/// Does the return type come back as a C int that should be converted to bool?
692pub(super) fn returns_bool_via_int(ty: &TypeRef) -> bool {
693    matches!(ty, TypeRef::Primitive(PrimitiveType::Bool))
694}
695
696/// Does the return type need JSON deserialization from an IntPtr string?
697pub(super) fn returns_json_object(ty: &TypeRef) -> bool {
698    matches!(
699        ty,
700        TypeRef::Vec(_) | TypeRef::Map(_, _) | TypeRef::Named(_) | TypeRef::Bytes | TypeRef::Optional(_)
701    )
702}
703
704/// Returns true if the FFI return type is a pointer (IntPtr), as opposed to a numeric value.
705/// Only pointer-returning functions use `IntPtr.Zero` as an error sentinel.
706pub(super) fn returns_ptr(ty: &TypeRef) -> bool {
707    matches!(
708        ty,
709        TypeRef::String
710            | TypeRef::Char
711            | TypeRef::Path
712            | TypeRef::Json
713            | TypeRef::Named(_)
714            | TypeRef::Vec(_)
715            | TypeRef::Map(_, _)
716            | TypeRef::Bytes
717            | TypeRef::Optional(_)
718    )
719}
720
721/// Returns the argument expression to pass to the native method for a given parameter.
722///
723/// For truly opaque types (is_opaque = true), the C# class wraps an IntPtr; pass `.Handle`.
724/// For data-struct `Named` types this is the handle variable (e.g. `optionsHandle`).
725/// For everything else it is the parameter name (with `!` for optional).
726pub(super) fn native_call_arg(
727    ty: &TypeRef,
728    param_name: &str,
729    optional: bool,
730    true_opaque_types: &HashSet<String>,
731) -> String {
732    match ty {
733        TypeRef::Named(type_name) if true_opaque_types.contains(type_name) => {
734            // Truly opaque: unwrap the IntPtr from the C# handle class.
735            let bang = if optional { "!" } else { "" };
736            format!("{param_name}{bang}.Handle")
737        }
738        TypeRef::Named(_) | TypeRef::Vec(_) | TypeRef::Map(_, _) => {
739            format!("{param_name}Handle")
740        }
741        TypeRef::Bytes => {
742            format!("{param_name}Handle.AddrOfPinnedObject()")
743        }
744        TypeRef::Primitive(alef_core::ir::PrimitiveType::Bool) => {
745            // FFI convention: bool marshalled as int (0 = false, non-zero = true)
746            if optional {
747                format!("({param_name}?.Value ? 1 : 0)")
748            } else {
749                format!("({param_name} ? 1 : 0)")
750            }
751        }
752        ty => {
753            if optional {
754                // For optional primitive types (e.g. ulong?, uint?), pass the FFI's
755                // None sentinel when the value is null. The FFI shim decodes
756                // `{prim}::MAX` (and NAN for floats) as None — passing 0 collides with
757                // a legitimate zero from the caller, e.g. timeout_secs=0 = "no timeout"
758                // would be silently treated as "unset" without this. Mirrors the
759                // `alef-backend-ffi` `param_optional_numeric_conversion` decoder.
760                // String/Char/Path/Json are reference types so `!` is correct for those.
761                if let TypeRef::Primitive(prim) = ty {
762                    use alef_core::ir::PrimitiveType;
763                    let sentinel = match prim {
764                        PrimitiveType::U8 => "byte.MaxValue",
765                        PrimitiveType::U16 => "ushort.MaxValue",
766                        PrimitiveType::U32 => "uint.MaxValue",
767                        PrimitiveType::U64 | PrimitiveType::Usize => "ulong.MaxValue",
768                        PrimitiveType::I8 => "sbyte.MaxValue",
769                        PrimitiveType::I16 => "short.MaxValue",
770                        PrimitiveType::I32 => "int.MaxValue",
771                        PrimitiveType::I64 | PrimitiveType::Isize => "long.MaxValue",
772                        PrimitiveType::F32 => "float.NaN",
773                        PrimitiveType::F64 => "double.NaN",
774                        PrimitiveType::Bool => unreachable!("handled above"),
775                    };
776                    format!("{param_name} ?? {sentinel}")
777                } else if matches!(ty, TypeRef::Duration) {
778                    format!("{param_name}.GetValueOrDefault()")
779                } else {
780                    format!("{param_name}!")
781                }
782            } else {
783                param_name.to_string()
784            }
785        }
786    }
787}
788
789/// For each `Named` parameter, emit code to serialise it to JSON and obtain a native handle.
790///
791/// For truly opaque types (is_opaque = true), the C# class already wraps the native handle, so
792/// we pass `param.Handle` directly without any JSON serialisation.
793pub(super) fn emit_named_param_setup(
794    out: &mut String,
795    params: &[alef_core::ir::ParamDef],
796    indent: &str,
797    true_opaque_types: &HashSet<String>,
798    exception_name: &str,
799) {
800    for param in params {
801        let param_name = param.name.to_lower_camel_case();
802        let json_var = format!("{param_name}Json");
803        let handle_var = format!("{param_name}Handle");
804
805        match &param.ty {
806            TypeRef::Named(type_name) => {
807                // Truly opaque handles: the C# wrapper class holds the IntPtr directly.
808                // No from_json round-trip needed — pass .Handle directly in native_call_arg.
809                if true_opaque_types.contains(type_name) {
810                    continue;
811                }
812                let from_json_method = format!("{}FromJson", type_name.to_pascal_case());
813
814                // Config parameters: always treat as optional and default null to new instance
815                let is_config_param = param.name == "config";
816                let param_to_serialize = if is_config_param {
817                    let type_pascal = type_name.to_pascal_case();
818                    format!("({} ?? new {}())", param_name, type_pascal)
819                } else {
820                    param_name.to_string()
821                };
822
823                if param.optional && !is_config_param {
824                    // Optional Named param: pass IntPtr.Zero through to native when the
825                    // C# arg is null instead of round-tripping `"null"` through FromJson
826                    // which would error with "invalid type: null, expected struct T".
827                    out.push_str(&crate::template_env::render(
828                        "named_param_handle_from_json_optional.jinja",
829                        minijinja::context! {
830                            indent,
831                            handle_var => &handle_var,
832                            from_json_method => &from_json_method,
833                            json_var => &json_var,
834                            param_name => &param_name,
835                            exception_name => exception_name,
836                        },
837                    ));
838                } else {
839                    out.push_str(&crate::template_env::render(
840                        "named_param_json_serialize.jinja",
841                        minijinja::context! { indent, json_var => &json_var, param_name => &param_to_serialize },
842                    ));
843                    out.push_str(&crate::template_env::render(
844                        "named_param_handle_from_json.jinja",
845                        minijinja::context! {
846                            indent,
847                            handle_var => &handle_var,
848                            from_json_method => &from_json_method,
849                            json_var => &json_var,
850                            exception_name => exception_name,
851                        },
852                    ));
853                }
854            }
855            TypeRef::Vec(_) | TypeRef::Map(_, _) => {
856                // Vec/Map: serialize to JSON string, marshal to native pointer
857                out.push_str(&crate::template_env::render(
858                    "named_param_json_serialize.jinja",
859                    minijinja::context! { indent, json_var => &json_var, param_name => &param_name },
860                ));
861                out.push_str(&crate::template_env::render(
862                    "named_param_handle_string.jinja",
863                    minijinja::context! { indent, handle_var => &handle_var, json_var => &json_var },
864                ));
865            }
866            TypeRef::Bytes => {
867                // byte[]: pin the managed array and pass pointer to native
868                out.push_str(&crate::template_env::render(
869                    "named_param_handle_pin.jinja",
870                    minijinja::context! { indent, handle_var => &handle_var, param_name => &param_name },
871                ));
872            }
873            _ => {}
874        }
875    }
876}
877
878/// Emit cleanup code to free native handles allocated for `Named` parameters.
879///
880/// Truly opaque handles (is_opaque = true) are NOT freed here — their lifetime is managed by
881/// the C# wrapper class (IDisposable). Only data-struct handles (from_json-allocated) are freed.
882pub(super) fn emit_named_param_teardown(
883    out: &mut String,
884    params: &[alef_core::ir::ParamDef],
885    true_opaque_types: &HashSet<String>,
886) {
887    for param in params {
888        let param_name = param.name.to_lower_camel_case();
889        let handle_var = format!("{param_name}Handle");
890        match &param.ty {
891            TypeRef::Named(type_name) => {
892                if true_opaque_types.contains(type_name) {
893                    // Caller owns the opaque handle — do not free it here.
894                    continue;
895                }
896                let free_method = format!("{}Free", type_name.to_pascal_case());
897                out.push_str(&crate::template_env::render(
898                    "named_param_teardown_free.jinja",
899                    minijinja::context! { indent => "        ", free_method => &free_method, handle_var => &handle_var },
900                ));
901            }
902            TypeRef::Vec(_) | TypeRef::Map(_, _) => {
903                out.push_str(&crate::template_env::render(
904                    "named_param_teardown_hglobal.jinja",
905                    minijinja::context! { indent => "        ", handle_var => &handle_var },
906                ));
907            }
908            TypeRef::Bytes => {
909                out.push_str(&crate::template_env::render(
910                    "named_param_teardown_gchandle.jinja",
911                    minijinja::context! { indent => "        ", handle_var => &handle_var },
912                ));
913            }
914            _ => {}
915        }
916    }
917}
918
919/// Emit cleanup code with configurable indentation (used inside `Task.Run` lambdas).
920pub(super) fn emit_named_param_teardown_indented(
921    out: &mut String,
922    params: &[alef_core::ir::ParamDef],
923    indent: &str,
924    true_opaque_types: &HashSet<String>,
925) {
926    for param in params {
927        let param_name = param.name.to_lower_camel_case();
928        let handle_var = format!("{param_name}Handle");
929        match &param.ty {
930            TypeRef::Named(type_name) => {
931                if true_opaque_types.contains(type_name) {
932                    // Caller owns the opaque handle — do not free it here.
933                    continue;
934                }
935                let free_method = format!("{}Free", type_name.to_pascal_case());
936                out.push_str(&crate::template_env::render(
937                    "named_param_teardown_free.jinja",
938                    minijinja::context! { indent, free_method => &free_method, handle_var => &handle_var },
939                ));
940            }
941            TypeRef::Vec(_) | TypeRef::Map(_, _) => {
942                out.push_str(&crate::template_env::render(
943                    "named_param_teardown_hglobal.jinja",
944                    minijinja::context! { indent, handle_var => &handle_var },
945                ));
946            }
947            TypeRef::Bytes => {
948                out.push_str(&crate::template_env::render(
949                    "named_param_teardown_gchandle.jinja",
950                    minijinja::context! { indent, handle_var => &handle_var },
951                ));
952            }
953            _ => {}
954        }
955    }
956}
957
958use heck::ToLowerCamelCase;