zerodds-idl-java 1.0.0-rc.1

OMG IDL4 → Java 17 Code-Generator (idl4-java-1.0 + DDS-Java-PSM) für ZeroDDS.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! Bitset/Bitmask → Java-Codegen (C5.4-b Cluster E).
//!
//! Java hat keine native Bitset-Syntax. Wir waehlen folgendes Mapping
//! (dokumentiert in `runtime/README.md`):
//!
//! ## Bitmask
//! `@bit_bound(N) bitmask Flags { @position(K) READ, ... };`
//! → ein Inner-Enum `Flag` mit den Werten + ein Wrapper-Class
//! `Flags`, deren Feld `EnumSet<Flag> bits` ist. Vorteil ggue.
//! `java.util.BitSet`: typsicher, idiomatisch, statisch begrenzt auf
//! die deklarierten Werte. `bit_bound` >= 64 → Mapping bleibt; der
//! parser erzwingt schon `bit_bound <= 64`.
//!
//! ## Bitset
//! `bitset MyBits { bitfield<3> a; bitfield<5> b; };`
//! → eine Klasse `MyBits` mit privatem `long bits`-Feld + Getter/
//! Setter pro Bitfield, die Maske + Shift inline in den Code
//! emittieren. Anonyme Bitfields (`name == None`) erhoehen nur den
//! Bit-Cursor. Summe der Widths > 64 → harter Fehler
//! [`JavaGenError::UnsupportedConstruct`].
//!
//! Beide Konstrukte sind self-contained — sie referenzieren keine
//! Runtime-Hilfen. Die Annotations `@bit_bound` und `@position` werden
//! im Member-Layout aufgegangen, nicht im Java-Quelltext gespiegelt.

use std::fmt::Write;

use zerodds_idl::ast::{
    Annotation, AnnotationParams, BitmaskDecl, BitsetDecl, ConstExpr, LiteralKind,
};

use crate::JavaGenOptions;
use crate::emitter::{JavaFile, fmt_err, indent_unit, wrap_compilation_unit_default};
use crate::error::JavaGenError;
use crate::keywords::sanitize_identifier;

const DEFAULT_BIT_BOUND: u32 = 32;
/// Java-`long` ist 64 Bit signed — das ist die maximale Breite, die
/// wir fuer Bitset/Bitmask abbilden koennen.
const MAX_BIT_BOUND: u32 = 64;

/// Bitmask → Wrapper-Klasse `Flags` mit nested-Enum `Flag` und
/// `EnumSet<Flag> bits`-Feld.
pub(crate) fn emit_bitmask_file(
    b: &BitmaskDecl,
    pkg: &str,
    opts: &JavaGenOptions,
) -> Result<JavaFile, JavaGenError> {
    let class = sanitize_identifier(&b.name.text)?;
    let ind = indent_unit(opts);
    let bit_bound =
        extract_int_annotation(&b.annotations, "bit_bound").unwrap_or(DEFAULT_BIT_BOUND);
    if bit_bound > MAX_BIT_BOUND {
        return Err(JavaGenError::UnsupportedConstruct {
            construct: format!("bitmask bit_bound {bit_bound} > 64"),
            context: Some(b.name.text.clone()),
        });
    }

    let mut body = String::new();
    writeln!(body, "public final class {class} {{").map_err(fmt_err)?;
    writeln!(body, "{ind}/** IDL `@bit_bound({bit_bound})` */").map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}public static final int BIT_BOUND = {bit_bound};"
    )
    .map_err(fmt_err)?;
    writeln!(body).map_err(fmt_err)?;

    // Inner enum `Flag` with explicit bit-positions.
    writeln!(body, "{ind}/** Bit-position enum for {class}. */").map_err(fmt_err)?;
    writeln!(body, "{ind}public enum Flag {{").map_err(fmt_err)?;
    let mut next_pos: u32 = 0;
    let n_values = b.values.len();
    for (idx, v) in b.values.iter().enumerate() {
        let name = sanitize_identifier(&v.name.text)?;
        let pos = extract_int_annotation(&v.annotations, "position").unwrap_or(next_pos);
        next_pos = pos + 1;
        let sep = if idx + 1 == n_values { ';' } else { ',' };
        writeln!(body, "{ind}{ind}{name}({pos}){sep}").map_err(fmt_err)?;
    }
    if n_values == 0 {
        // Empty bitmask — emit a marker `;` to keep enum syntactically valid.
        writeln!(body, "{ind}{ind};").map_err(fmt_err)?;
    }
    writeln!(body).map_err(fmt_err)?;
    writeln!(body, "{ind}{ind}private final int position;").map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}{ind}Flag(int position) {{ this.position = position; }}"
    )
    .map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}{ind}public int position() {{ return position; }}"
    )
    .map_err(fmt_err)?;
    writeln!(body, "{ind}}}").map_err(fmt_err)?;
    writeln!(body).map_err(fmt_err)?;

    // Wrapper field + accessors.
    writeln!(
        body,
        "{ind}private java.util.EnumSet<Flag> bits = java.util.EnumSet.noneOf(Flag.class);",
    )
    .map_err(fmt_err)?;
    writeln!(body).map_err(fmt_err)?;
    writeln!(body, "{ind}public {class}() {{}}").map_err(fmt_err)?;
    writeln!(body).map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}public java.util.EnumSet<Flag> bits() {{ return bits; }}"
    )
    .map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}public void setBits(java.util.EnumSet<Flag> bits) \
         {{ this.bits = java.util.EnumSet.copyOf(bits); }}",
    )
    .map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}public boolean isSet(Flag f) {{ return bits.contains(f); }}",
    )
    .map_err(fmt_err)?;
    writeln!(body, "{ind}public void set(Flag f) {{ bits.add(f); }}").map_err(fmt_err)?;
    writeln!(body, "{ind}public void clear(Flag f) {{ bits.remove(f); }}").map_err(fmt_err)?;
    writeln!(body, "}}").map_err(fmt_err)?;

    let source = wrap_compilation_unit_default(pkg, &body);
    Ok(JavaFile {
        package_path: pkg.to_string(),
        class_name: class,
        source,
    })
}

