zerodds-ast 1.0.0-rc.7

Common AST for the ZeroDDS type system: source-neutral node space with per-source typed facets. Implements docs/specs/zerodds-ast-1.0.md.
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! The profile construct table is total. Spec §8.2.
//!
//! Completeness here means something checkable: every construct the
//! model can hold, and every facet property a profile can forbid, has an
//! explicit ruling in every profile. The table itself has no catch-all
//! arm, so an unruled pair is a compile error — these tests prove the
//! rulings are also *reachable* from a real model, which the compiler
//! cannot.
#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]

use zerodds_ast::facet::xtypes::{MemberIdAssignment, MemberIdOrigin};
use zerodds_ast::profile::ALL_PROFILES;
use zerodds_ast::{
    Ast, Bound, CharWidth, ConstructKind, Extensibility, FacetProperty, MemberSpec, PrimitiveKind,
    Profile, TypeKind, ValuetypeKind, Verdict, construct_verdict, facet_verdict,
};
use zerodds_ast_core::{Id, Ref};

/// One instance of every construct the model can hold.
///
/// If a construct cannot be built here, it cannot be reached by a
/// frontend either, and its ruling would be dead.
fn model_with_every_construct() -> (Ast, Vec<(ConstructKind, Id<zerodds_ast::TypeNode>)>) {
    let mut ast = Ast::new();
    let root = ast.root_module();
    let mut out = Vec::new();

    let long = ast
        .add_type(root, "L", TypeKind::Primitive(PrimitiveKind::Int32))
        .expect("L");
    out.push((ConstructKind::Primitive, long));

    let narrow = ast
        .add_type(root, "I8", TypeKind::Primitive(PrimitiveKind::Int8))
        .expect("I8");
    out.push((ConstructKind::NarrowInteger, narrow));

    let long_double = ast
        .add_type(root, "LD", TypeKind::Primitive(PrimitiveKind::Float128))
        .expect("LD");
    out.push((ConstructKind::LongDouble, long_double));

    let fixed = ast
        .add_type(
            root,
            "Money",
            TypeKind::Fixed {
                digits: 9,
                scale: 2,
            },
        )
        .expect("Money");
    out.push((ConstructKind::Fixed, fixed));

    let text = ast
        .add_type(
            root,
            "S",
            TypeKind::String {
                width: CharWidth::Char,
                bound: Bound::Unbounded,
            },
        )
        .expect("S");
    out.push((ConstructKind::String, text));

    let wide = ast
        .add_type(
            root,
            "WS",
            TypeKind::String {
                width: CharWidth::WChar,
                bound: Bound::Unbounded,
            },
        )
        .expect("WS");
    out.push((ConstructKind::WideString, wide));

    let seq = ast
        .add_type(
            root,
            "LSeq",
            TypeKind::Sequence {
                element: Ref::Resolved(long),
                bound: Bound::Unbounded,
            },
        )
        .expect("LSeq");
    out.push((ConstructKind::Sequence, seq));

    let one = ast.add_int_expr(4).expect("4");
    let two = ast.add_int_expr(2).expect("2");
    let array1 = ast
        .add_type(
            root,
            "Quad",
            TypeKind::Array {
                element: Ref::Resolved(long),
                dims: vec![one],
            },
        )
        .expect("Quad");
    out.push((ConstructKind::Array1D, array1));

    let array2 = ast
        .add_type(
            root,
            "Grid",
            TypeKind::Array {
                element: Ref::Resolved(long),
                dims: vec![one, two],
            },
        )
        .expect("Grid");
    out.push((ConstructKind::ArrayMultiD, array2));

    let map = ast
        .add_type(
            root,
            "Lookup",
            TypeKind::Map {
                key: Ref::Resolved(long),
                value: Ref::Resolved(long),
                bound: Bound::Unbounded,
            },
        )
        .expect("Lookup");
    out.push((ConstructKind::Map, map));

    let structure = ast
        .add_type(
            root,
            "Point",
            TypeKind::Structure {
                base: None,
                members: Vec::new(),
            },
        )
        .expect("Point");
    out.push((ConstructKind::Structure, structure));

    let union = ast
        .add_type(
            root,
            "Value",
            TypeKind::Union {
                discriminator: Ref::Resolved(long),
                cases: Vec::new(),
                default_case: None,
            },
        )
        .expect("Value");
    out.push((ConstructKind::Union, union));

    let enumeration = ast
        .add_type(
            root,
            "Colour",
            TypeKind::Enumeration {
                enumerators: Vec::new(),
            },
        )
        .expect("Colour");
    out.push((ConstructKind::Enumeration, enumeration));

    let bitmask = ast
        .add_type(root, "Flags", TypeKind::Bitmask { bits: Vec::new() })
        .expect("Flags");
    out.push((ConstructKind::Bitmask, bitmask));

    let bitset = ast
        .add_type(
            root,
            "Header",
            TypeKind::Bitset {
                base: None,
                fields: Vec::new(),
            },
        )
        .expect("Header");
    out.push((ConstructKind::Bitset, bitset));

    let alias = ast
        .add_type(
            root,
            "Name",
            TypeKind::Alias {
                aliased: Ref::Resolved(text),
            },
        )
        .expect("Name");
    out.push((ConstructKind::Alias, alias));

    let valuetype = ast
        .add_type(
            root,
            "V",
            TypeKind::Valuetype {
                kind: ValuetypeKind::Regular,
                bases: Vec::new(),
                state: Vec::new(),
                initializers: Vec::new(),
                supported: Vec::new(),
            },
        )
        .expect("V");
    out.push((ConstructKind::Valuetype, valuetype));

    let value_box = ast
        .add_type(
            root,
            "BoxedL",
            TypeKind::ValueBox {
                boxed: Ref::Resolved(long),
            },
        )
        .expect("BoxedL");
    out.push((ConstructKind::ValueBox, value_box));

    let native = ast
        .add_type(root, "Opaque", TypeKind::Native)
        .expect("Opaque");
    out.push((ConstructKind::Native, native));

    let any = ast.add_type(root, "any", TypeKind::Any).expect("any");
    out.push((ConstructKind::Any, any));

    let value_base = ast
        .add_type(root, "ValueBase", TypeKind::ValueBase)
        .expect("ValueBase");
    out.push((ConstructKind::ValueBase, value_base));

    let iface = ast
        .add_interface(
            root,
            "Control",
            zerodds_ast::InterfaceKind::Interface,
            Vec::new(),
        )
        .expect("Control");
    let object_ref = ast
        .add_type(
            root,
            "ControlRef",
            TypeKind::ObjectRef {
                interface: Some(Ref::Resolved(iface)),
            },
        )
        .expect("ControlRef");
    out.push((ConstructKind::ObjectRef, object_ref));

    let annotation_def = ast.add_annotation_def(root, "my_ann").expect("@my_ann");
    let annotation_type = ast
        .add_type(
            root,
            "MyAnn",
            TypeKind::AnnotationType {
                definition: annotation_def,
            },
        )
        .expect("MyAnn");
    out.push((ConstructKind::AnnotationType, annotation_type));

    (ast, out)
}

