nativeview-codegen 0.1.0-rc.1

Multi-language ABI, policy, flat-op codec, and hybrid Direct-FFI code generator for Native View Engine.
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
//! Per-language spellings of ABI types, names, and values, called from the Askama templates.
//!
//! Every function takes the borrowed `nativeview-abi` descriptors directly. [`super::layout`] and
//! [`super::validate`] reject every type spelling a template cannot express before rendering
//! starts, so the `/* unsupported */` fallback below is unreachable (and would fail the host
//! compile if it were not).

use nativeview_abi::schema::{AbiAliasDescriptor, AbiParamDescriptor};

use super::layout::{FieldLayout, Scalar, Ty, Width};
use super::model::{HexLiteral, U64Split};
use crate::naming::{screaming_snake_to_camel_case, to_camel_case, to_pascal_case, to_snake_case};

/// `SCREAMING_SNAKE` of a Rust identifier (`NvValueTag` -> `NV_VALUE_TAG`, `hit_test` -> `HIT_TEST`).
pub fn screaming(name: &str) -> String {
    let mut s = to_snake_case(name);
    s.make_ascii_uppercase();
    s
}

/// `lowerCamel` of a snake / SCREAMING identifier (`NV_ABI_MAGIC` -> `nvAbiMagic`). A leading `_`
/// is dropped: Dart would make the name library-private.
pub fn camel(name: &str) -> String {
    let name = name.trim_start_matches('_');
    if !name.bytes().any(|b| b.is_ascii_lowercase()) {
        screaming_snake_to_camel_case(name)
    } else {
        to_camel_case(name)
    }
}

/// `snake_case` of a Rust identifier (`Surface` -> `surface`).
pub fn snake(name: &str) -> String {
    to_snake_case(name)
}

/// `UpperCamel` of a snake / dotted identifier (`nv.text_input` -> `NvTextInput`).
pub fn pascal(name: &str) -> String {
    to_pascal_case(&name.replace('.', "_"))
}

/// Enum discriminant / constant: hex from `0x1000` up, decimal below.
pub fn int(value: &i64) -> String {
    if *value >= 0x1000 {
        format!("0x{value:X}")
    } else {
        value.to_string()
    }
}

/// Unsigned constant spelled as declared (`hex = true` -> `0x...`).
pub fn uint(value: &u64, hex: &bool) -> String {
    if *hex {
        format!("0x{value:X}")
    } else {
        value.to_string()
    }
}

/// 64-bit unsigned constant (`hex = true` -> 16-digit `0x...`).
pub fn uint64(value: &u64, hex: &bool) -> String {
    if *hex {
        format!("0x{value:016X}")
    } else {
        value.to_string()
    }
}

/// Low 32 bits of a 64-bit constant as `0x........`.
pub fn lo_hex(value: &u64) -> HexLiteral {
    U64Split::new(*value).lo_hex()
}

/// High 32 bits of a 64-bit constant as `0x........`.
pub fn hi_hex(value: &u64) -> HexLiteral {
    U64Split::new(*value).hi_hex()
}

/// Java `int` literal: hex above `i32::MAX`, where a decimal literal would not compile.
pub fn java_int(value: &u64, hex: &bool) -> String {
    uint(value, &(*hex || *value > 0x7FFF_FFFF))
}

/// `NvNodeRow` -> `NODE_ROW` (Java / TS constant prefix of a record).
pub fn record_prefix(name: &str) -> String {
    screaming(name.strip_prefix("Nv").unwrap_or(name))
}

/// Constant prefix of one runtime ABI enum's values: its name without the `NvRuntime` / `Nv`
/// namespace, `SCREAMING` (`NvRuntimeStatus` -> `STATUS`), so each enum owns its own prefix.
pub fn enum_prefix(name: &str) -> String {
    screaming(
        name.strip_prefix("NvRuntime")
            .or_else(|| name.strip_prefix("Nv"))
            .unwrap_or(name),
    )
}

/// Constant name without the `NV_` namespace (`NV_ABI_MAGIC` -> `ABI_MAGIC`).
pub fn bare(name: &str) -> &str {
    name.strip_prefix("NV_").unwrap_or(name)
}