/// Bitset → wrapper-class with `long bits` + accessor pair per
/// (named) bitfield.
pub(crate) fn emit_bitset_file(
    b: &BitsetDecl,
    pkg: &str,
    opts: &JavaGenOptions,
) -> Result<JavaFile, JavaGenError> {
    let class = sanitize_identifier(&b.name.text)?;
    let ind = indent_unit(opts);

    // Cumulative bit-position cursor.
    let mut cursor: u32 = 0;
    // Pre-validate total width and gather (name, width, offset) tuples.
    struct Field {
        name: String,
        width: u32,
        offset: u32,
        anonymous: bool,
    }
    let mut fields: Vec<Field> = Vec::new();
    for bf in &b.bitfields {
        let width =
            bitfield_width(&bf.spec.width).ok_or_else(|| JavaGenError::UnsupportedConstruct {
                construct: format!(
                    "bitfield width must be integer literal in '{}'",
                    b.name.text
                ),
                context: Some(b.name.text.clone()),
            })?;
        if width == 0 || width > MAX_BIT_BOUND {
            return Err(JavaGenError::UnsupportedConstruct {
                construct: format!("bitfield width {width} (must be 1..=64)"),
                context: Some(b.name.text.clone()),
            });
        }
        let offset = cursor;
        cursor = cursor.saturating_add(width);
        if cursor > MAX_BIT_BOUND {
            return Err(JavaGenError::UnsupportedConstruct {
                construct: format!("bitset cumulative width {cursor} > 64 (Java-long-Backing)"),
                context: Some(b.name.text.clone()),
            });
        }
        let (sane_name, anonymous) = match &bf.name {
            Some(id) => (sanitize_identifier(&id.text)?, false),
            None => (format!("_pad_{offset}"), true),
        };
        fields.push(Field {
            name: sane_name,
            width,
            offset,
            anonymous,
        });
    }

    let mut body = String::new();
    writeln!(body, "public final class {class} {{").map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}/** Cumulative bit-storage (width = {cursor}). */"
    )
    .map_err(fmt_err)?;
    writeln!(body, "{ind}public static final int BIT_WIDTH = {cursor};").map_err(fmt_err)?;
    writeln!(body, "{ind}private long bits;").map_err(fmt_err)?;
    writeln!(body).map_err(fmt_err)?;
    writeln!(body, "{ind}public {class}() {{}}").map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}public {class}(long bits) {{ this.bits = bits; }}",
    )
    .map_err(fmt_err)?;
    writeln!(body, "{ind}public long rawBits() {{ return bits; }}").map_err(fmt_err)?;
    writeln!(
        body,
        "{ind}public void setRawBits(long bits) {{ this.bits = bits; }}",
    )
    .map_err(fmt_err)?;
    writeln!(body).map_err(fmt_err)?;

    for f in &fields {
        if f.anonymous {
            // Padding-Bitfield — kein Accessor, nur Layout-Kommentar.
            writeln!(
                body,
                "{ind}// padding: width={} offset={}",
                f.width, f.offset,
            )
            .map_err(fmt_err)?;
            continue;
        }
        let mask: u64 = if f.width == 64 {
            u64::MAX
        } else {
            (1u64 << f.width) - 1
        };
        let cap = capitalize(&f.name);
        let return_ty = if f.width <= 32 { "int" } else { "long" };
        let cast_in = if f.width <= 32 { "(int)" } else { "" };
        let cast_set = if f.width <= 32 { "(long)" } else { "" };
        writeln!(body, "{ind}/** width={} offset={} */", f.width, f.offset,).map_err(fmt_err)?;
        writeln!(
            body,
            "{ind}public {return_ty} get{cap}() {{ return {cast_in}((bits >>> {offset}) & 0x{mask:X}L); }}",
            offset = f.offset,
        )
        .map_err(fmt_err)?;
        writeln!(
            body,
            "{ind}public void set{cap}({return_ty} value) {{ \
             bits = (bits & ~(0x{mask:X}L << {offset})) | \
             ((({cast_set}value) & 0x{mask:X}L) << {offset}); }}",
            offset = f.offset,
        )
        .map_err(fmt_err)?;
    }

    writeln!(body, "}}").map_err(fmt_err)?;

    let source = wrap_compilation_unit_default(pkg, &body);
    Ok(JavaFile {
        package_path: pkg.to_string(),
        class_name: class,
        source,
    })
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn bitfield_width(e: &ConstExpr) -> Option<u32> {
    if let ConstExpr::Literal(l) = e {
        if matches!(l.kind, LiteralKind::Integer) {
            return l.raw.parse::<u32>().ok();
        }
    }
    None
}