#[test]
fn the_construct_table_is_total_over_profiles_and_constructs() {
    // Not a tautology: the table has no catch-all arm, so this asserts
    // the enumeration in ConstructKind::ALL matches the arms the
    // compiler accepted.
    let mut rulings = 0usize;
    for profile in ALL_PROFILES {
        for construct in ConstructKind::ALL {
            let verdict = construct_verdict(profile, construct);
            if let Verdict::Rejected {
                rule,
                spec_reference,
                detail,
            } = verdict
            {
                assert!(!rule.is_empty(), "{profile:?}/{construct:?} rule");
                assert!(
                    !spec_reference.is_empty(),
                    "{profile:?}/{construct:?} paragraph"
                );
                assert!(!detail.is_empty(), "{profile:?}/{construct:?} detail");
            }
            rulings += 1;
        }
    }
    assert_eq!(rulings, ALL_PROFILES.len() * ConstructKind::ALL.len());
    assert_eq!(ConstructKind::ALL.len(), 23);
}

#[test]
fn the_facet_table_is_total_over_profiles_and_properties() {
    let mut rulings = 0usize;
    for profile in ALL_PROFILES {
        for property in FacetProperty::ALL {
            if let Verdict::Rejected {
                rule,
                spec_reference,
                detail,
            } = facet_verdict(profile, property)
            {
                assert!(!rule.is_empty(), "{profile:?}/{property:?} rule");
                assert!(
                    !spec_reference.is_empty(),
                    "{profile:?}/{property:?} paragraph"
                );
                assert!(!detail.is_empty(), "{profile:?}/{property:?} detail");
            }
            rulings += 1;
        }
    }
    assert_eq!(rulings, ALL_PROFILES.len() * FacetProperty::ALL.len());
    assert_eq!(FacetProperty::ALL.len(), 14);
}