/// Java `int` constants for a layout value: one constant when every data model agrees, else
/// `_64` / `_32` (or one constant when those agree) plus `_I386` where i386 differs from ILP32.
pub fn java_width(name: String, w: &Width) -> String {
    let line = |suffix: &str, v: usize| format!("    public static final int {name}{suffix} = {v};");
    let mut out = match w.fixed() {
        Some(n) => return line("", n),
        None if w.p64() == w.p32() => line("", w.p64()),
        None => format!("{}\n{}", line("_64", w.p64()), line("_32", w.p32())),
    };
    if w.i386() != w.p32() {
        out.push('\n');
        out.push_str(&line("_I386", w.i386()));
    }
    out
}

/// Single-line comment text: newlines folded so a doc can never end the comment early.
pub fn one_line(doc: &str) -> String {
    doc.split('\n')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
        .replace("*/", "* /")
}

pub mod c {
    use super::*;

    pub fn scalar(s: Scalar) -> &'static str {
        match s {
            Scalar::Bool => "bool",
            Scalar::U8 => "uint8_t",
            Scalar::U16 => "uint16_t",
            Scalar::U32 => "uint32_t",
            Scalar::U64 => "uint64_t",
            Scalar::I32 => "int32_t",
            Scalar::I64 => "int64_t",
            Scalar::F32 => "float",
            Scalar::F64 => "double",
            Scalar::Usize => "size_t",
        }
    }

    fn resolve_c_alias<'a>(aliases: &'a [AbiAliasDescriptor], name: &'a str) -> &'a str {
        aliases
            .iter()
            .find(|a| a.rust_name == Some(name))
            .map_or(name, |a| a.name)
    }

    /// C spelling of an ABI type (arrays spell their element type; see [`decl`]).
    pub fn ty(aliases: &[AbiAliasDescriptor], spelling: &str) -> String {
        match Ty::parse(spelling) {
            Ok(Ty::Unit | Ty::Void) => "void".to_owned(),
            Ok(Ty::Scalar(s)) => scalar(s).to_owned(),
            Ok(Ty::Ptr {
                mutable: false,
                pointee,
            }) => {
                if matches!(Ty::parse(pointee), Ok(Ty::Ptr { .. })) {
                    format!("{} const*", ty(aliases, pointee))
                } else {
                    format!("const {}*", ty(aliases, pointee))
                }
            }
            Ok(Ty::Ptr {
                mutable: true,
                pointee,
            }) => format!("{}*", ty(aliases, pointee)),
            Ok(Ty::Array { elem, .. }) => ty(aliases, elem),
            Ok(Ty::Nullable(cb)) => cb.to_owned(),
            Ok(Ty::Named(name)) => resolve_c_alias(aliases, name).to_owned(),
            Err(_) => "/* unsupported */".to_owned(),
        }
    }

    /// `type name` / `type name[N]` declaration of a field or parameter.
    pub fn decl(aliases: &[AbiAliasDescriptor], spelling: &str, name: &str) -> String {
        match Ty::parse(spelling) {
            Ok(Ty::Array { len, .. }) => format!("{} {name}[{len}]", ty(aliases, spelling)),
            _ => format!("{} {name}", ty(aliases, spelling)),
        }
    }

    /// C parameter list (`void` when empty, else comma-separated `type name`).
    pub fn fn_params(aliases: &[AbiAliasDescriptor], params: &[AbiParamDescriptor]) -> String {
        if params.is_empty() {
            "void".to_owned()
        } else {
            params
                .iter()
                .map(|p| decl(aliases, p.ty, p.name))
                .collect::<Vec<_>>()
                .join(", ")
        }
    }

    /// `16u`, `(sizeof(void*) == 8u ? 16u : 8u)`, or, when i386 System V differs from other 32-bit
    /// targets, `(sizeof(void*) == 8u ? 24u : (NV_ABI_U64_FIELD_ALIGN == 8u ? 16u : 12u))`.
    pub fn width(w: &Width) -> String {
        if let Some(n) = w.fixed() {
            return format!("{n}u");
        }
        let p32 = if w.p32() == w.i386() {
            format!("{}u", w.p32())
        } else {
            format!(
                "(NV_ABI_U64_FIELD_ALIGN == 8u ? {}u : {}u)",
                w.p32(),
                w.i386()
            )
        };
        format!("(sizeof(void*) == 8u ? {}u : {p32})", w.p64())
    }

    pub fn field_decl(aliases: &[AbiAliasDescriptor], f: &FieldLayout<'_>) -> String {
        match f.ty {
            Ty::Array { elem, len } => format!("{} {}[{len}]", ty(aliases, elem), f.desc.name),
            _ => format!("{} {}", ty(aliases, f.desc.abi_type()), f.desc.name),
        }
    }
}