fn extract_int_annotation(anns: &[Annotation], name: &str) -> Option<u32> {
    let a = anns
        .iter()
        .find(|a| a.name.parts.last().map(|p| p.text.as_str()) == Some(name))?;
    if let AnnotationParams::Single(ConstExpr::Literal(l)) = &a.params {
        if matches!(l.kind, LiteralKind::Integer) {
            return l.raw.parse::<u32>().ok();
        }
    }
    None
}

fn capitalize(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
        None => String::new(),
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
    use super::*;
    use crate::JavaGenOptions;
    use zerodds_idl::config::ParserConfig;

    fn parse(src: &str) -> zerodds_idl::ast::Specification {
        zerodds_idl::parse(src, &ParserConfig::default()).expect("parse")
    }

    fn first_bitmask(ast: &zerodds_idl::ast::Specification) -> &BitmaskDecl {
        for d in &ast.definitions {
            if let zerodds_idl::ast::Definition::Type(zerodds_idl::ast::TypeDecl::Constr(
                zerodds_idl::ast::ConstrTypeDecl::Bitmask(b),
            )) = d
            {
                return b;
            }
        }
        panic!("no bitmask in fixture");
    }

    fn first_bitset(ast: &zerodds_idl::ast::Specification) -> &BitsetDecl {
        for d in &ast.definitions {
            if let zerodds_idl::ast::Definition::Type(zerodds_idl::ast::TypeDecl::Constr(
                zerodds_idl::ast::ConstrTypeDecl::Bitset(b),
            )) = d
            {
                return b;
            }
        }
        panic!("no bitset in fixture");
    }

    #[test]
    fn bitmask_default_bit_bound_is_32() {
        let ast = parse("bitmask Flags { READ, WRITE };");
        let b = first_bitmask(&ast);
        let f = emit_bitmask_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("BIT_BOUND = 32"));
        assert!(f.source.contains("READ(0)"));
        assert!(f.source.contains("WRITE(1)"));
    }

    #[test]
    fn bitmask_explicit_bit_bound_8_emits() {
        let ast = parse("@bit_bound(8) bitmask Flags { F0, F1 };");
        let b = first_bitmask(&ast);
        let f = emit_bitmask_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("BIT_BOUND = 8"));
    }

    #[test]
    fn bitmask_position_overrides_implicit() {
        let ast = parse("@bit_bound(16) bitmask M { @position(3) A, B };");
        let b = first_bitmask(&ast);
        let f = emit_bitmask_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("A(3)"));
        // B follows at position 4 (next_implicit after explicit 3).
        assert!(f.source.contains("B(4)"));
    }

    #[test]
    fn bitmask_uses_enumset_field() {
        let ast = parse("@bit_bound(8) bitmask M { A };");
        let b = first_bitmask(&ast);
        let f = emit_bitmask_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("java.util.EnumSet<Flag>"));
        assert!(f.source.contains("public boolean isSet(Flag f)"));
    }

    #[test]
    fn bitmask_too_large_bit_bound_errors() {
        // Construct synthetic — parser would reject >64 too, but we
        // also defend in the emitter.
        let mut b = BitmaskDecl {
            name: zerodds_idl::ast::Identifier::new("M", zerodds_idl::errors::Span::SYNTHETIC),
            values: alloc::vec![],
            annotations: alloc::vec![],
            span: zerodds_idl::errors::Span::SYNTHETIC,
        };
        // craft @bit_bound(65)
        b.annotations.push(synth_annotation("bit_bound", "65"));
        let res = emit_bitmask_file(&b, "", &JavaGenOptions::default());
        assert!(matches!(
            res,
            Err(JavaGenError::UnsupportedConstruct { .. })
        ));
    }

    #[test]
    fn bitset_simple_emits_long_backing() {
        let ast = parse("bitset MyBits { bitfield<3> a; bitfield<5> b; };");
        let b = first_bitset(&ast);
        let f = emit_bitset_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("public final class MyBits"));
        assert!(f.source.contains("private long bits;"));
        assert!(f.source.contains("public int getA()"));
        assert!(f.source.contains("public int getB()"));
        // a: width=3, offset=0 → mask 0x7
        assert!(f.source.contains("0x7L"));
        // b: width=5, offset=3 → mask 0x1F
        assert!(f.source.contains("0x1FL"));
    }

    #[test]
    fn bitset_total_width_over_64_errors() {
        let ast = parse("bitset Big { bitfield<40> a; bitfield<30> b; };");
        let b = first_bitset(&ast);
        let res = emit_bitset_file(b, "", &JavaGenOptions::default());
        assert!(
            matches!(res, Err(JavaGenError::UnsupportedConstruct { .. })),
            "expected UnsupportedConstruct, got {res:?}",
        );
    }

    #[test]
    fn bitset_with_anonymous_padding_skips_accessor() {
        let ast = parse("bitset P { bitfield<2> a; bitfield<3>; bitfield<4> b; };");
        let b = first_bitset(&ast);
        let f = emit_bitset_file(b, "", &JavaGenOptions::default()).expect("ok");
        // accessor for a + b but not the padding.
        assert!(f.source.contains("public int getA()"));
        assert!(f.source.contains("public int getB()"));
        assert!(f.source.contains("padding: width=3 offset=2"));
    }

    #[test]
    fn bitset_64bit_field_uses_long_return() {
        let ast = parse("bitset Big { bitfield<64> a; };");
        let b = first_bitset(&ast);
        let f = emit_bitset_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("public long getA()"));
    }

    #[test]
    fn bitset_large_field_above_32_returns_long() {
        let ast = parse("bitset Big { bitfield<40> a; };");
        let b = first_bitset(&ast);
        let f = emit_bitset_file(b, "", &JavaGenOptions::default()).expect("ok");
        assert!(f.source.contains("public long getA()"));
    }

    // ---- synthetic helpers (avoid IDL parse for >64 bit_bound which is rejected) ----

    extern crate alloc;

    fn synth_annotation(name: &str, raw: &str) -> zerodds_idl::ast::Annotation {
        use zerodds_idl::ast::{Identifier, Literal, ScopedName};
        use zerodds_idl::errors::Span;
        zerodds_idl::ast::Annotation {
            name: ScopedName {
                absolute: false,
                parts: alloc::vec![Identifier::new(name, Span::SYNTHETIC)],
                span: Span::SYNTHETIC,
            },
            params: AnnotationParams::Single(ConstExpr::Literal(Literal {
                kind: LiteralKind::Integer,
                raw: raw.to_string(),
                span: Span::SYNTHETIC,
            })),
            span: Span::SYNTHETIC,
        }
    }
}