#[test]
fn every_construct_the_model_can_hold_is_classified_distinctly() {
    // A construct that classifies as something else would silently
    // inherit that construct's rulings.
    let (ast, constructs) = model_with_every_construct();
    for (expected, id) in &constructs {
        let node = ast.type_(*id).expect("type");
        assert_eq!(
            ConstructKind::of(&node.kind),
            *expected,
            "{} classified wrongly",
            node.name
        );
    }
    assert_eq!(constructs.len(), ConstructKind::ALL.len());
}

#[test]
fn validation_reports_exactly_the_constructs_the_table_rejects() {
    // The link between table and validator: for each profile, the set of
    // types validate() complains about is the set the table rejects.
    let (ast, constructs) = model_with_every_construct();

    for profile in ALL_PROFILES {
        // FlatData rules on reachability rather than on the type itself.
        if profile == Profile::FlatData {
            continue;
        }
        let expected: Vec<ConstructKind> = constructs
            .iter()
            .filter(|(construct, _)| !construct_verdict(profile, *construct).is_allowed())
            .map(|(construct, _)| *construct)
            .collect();

        let reported = match ast.validate(profile) {
            Ok(()) => Vec::new(),
            Err(violations) => violations
                .iter()
                .filter_map(|v| {
                    let any = ast.resolve_node_id(v.node)?;
                    let id = any.as_type()?;
                    let node = ast.type_(id).ok()?;
                    Some(ConstructKind::of(&node.kind))
                })
                .collect::<Vec<_>>(),
        };

        for construct in &expected {
            assert!(
                reported.contains(construct),
                "{profile:?} should have rejected {construct:?}"
            );
        }
        for construct in &reported {
            assert!(
                expected.contains(construct)
                    || !construct_verdict(profile, *construct).is_allowed(),
                "{profile:?} rejected {construct:?}, which the table allows"
            );
        }
    }
}