pub mod dart {
    use super::*;

    /// Follows value-alias chains (`NodeId`/`NvNodeId` -> `u32`, `NvAppHandle` -> `*mut c_void`)
    /// to their underlying ABI type, stopping at callbacks and aliases emitted as Dart typedefs
    /// (`keep_aliases`, e.g. `NvRuntimeHandle`). Scalar aliases (`NvOverlayFlags` -> `u16`) are
    /// never kept: `ffi.Struct` fields need annotated `int`s, and a Dart typedef of the same name
    /// would collide with the table enum class it mirrors.
    fn resolve_alias<'a>(
        spelling: &'a str,
        aliases: &'a [AbiAliasDescriptor],
        keep_aliases: &'a [AbiAliasDescriptor],
        callbacks: &[&str],
    ) -> &'a str {
        let mut cur = spelling;
        for _ in 0..=aliases.len() {
            let Ok(Ty::Named(name)) = Ty::parse(cur) else {
                break;
            };
            if callbacks.contains(&name) || keep_aliases.iter().any(|k| k.name == name && is_typedef(k)) {
                break;
            }
            match aliases
                .iter()
                .find(|a| a.name == name || a.rust_name == Some(name))
            {
                Some(alias) => cur = alias.target,
                None => break,
            }
        }
        cur
    }

    /// `true` when `alias` is emitted as a Dart `typedef` (every non-scalar alias).
    pub fn is_typedef(alias: &AbiAliasDescriptor) -> bool {
        !matches!(Ty::parse(alias.target), Ok(Ty::Scalar(_)))
    }

    /// `dart:ffi` native type (`ffi.Uint32`, `ffi.Pointer<NvValue>`, `NvStrSlice`,
    /// `ffi.Pointer<ffi.NativeFunction<NvRuntimeWakeNative>>`).
    pub fn native(
        aliases: &[AbiAliasDescriptor],
        keep_aliases: &[AbiAliasDescriptor],
        callbacks: &[&str],
        spelling: &str,
    ) -> String {
        match Ty::parse(resolve_alias(spelling, aliases, keep_aliases, callbacks)) {
            Ok(Ty::Unit | Ty::Void) => "ffi.Void".to_owned(),
            Ok(Ty::Scalar(s)) => native_scalar(s).to_owned(),
            Ok(Ty::Ptr { pointee, .. }) => format!(
                "ffi.Pointer<{}>",
                native(aliases, keep_aliases, callbacks, pointee)
            ),
            Ok(Ty::Array { elem, .. }) => format!(
                "ffi.Array<{}>",
                native(aliases, keep_aliases, callbacks, elem)
            ),
            Ok(Ty::Nullable(cb)) => {
                format!(
                    "ffi.Pointer<ffi.NativeFunction<{}Native>>",
                    callback_stem(cb)
                )
            }
            Ok(Ty::Named(name)) if callbacks.contains(&name) => {
                format!(
                    "ffi.Pointer<ffi.NativeFunction<{}Native>>",
                    callback_stem(name)
                )
            }
            Ok(Ty::Named(name)) => name.to_owned(),
            Err(_) => "/* unsupported */".to_owned(),
        }
    }

    /// Dart-side `asFunction` type (`int`, `double`, `bool`, `void`, `ffi.Pointer<...>`).
    pub fn dart_side(
        aliases: &[AbiAliasDescriptor],
        keep_aliases: &[AbiAliasDescriptor],
        callbacks: &[&str],
        spelling: &str,
    ) -> String {
        match Ty::parse(resolve_alias(spelling, aliases, keep_aliases, callbacks)) {
            Ok(Ty::Unit | Ty::Void) => "void".to_owned(),
            Ok(Ty::Scalar(s)) => dart_scalar(s).to_owned(),
            _ => native(aliases, keep_aliases, callbacks, spelling),
        }
    }

    /// Single-line `RET Function(T name, ...)` signature (`native` or `dart_side`).
    pub fn fn_sig(
        aliases: &[AbiAliasDescriptor],
        keep_aliases: &[AbiAliasDescriptor],
        callbacks: &[&str],
        params: &[AbiParamDescriptor],
        ret: &str,
        as_dart: bool,
    ) -> String {
        let pick = |ty: &str| {
            if as_dart {
                dart_side(aliases, keep_aliases, callbacks, ty)
            } else {
                native(aliases, keep_aliases, callbacks, ty)
            }
        };
        let list: Vec<String> = params
            .iter()
            .map(|p| format!("{} {}", pick(p.ty), camel(p.name)))
            .collect();
        format!("{} Function({})", pick(ret), list.join(", "))
    }

    fn native_scalar(s: Scalar) -> &'static str {
        match s {
            Scalar::Bool => "ffi.Bool",
            Scalar::U8 => "ffi.Uint8",
            Scalar::U16 => "ffi.Uint16",
            Scalar::U32 => "ffi.Uint32",
            Scalar::U64 => "ffi.Uint64",
            Scalar::I32 => "ffi.Int32",
            Scalar::I64 => "ffi.Int64",
            Scalar::F32 => "ffi.Float",
            Scalar::F64 => "ffi.Double",
            Scalar::Usize => "ffi.Size",
        }
    }

    fn dart_scalar(s: Scalar) -> &'static str {
        match s {
            Scalar::Bool => "bool",
            Scalar::F32 | Scalar::F64 => "double",
            _ => "int",
        }
    }

    /// `external` field declaration (with its `@ffi.*` annotation) of an `ffi.Struct` / `ffi.Union`.
    pub fn field(
        aliases: &[AbiAliasDescriptor],
        keep_aliases: &[AbiAliasDescriptor],
        callbacks: &[&str],
        f: &FieldLayout<'_>,
    ) -> String {
        let name = camel(f.desc.name);
        let resolved = resolve_alias(f.desc.abi_type(), aliases, keep_aliases, callbacks);
        match Ty::parse(resolved) {
            Ok(Ty::Scalar(s)) => {
                format!(
                    "@{}()\n  external {} {name};",
                    native_scalar(s),
                    dart_scalar(s)
                )
            }
            Ok(Ty::Array { len, .. }) => {
                format!(
                    "@ffi.Array({len})\n  external {} {name};",
                    native(aliases, keep_aliases, callbacks, resolved)
                )
            }
            _ => format!(
                "external {} {name};",
                native(aliases, keep_aliases, callbacks, resolved)
            ),
        }
    }

    /// Dart typedef stem of a C callback typedef (`NvRuntimeWakeFn` -> `NvRuntimeWake`).
    pub fn callback_stem(name: &str) -> &str {
        name.strip_suffix("Fn").unwrap_or(name)
    }

    /// Dart enum constant name; Dart keywords get a `Value` suffix.
    pub fn variant(name: &str) -> String {
        let id = camel(name);
        match id.as_str() {
            "null" | "bool" | "default" | "true" | "false" | "switch" | "case" | "class"
            | "const" | "final" | "in" | "is" | "new" | "this" | "var" | "void" | "with"
            | "enum" | "super" | "return" | "do" | "if" | "else" | "for" | "while" => {
                format!("{id}Value")
            }
            _ => id,
        }
    }
}

pub mod swift {
    use super::*;

    /// Swift enum case / static constant name: `lowerCamel`, with a trailing `_` for Swift
    /// keywords (and `none`, which would shadow `Optional.none`).
    pub fn variant(name: &str) -> String {
        const RESERVED: &[&str] = &[
            "Any", "Self", "as", "break", "case", "catch", "class", "continue", "default", "defer",
            "deinit", "do", "else", "enum", "extension", "fallthrough", "false", "for", "func",
            "guard", "if", "import", "in", "init", "inout", "internal", "is", "let", "nil", "none",
            "operator", "private", "protocol", "public", "repeat", "rethrows", "return", "self",
            "static", "struct", "subscript", "super", "switch", "throw", "throws", "true", "try",
            "typealias", "var", "where", "while",
        ];
        let id = camel(name);
        if RESERVED.contains(&id.as_str()) {
            format!("{id}_")
        } else {
            id
        }
    }
}

pub mod ts {
    use super::*;

    /// TS constant-object key of an enum variant (`PassThrough` -> `PASS_THROUGH`).
    pub fn key(name: &str) -> String {
        screaming(name)
    }
}