#[test]
fn every_facet_property_is_reachable_from_a_real_node() {
    // A property nothing can carry would have dead rulings.
    let mut ast = Ast::new();
    let root = ast.root_module();
    let long = ast
        .add_type(root, "L", TypeKind::Primitive(PrimitiveKind::Int32))
        .expect("L");
    let s = ast
        .add_type(
            root,
            "Carrier",
            TypeKind::Structure {
                base: None,
                members: Vec::new(),
            },
        )
        .expect("Carrier");
    let member = ast
        .add_member(s, MemberSpec::new("x", Ref::Resolved(long)))
        .expect("x");
    let expr = ast.add_int_expr(1).expect("1");
    let any = ast.any_of_member(member).expect("any");

    {
        let facets = ast.facets_mut(any).expect("facets");
        let xtypes = facets.xtypes_mut();
        xtypes.is_key = true;
        xtypes.is_optional = true;
        xtypes.is_external = true;
        xtypes.is_must_understand = true;
        xtypes.is_non_serialized = true;
        xtypes.try_construct = Some(zerodds_ast::TryConstruct::UseDefault);
        xtypes.extensibility = Some(Extensibility::Mutable);
        xtypes.member_id = Some(zerodds_ast::MemberIdAssignment {
            effective: 1,
            origin: zerodds_ast::MemberIdOrigin::Declared,
        });
        xtypes.default_value = Some(expr);
        xtypes.range = Some(zerodds_ast::RangeConstraint {
            min: Some(expr),
            max: Some(expr),
        });
        xtypes.unit = Some("m/s".to_string());
        xtypes.verbatim.push(zerodds_ast::VerbatimDirective {
            language: "c++".to_string(),
            placement: zerodds_ast::VerbatimPlacement::BeginFile,
            text: "// hi".to_string(),
        });
        facets.ros2_mut().default_value = Some(expr);
        facets.vendor.rti = Some(zerodds_ast::RtiFacet::default());
    }

    // Every property is now set on one node, so a profile that forbids
    // any of them must complain about this member.
    let violations = ast
        .validate(Profile::Corba33)
        .expect_err("CORBA rejects most of these");
    assert!(
        violations.len() >= 10,
        "expected CORBA to reject most properties, got {}",
        violations.len()
    );

    // And the widest profile accepts every construct it rules on.
    let vendor_ok = ast.validate(Profile::Vendor(zerodds_ast::VendorKind::Rti));
    assert!(
        vendor_ok.is_ok(),
        "a vendor dialect admits what the model can hold: {vendor_ok:?}"
    );
}

/// A profile answers about the **source**, not about what lowering
/// derived from it.
///
/// `struct Sample { long a; double b; };` is CORBA IDL. Every member
/// of it carries a member id in the model, because XTypes requires one
/// and the default rule is the previous member's id plus one — a rule
/// no source ever writes. The `ExplicitMemberId` property asked
/// whether the model *had* an id rather than where the id came from,
/// so it answered yes for every aggregate ever lowered.
///
/// What that cost: `Profile::Corba33` admitted 2 of the 376 interop
/// corpus cases and `Profile::Ros2` admitted none, which made the only
/// instrument for asking "is this case even inside that toolchain's
/// specification" unusable. Nine toolchains were then measured against
/// the whole corpus instead — a corpus of XTypes constructs — and TAO
/// was recorded as refusing 127 cases that CORBA 3.3 does not define.
/// With the origin asked for, CORBA admits 199 and ROS 2 admits 146,
/// and the ORBs carry 177 of their 199.
#[test]
fn a_default_member_id_is_not_an_explicit_one() {
    for (origin, explicit) in [
        (MemberIdOrigin::PreviousPlusOne, false),
        (MemberIdOrigin::Declared, true),
        (MemberIdOrigin::AutoSequential, true),
        (MemberIdOrigin::AutoHash, true),
    ] {
        let mut ast = Ast::new();
        let root = ast.root_module();
        let long = ast
            .add_type(root, "L", TypeKind::Primitive(PrimitiveKind::Int32))
            .expect("L");
        let s = ast
            .add_type(
                root,
                "S",
                TypeKind::Structure {
                    base: None,
                    members: Vec::new(),
                },
            )
            .expect("S");
        let member = ast
            .add_member(s, MemberSpec::new("m", Ref::Resolved(long)))
            .expect("m");
        let any = ast.any_of_member(member).expect("any");
        ast.facets_mut(any).expect("facets").xtypes_mut().member_id = Some(MemberIdAssignment {
            effective: 0,
            origin: origin.clone(),
        });
        // The mutation this catches: `member_id.is_some()`, which
        // answers `true` for all four origins and makes the first
        // line of the loop fail.
        assert_eq!(
            ast.validate(Profile::Corba33).is_err(),
            explicit,
            "{origin:?} should {} put the type outside CORBA 3.3",
            if explicit { "" } else { "not" }
        );
    }
}