zerodds-lang-rules 1.0.0-rc.7.1

Maps the common AST onto the language CSTs through rules that are data, not code: seesaw v2 rule specifications, comparable and checkable.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! The octets of the topic types of the Enhanced Service Mapping, for the
//! C++ binding of DDS-RPC 1.0.
//!
//! `dds::rpc::Requester` and `dds::rpc::Replier` under the Enhanced profile
//! (§7.8.2) write no header: the sample is the bare `${I}_Request` or
//! `${I}_Reply` structure, serialized as PL_CDR2 because the clause reads
//! an `@Choice` structure "as if the structure had the
//! `@Extensibility(MUTABLE_EXTENSIBILITY)` annotation and all the members
//! of the structure had the `@Optional` annotation" (§7.5.1.2.1.1). This
//! writes the encoder and the decoder of each structure and the
//! `dds_type_traits` of the two topic types.
//!
//! The forms are those of `emit::interface_rust::enhanced`, octet for octet:
//!
//! - a choice structure (`Request`, `Reply`, `Result`): a `DHEADER`, the
//!   `EMHEADER` of the one member present (member id = HASH & 0x0FFFFFFF,
//!   length code 4 with the `NEXTINT`; 2 for `remoteEx`, a 32-bit value),
//!   the member;
//! - a `@Mutable` `In` or `Out`: a `DHEADER`, an `EMHEADER` and the value of
//!   each member, the length code the member's type calls for
//!   ([`crate::emit::interface_rust::emheader_length_code`]); on decode in
//!   any order, a member that is absent takes its default (XTypes 1.3
//!   §7.2.4.4.8) and one that is not known is skipped by its length;
//! - any other `In` or `Out`, and an exception: the members one after the
//!   other.
//!
//! The octets of a member's own type are those of the shared codec walk
//! (`super::codec::member_codecs`), under the key the Basic mapping uses.

use super::codec::{
    assign, binary, block, call, const_ref, dds_type_name, decode_member, dot, encode_member,
    exception_members, expr_statement, function, method, mut_ref, name, param, path, reader_type,
    scoped, with_unused, writer_type,
};
use super::{cxx_escape, qualified};
use crate::adapter::interface::{
    BindingError, EnhancedMemberType, EnhancedOperation, EnhancedService, EnhancedStruct, Raise,
    Service,
};
use zerodds_ast::{Ast, ExceptionNode, ModuleNode};
use zerodds_ast_core::Id;
use zerodds_lang_cst::common::{Doc, Ident, Literal, Path};
use zerodds_lang_cst::cpp;

/// The remote exception codes of §7.5.2 are `0..=REMOTE_EX_MAX`.
const REMOTE_EX_MAX: i128 = 5;

/// The size of an `EMHEADER`: what is left of a choice structure after its
/// one member, when it is at least this much, is a second member.
const EMHEADER_SIZE: i128 = 4;

/// `zerodds::wire::Status::Ok`.
fn status_ok() -> cpp::Expr {
    path(&["zerodds", "wire", "Status", "Ok"])
}

fn int(value: i128) -> cpp::Expr {
    cpp::Expr::Literal(Literal::int(value))
}

fn unsigned_type(name_: &str) -> cpp::Type {
    qualified("std", name_)
}

fn zero_of(type_: cpp::Type) -> cpp::Expr {
    cpp::Expr::Construct {
        type_: Box::new(type_),
        args: Vec::new(),
        braced: true,
    }
}

/// The identifier a structure's functions are named by: its scope and its
/// name, so that two structures of two modules do not meet.
fn suffix(ast: &Ast, module: Id<ModuleNode>, simple: &str) -> Result<String, BindingError> {
    Ok(dds_type_name(ast, module, simple)?.replace("::", "_"))
}

/// `zd_enc_enh_<scope>_<Name>`.
pub(super) fn enc_struct(
    ast: &Ast,
    module: Id<ModuleNode>,
    simple: &str,
) -> Result<String, BindingError> {
    Ok(format!("zd_enc_enh_{}", suffix(ast, module, simple)?))
}

/// `zd_dec_enh_<scope>_<Name>`.
pub(super) fn dec_struct(
    ast: &Ast,
    module: Id<ModuleNode>,
    simple: &str,
) -> Result<String, BindingError> {
    Ok(format!("zd_dec_enh_{}", suffix(ast, module, simple)?))
}

fn exception_module(ast: &Ast, raised: &Raise) -> Result<Id<ModuleNode>, BindingError> {
    Ok(ast
        .exception(raised.id)
        .map_err(|error| BindingError::Codec(format!("an exception is gone: {error:?}")))?
        .module)
}

fn enc_exception(ast: &Ast, raised: &Raise) -> Result<String, BindingError> {
    let module = exception_module(ast, raised)?;
    Ok(format!(
        "zd_enc_enh_ex_{}",
        suffix(ast, module, &cxx_escape(&raised.name))?
    ))
}

fn dec_exception(ast: &Ast, raised: &Raise) -> Result<String, BindingError> {
    let module = exception_module(ast, raised)?;
    Ok(format!(
        "zd_dec_enh_ex_{}",
        suffix(ast, module, &cxx_escape(&raised.name))?
    ))
}

/// The pair of a structure's functions: parameters `(Writer&, const T&)`
/// and `(Reader&, T&)`.
fn pair(
    enc: &str,
    dec: &str,
    type_name: &str,
    encode: Vec<cpp::Statement>,
    decode: Vec<cpp::Statement>,
    what: &str,
) -> Vec<cpp::Decl> {
    let type_ = cpp::Type::named(Path::single(type_name));
    vec![
        function(
            enc,
            vec![
                param(writer_type(), "w"),
                param(const_ref(type_.clone()), "v"),
            ],
            with_unused(encode, "w"),
            &format!("The octets of {what}. DDS-RPC 1.0 §7.5.1.2."),
        ),
        function(
            dec,
            vec![param(reader_type(), "r"), param(mut_ref(type_), "v")],
            with_unused(decode, "r"),
            &format!("{what} from its octets. DDS-RPC 1.0 §7.5.1.2."),
        ),
    ]
}

/// One member of an `In` or an `Out`.
enum Slot {
    Typed(zerodds_ast_core::Id<zerodds_ast::TypeNode>),
    Dummy,
}

/// Write one member's value.
fn write_slot(ast: &Ast, slot: &Slot, member: &str) -> cpp::Statement {
    match slot {
        Slot::Typed(id) => encode_member(ast, *id, dot(name("v"), member)),
        Slot::Dummy => expr_statement(method(name("w"), "write_u8", vec![dot(name("v"), member)])),
    }
}

/// Read one member's value.
fn read_slot(ast: &Ast, slot: &Slot, member: &str) -> cpp::Statement {
    match slot {
        Slot::Typed(id) => decode_member(ast, *id, dot(name("v"), member)),
        Slot::Dummy => assign(
            dot(name("v"), member),
            method(name("r"), "read_u8", Vec::new()),
        ),
    }
}

fn slots_of(structure: &EnhancedStruct) -> Result<Vec<(String, u32, Slot)>, BindingError> {
    let mut out = Vec::new();
    for member in &structure.members {
        let slot = match &member.type_ {
            EnhancedMemberType::Idl(id) => Slot::Typed(*id),
            EnhancedMemberType::Dummy => Slot::Dummy,
            _ => {
                return Err(BindingError::Codec(format!(
                    "member `{}` of `{}` is not a member of an In or Out structure",
                    member.name, structure.name
                )));
            }
        };
        out.push((cxx_escape(&member.name), member.member_id, slot));
    }
    Ok(out)
}

/// The encoder and the decoder of an `In` or an `Out` structure.
fn fields_codec(
    ast: &Ast,
    module: Id<ModuleNode>,
    structure: &EnhancedStruct,
) -> Result<Vec<cpp::Decl>, BindingError> {
    let type_name = scoped(ast, module, &structure.name)?;
    let enc = enc_struct(ast, module, &structure.name)?;
    let dec = dec_struct(ast, module, &structure.name)?;
    let slots = slots_of(structure)?;
    let mut encode = Vec::new();
    let mut decode = Vec::new();
    if !structure.mutable {
        for (member, _, slot) in &slots {
            encode.push(write_slot(ast, slot, member));
            decode.push(read_slot(ast, slot, member));
        }
        return Ok(pair(
            &enc,
            &dec,
            &type_name,
            encode,
            decode,
            "one structure of the call",
        ));
    }

    // PL_CDR2: a DHEADER, then an EMHEADER and the value of each member.
    encode.push(cpp::Statement::Declare {
        type_: cpp::Type::Auto,
        name: Ident::new("zd_dheader"),
        value: Some(method(name("w"), "write_dheader", Vec::new())),
    });
    for (index, (member, id, slot)) in slots.iter().enumerate() {
        let code = match slot {
            Slot::Typed(target) => {
                crate::emit::interface_rust::emheader_length_code(ast, module, *target)
            }
            Slot::Dummy => 0,
        };
        let header = format!("zd_member_{index}");
        encode.push(cpp::Statement::Declare {
            type_: cpp::Type::Auto,
            name: Ident::new(&header),
            value: Some(method(
                name("w"),
                "write_emheader",
                vec![
                    int(i128::from(*id)),
                    int(i128::from(code)),
                    cpp::Expr::Literal(Literal::Bool(false)),
                ],
            )),
        });
        encode.push(write_slot(ast, slot, member));
        encode.push(expr_statement(method(
            name("w"),
            "finish_emheader",
            vec![name(&header)],
        )));
    }
    encode.push(expr_statement(method(
        name("w"),
        "finish_dheader",
        vec![name("zd_dheader")],
    )));

    // A member the peer did not send keeps the default of its type
    // (XTypes 1.3 §7.2.4.4.8), one this does not know is skipped by its
    // length.
    decode.push(assign(
        name("v"),
        zero_of(cpp::Type::named(Path::single(&type_name))),
    ));
    decode.extend(frame_open());
    let mut arms = Vec::new();
    for (member, id, slot) in &slots {
        arms.push((
            binary(
                "==",
                dot(name("zd_header"), "member_id"),
                int(i128::from(*id)),
            ),
            block(vec![read_slot(ast, slot, member)]),
        ));
    }
    let mut loop_body = member_header();
    if !arms.is_empty() {
        loop_body.extend(one_of(arms));
    }
    loop_body.push(skip_to("zd_after"));
    decode.push(cpp::Statement::While {
        condition: binary(
            "&&",
            binary(
                "<",
                method(name("r"), "position", Vec::new()),
                name("zd_end"),
            ),
            binary(
                "==",
                method(name("r"), "reader_status", Vec::new()),
                status_ok(),
            ),
        ),
        body: block(loop_body),
    });
    decode.push(skip_to("zd_end"));
    Ok(pair(
        &enc,
        &dec,
        &type_name,
        encode,
        decode,
        "one structure of the call",
    ))
}

/// `if (a) {..}` for each arm, one after the other: the conditions exclude
/// one another, and a chain of `else` would nest as deep as there are
/// operations, which a compiler limits.
fn one_of(arms: Vec<(cpp::Expr, cpp::Block)>) -> Vec<cpp::Statement> {
    arms.into_iter()
        .map(|(condition, then)| cpp::Statement::If {
            condition,
            then,
            otherwise: None,
        })
        .collect()
}

/// `r.reader_skip_to(target);`
fn skip_to(target: &str) -> cpp::Statement {
    expr_statement(method(name("r"), "reader_skip_to", vec![name(target)]))
}

/// `const std::uint32_t zd_length = r.read_dheader();`
/// `const std::size_t zd_end = r.position() + zd_length;`
fn frame_open() -> Vec<cpp::Statement> {
    vec![
        cpp::Statement::Declare {
            type_: cpp::Type::Const(Box::new(unsigned_type("uint32_t"))),
            name: Ident::new("zd_length"),
            value: Some(method(name("r"), "read_dheader", Vec::new())),
        },
        cpp::Statement::Declare {
            type_: cpp::Type::Const(Box::new(unsigned_type("size_t"))),
            name: Ident::new("zd_end"),
            value: Some(binary(
                "+",
                method(name("r"), "position", Vec::new()),
                name("zd_length"),
            )),
        },
    ]
}

/// The `EMHEADER` of the next member, and where that member ends.
fn member_header() -> Vec<cpp::Statement> {
    vec![
        cpp::Statement::Declare {
            type_: cpp::Type::Const(Box::new(cpp::Type::Auto)),
            name: Ident::new("zd_header"),
            value: Some(method(name("r"), "read_emheader", Vec::new())),
        },
        cpp::Statement::Declare {
            type_: cpp::Type::Const(Box::new(unsigned_type("size_t"))),
            name: Ident::new("zd_after"),
            value: Some(binary(
                "+",
                method(name("r"), "position", Vec::new()),
                dot(name("zd_header"), "extent"),
            )),
        },
    ]
}

/// One member of a choice structure.
struct Case {
    /// The C++ member, a `std::optional`.
    member: String,
    member_id: u32,
    /// The name of the member's encoder and decoder; `None` for `remoteEx`.
    functions: Option<(String, String)>,
}

/// The encoder and the decoder of a choice structure.
fn choice_codec(
    ast: &Ast,
    module: Id<ModuleNode>,
    structure: &EnhancedStruct,
    cases: &[Case],
    what: &str,
) -> Result<Vec<cpp::Decl>, BindingError> {
    let type_name = scoped(ast, module, &structure.name)?;
    let enc = enc_struct(ast, module, &structure.name)?;
    let dec = dec_struct(ast, module, &structure.name)?;

    let mut encode = vec![cpp::Statement::Declare {
        type_: cpp::Type::Primitive(cpp::Primitive::Int32),
        name: Ident::new("zd_present"),
        value: Some(int(0)),
    }];
    for case in cases {
        encode.push(cpp::Statement::If {
            condition: dot(name("v"), &case.member),
            then: block(vec![expr_statement(binary(
                "+=",
                name("zd_present"),
                int(1),
            ))]),
            otherwise: None,
        });
    }
    // "exactly one member shall be present at any given time" (§7.5.1.2.1.1).
    encode.push(cpp::Statement::If {
        condition: binary("!=", name("zd_present"), int(1)),
        then: block(vec![cpp::Statement::Throw(call(
            path(&["dds", "rpc", "UnsupportedError"]),
            vec![cpp::Expr::Literal(Literal::Str(format!(
                "{} holds exactly one member (DDS-RPC 1.0 §7.5.1.2.1.1)",
                structure.name
            )))],
        ))]),
        otherwise: None,
    });
    encode.push(cpp::Statement::Declare {
        type_: cpp::Type::Auto,
        name: Ident::new("zd_dheader"),
        value: Some(method(name("w"), "write_dheader", Vec::new())),
    });
    let mut encode_arms = Vec::new();
    for case in cases {
        let value = cpp::Expr::Deref(Box::new(dot(name("v"), &case.member)));
        let (code, write) = match &case.functions {
            Some((enc_member, _)) => (
                4,
                expr_statement(call(name(enc_member), vec![name("w"), value])),
            ),
            None => (
                2,
                expr_statement(method(
                    name("w"),
                    "write_u32",
                    vec![cpp::Expr::Cast {
                        kind: cpp::CastKind::Static,
                        type_: Box::new(unsigned_type("uint32_t")),
                        value: Box::new(value),
                    }],
                )),
            ),
        };
        encode_arms.push((
            dot(name("v"), &case.member),
            block(vec![
                cpp::Statement::Declare {
                    type_: cpp::Type::Auto,
                    name: Ident::new("zd_member"),
                    value: Some(method(
                        name("w"),
                        "write_emheader",
                        vec![
                            int(i128::from(case.member_id)),
                            int(code),
                            cpp::Expr::Literal(Literal::Bool(false)),
                        ],
                    )),
                },
                write,
                expr_statement(method(
                    name("w"),
                    "finish_emheader",
                    vec![name("zd_member")],
                )),
            ]),
        ));
    }
    encode.extend(one_of(encode_arms));
    encode.push(expr_statement(method(
        name("w"),
        "finish_dheader",
        vec![name("zd_dheader")],
    )));

    // Exactly one member is present; a member this does not know is
    // skipped by its length and leaves every case empty.
    let mut decode = vec![assign(
        name("v"),
        zero_of(cpp::Type::named(Path::single(&type_name))),
    )];
    decode.extend(frame_open());
    let mut decode_arms = Vec::new();
    for case in cases {
        let body = match &case.functions {
            Some((_, dec_member)) => vec![
                expr_statement(method(dot(name("v"), &case.member), "emplace", Vec::new())),
                expr_statement(call(
                    name(dec_member),
                    vec![
                        name("r"),
                        cpp::Expr::Deref(Box::new(dot(name("v"), &case.member))),
                    ],
                )),
            ],
            None => vec![
                cpp::Statement::Declare {
                    type_: cpp::Type::Const(Box::new(unsigned_type("uint32_t"))),
                    name: Ident::new("zd_code"),
                    value: Some(method(name("r"), "read_u32", Vec::new())),
                },
                // §7.5.2: a code outside the six is not one this binding
                // understands.
                cpp::Statement::If {
                    condition: binary(">", name("zd_code"), int(REMOTE_EX_MAX)),
                    then: block(vec![super::codec::status_refuse()]),
                    otherwise: Some(block(vec![assign(
                        dot(name("v"), &case.member),
                        cpp::Expr::Cast {
                            kind: cpp::CastKind::Static,
                            type_: Box::new(qualified("dds::rpc", "RemoteExceptionCode_t")),
                            value: Box::new(name("zd_code")),
                        },
                    )])),
                },
            ],
        };
        decode_arms.push((
            binary(
                "==",
                dot(name("zd_header"), "member_id"),
                int(i128::from(case.member_id)),
            ),
            block(body),
        ));
    }
    // §7.5.1.2.1.1: "exactly one member shall be present at any given
    // time". A structure with no member at all is as wrong as one with two;
    // the replier answers the request that cannot be decoded with
    // `REMOTE_EX_INVALID_ARGUMENT` (§7.5.2).
    decode.push(cpp::Statement::If {
        condition: binary("<", name("zd_length"), int(EMHEADER_SIZE)),
        then: block(vec![super::codec::status_refuse()]),
        otherwise: None,
    });
    let mut guarded = member_header();
    guarded.extend(one_of(decode_arms));
    guarded.push(skip_to("zd_after"));
    // A further member behind the first: two options are active.
    guarded.push(cpp::Statement::If {
        condition: binary(
            "<=",
            binary(
                "+",
                method(name("r"), "position", Vec::new()),
                int(EMHEADER_SIZE),
            ),
            name("zd_end"),
        ),
        then: block(vec![super::codec::status_refuse()]),
        otherwise: None,
    });
    decode.push(cpp::Statement::If {
        condition: binary(
            "&&",
            binary(
                "<",
                method(name("r"), "position", Vec::new()),
                name("zd_end"),
            ),
            binary(
                "==",
                method(name("r"), "reader_status", Vec::new()),
                status_ok(),
            ),
        ),
        then: block(guarded),
        otherwise: None,
    });
    decode.push(skip_to("zd_end"));
    Ok(pair(&enc, &dec, &type_name, encode, decode, what))
}

/// The raised exceptions of the operations, each once however many raise it.
fn raised_of(operations: &[&EnhancedOperation]) -> Vec<Raise> {
    let mut out: Vec<Raise> = Vec::new();
    for operation in operations {
        for raised in &operation.operation.raises {
            if !out.iter().any(|seen| seen.id == raised.id) {
                out.push(raised.clone());
            }
        }
    }
    out
}

/// The encoder and the decoder of one exception, in the module of the
/// exception.
fn exception_codec(ast: &Ast, raised: &Raise) -> Result<Vec<cpp::Decl>, BindingError> {
    let module = exception_module(ast, raised)?;
    let type_name = scoped(ast, module, &cxx_escape(&raised.name))?;
    let mut encode = Vec::new();
    let mut decode = Vec::new();
    for (member, id) in exception_members(ast, raised)? {
        encode.push(encode_member(ast, id, dot(name("v"), &member)));
        decode.push(decode_member(ast, id, dot(name("v"), &member)));
    }
    Ok(pair(
        &enc_exception(ast, raised)?,
        &dec_exception(ast, raised)?,
        &type_name,
        encode,
        decode,
        "one exception",
    ))
}

/// Everything the `dds_type_traits` of one interface's topic types stand
/// on: the codecs of the structures it declares itself, those of the
/// exceptions its operations raise (each exception once for the file,
/// whatever `declared` says), and those of its `Request` and `Reply`.
///
/// The structures of an inherited operation (§7.5.1.2.6) are written by the
/// base interface; this refers to them by name.
pub(super) fn codecs(
    ast: &Ast,
    service: &Service,
    model: &EnhancedService,
    declared: &mut Vec<Id<ExceptionNode>>,
) -> Result<Vec<cpp::Decl>, BindingError> {
    let module = service.module;
    let own: Vec<&EnhancedOperation> = model
        .operations
        .iter()
        .filter(|operation| operation.module == module && operation.interface == service.name)
        .collect();
    let mut out = Vec::new();
    for raised in raised_of(&own) {
        if declared.contains(&raised.id) {
            continue;
        }
        declared.push(raised.id);
        out.extend(exception_codec(ast, &raised)?);
    }
    for operation in &own {
        out.extend(fields_codec(ast, module, &operation.in_struct)?);
        out.extend(fields_codec(ast, module, &operation.out_struct)?);
        let mut cases = Vec::new();
        for member in &operation.result_struct.members {
            let functions = match &member.type_ {
                EnhancedMemberType::Struct(structure) => (
                    enc_struct(ast, module, structure)?,
                    dec_struct(ast, module, structure)?,
                ),
                EnhancedMemberType::Exception(raised) => {
                    (enc_exception(ast, raised)?, dec_exception(ast, raised)?)
                }
                _ => continue,
            };
            cases.push(Case {
                member: cxx_escape(&member.name),
                member_id: member.member_id,
                functions: Some(functions),
            });
        }
        out.extend(choice_codec(
            ast,
            module,
            &operation.result_struct,
            &cases,
            "the outcome of one operation",
        )?);
    }
    for (structure, reply) in [(&model.request, false), (&model.reply, true)] {
        let mut cases = Vec::new();
        for member in &structure.members {
            match &member.type_ {
                EnhancedMemberType::Struct(name_) => {
                    let owner = model
                        .operations
                        .iter()
                        .find(|operation| operation.operation.name == member.name)
                        .map_or(module, |operation| operation.module);
                    cases.push(Case {
                        member: cxx_escape(&member.name),
                        member_id: member.member_id,
                        functions: Some((
                            enc_struct(ast, owner, name_)?,
                            dec_struct(ast, owner, name_)?,
                        )),
                    });
                }
                EnhancedMemberType::RemoteException => cases.push(Case {
                    member: cxx_escape(&member.name),
                    member_id: member.member_id,
                    functions: None,
                }),
                _ => {}
            }
        }
        out.extend(choice_codec(
            ast,
            module,
            structure,
            &cases,
            if reply { "one reply" } else { "one request" },
        )?);
    }
    Ok(out)
}

fn static_method(
    returns: cpp::Type,
    method_name: &str,
    params: Vec<cpp::Param>,
    statements: Vec<cpp::Statement>,
    doc: &str,
) -> cpp::ClassMember {
    cpp::ClassMember::Method(cpp::Method {
        doc: Doc::line(doc),
        returns,
        name: Ident::new(method_name),
        params,
        is_const: false,
        is_static: true,
        is_virtual: false,
        is_pure: false,
        is_constexpr: false,
        is_noexcept: false,
        is_override: false,
        body: Some(block(statements)),
    })
}

/// `namespace dds::rpc { template <> struct dds_type_traits<T> {..}; }` for
/// the request and the reply type of one interface.
///
/// The sample is the bare structure, PL_CDR2, little-endian: whatever the
/// `WireEncoding` of the params says, the clause fixes this form (§7.5.1.2).
pub(super) fn traits(
    ast: &Ast,
    service: &Service,
    model: &EnhancedService,
) -> Result<cpp::Decl, BindingError> {
    let mut decls = Vec::new();
    let octets = qualified("dds::rpc", "Octets");
    let encoding = qualified("dds::rpc", "WireEncoding");
    let wire = |last: &str| Path::new(&["zerodds", "wire", last]);
    let little = path(&["zerodds", "wire", "Endianness", "Little"]);
    // XCDR2 caps every alignment at four (XTypes 1.3 §7.4.1).
    let alignment = int(4);
    // The XTypes description of the two topic types; a type with no
    // TypeObject (a recursive one) leaves the topics matched by type name.
    let objects =
        crate::emit::interface_rust::type_information::service_type_objects(ast, service, model)
            .ok();
    for (structure, reply) in [(&model.request, false), (&model.reply, true)] {
        let type_name = scoped(ast, service.module, &structure.name)?;
        let dds_name = dds_type_name(ast, service.module, &structure.name)?;
        let topic_type = cpp::Type::named(Path::single(&type_name));
        let enc = enc_struct(ast, service.module, &structure.name)?;
        let dec = dec_struct(ast, service.module, &structure.name)?;
        let unnamed = |type_: cpp::Type| cpp::Param {
            type_,
            name: None,
            default: None,
        };

        let serialize_at = static_method(
            octets.clone(),
            "serialize_at",
            vec![
                param(const_ref(topic_type.clone()), "value"),
                unnamed(qualified("std", "size_t")),
                unnamed(encoding.clone()),
            ],
            vec![
                cpp::Statement::Declare {
                    type_: cpp::Type::named(wire("Writer")),
                    name: Ident::new("w"),
                    value: Some(cpp::Expr::Construct {
                        type_: Box::new(cpp::Type::named(wire("Writer"))),
                        args: vec![little.clone(), alignment.clone()],
                        braced: false,
                    }),
                },
                expr_statement(call(name(&enc), vec![name("w"), name("value")])),
                cpp::Statement::Return(Some(method(name("w"), "bytes", Vec::new()))),
            ],
            "The octets of the structure. The sample has no header (§7.8.2): it starts at \
             offset 0 and is PL_CDR2 whatever the encoding of the params.",
        );
        let serialize = static_method(
            octets.clone(),
            "serialize",
            vec![param(const_ref(topic_type.clone()), "value")],
            vec![cpp::Statement::Return(Some(call(
                name("serialize_at"),
                vec![
                    name("value"),
                    int(0),
                    path(&["dds", "rpc", "WireEncoding", "XCDR2"]),
                ],
            )))],
            "The octets of the structure on their own.",
        );
        let deserialize = |with_encoding: bool| {
            let mut params = vec![
                param(const_ref(octets.clone()), "in"),
                param(mut_ref(qualified("std", "size_t")), "at"),
                param(mut_ref(topic_type.clone()), "out"),
            ];
            let statements = if with_encoding {
                params.push(unnamed(encoding.clone()));
                vec![
                    cpp::Statement::Declare {
                        type_: cpp::Type::named(wire("Reader")),
                        name: Ident::new("r"),
                        value: Some(cpp::Expr::Construct {
                            type_: Box::new(cpp::Type::named(wire("Reader"))),
                            args: vec![name("in"), little.clone(), alignment.clone()],
                            braced: false,
                        }),
                    },
                    expr_statement(method(name("r"), "seek", vec![name("at")])),
                    expr_statement(call(name(&dec), vec![name("r"), name("out")])),
                    assign(name("at"), method(name("r"), "position", Vec::new())),
                    cpp::Statement::Return(Some(binary(
                        "==",
                        method(name("r"), "reader_status", Vec::new()),
                        status_ok(),
                    ))),
                ]
            } else {
                vec![cpp::Statement::Return(Some(call(
                    name("deserialize"),
                    vec![
                        name("in"),
                        name("at"),
                        name("out"),
                        path(&["dds", "rpc", "WireEncoding", "XCDR2"]),
                    ],
                )))]
            };
            static_method(
                cpp::Type::Primitive(cpp::Primitive::Bool),
                "deserialize",
                params,
                statements,
                "The structure from the octets at `at`.",
            )
        };
        let type_name_method = static_method(
            cpp::Type::Pointer(Box::new(cpp::Type::Const(Box::new(cpp::Type::Primitive(
                cpp::Primitive::Char,
            ))))),
            "type_name",
            Vec::new(),
            vec![cpp::Statement::Return(Some(cpp::Expr::Literal(
                Literal::Str(dds_name),
            )))],
            "The DDS type name, §7.5.1.2.4 and §7.5.1.2.5.",
        );
        let mut members = vec![
            type_name_method,
            serialize_at,
            serialize,
            deserialize(true),
            deserialize(false),
        ];
        if let Some(objects) = &objects {
            let topic = if reply {
                &objects.reply
            } else {
                &objects.request
            };
            let bytes = |octets: &Vec<u8>| cpp::Expr::Literal(Literal::Bytes(octets.clone()));
            members.push(static_method(
                cpp::Type::Named {
                    path: Path::new(&["std", "shared_ptr"]),
                    args: vec![cpp::Type::Const(Box::new(cpp::Type::named(Path::new(&[
                        "dds",
                        "rpc",
                        "TypeInformation",
                    ]))))],
                },
                "type_information",
                Vec::new(),
                vec![cpp::Statement::Return(Some(call(
                    path(&["dds", "rpc", "make_type_information"]),
                    vec![
                        bytes(&topic.object),
                        cpp::Expr::InitList(topic.dependencies.iter().map(bytes).collect()),
                    ],
                )))],
                "The XTypes description of the topic type: its CompleteTypeObject and the \
                 objects it names (DDS-RPC 1.0 §7.5.1.2, §7.7.2.5).",
            ));
        }
        if reply {
            // §7.5.1.2.5 rule 5: the code is the `remoteEx` member of the
            // reply, so the runtime asks the type for it.
            let remote_ex = structure
                .members
                .iter()
                .find(|member| member.type_ == EnhancedMemberType::RemoteException)
                .map(|member| cxx_escape(&member.name))
                .ok_or_else(|| {
                    BindingError::Codec("the reply structure has no remoteEx member".to_string())
                })?;
            members.push(static_method(
                qualified("dds::rpc", "RemoteExceptionCode_t"),
                "remote_exception",
                vec![param(const_ref(topic_type.clone()), "value")],
                vec![
                    cpp::Statement::If {
                        condition: dot(name("value"), &remote_ex),
                        then: block(vec![cpp::Statement::Return(Some(cpp::Expr::Deref(
                            Box::new(dot(name("value"), &remote_ex)),
                        )))]),
                        otherwise: None,
                    },
                    cpp::Statement::Return(Some(path(&["dds", "rpc", "REMOTE_EX_OK"]))),
                ],
                "The remote exception code the reply carries, REMOTE_EX_OK if none.",
            ));
            members.push(static_method(
                topic_type.clone(),
                "from_remote_exception",
                vec![param(
                    qualified("dds::rpc", "RemoteExceptionCode_t"),
                    "code",
                )],
                vec![
                    cpp::Statement::Declare {
                        type_: topic_type.clone(),
                        name: Ident::new("zd_reply"),
                        value: Some(zero_of(topic_type.clone())),
                    },
                    assign(dot(name("zd_reply"), &remote_ex), name("code")),
                    cpp::Statement::Return(Some(name("zd_reply"))),
                ],
                "A reply that carries `code` and nothing else.",
            ));
        }
        decls.push(cpp::Decl::Template(cpp::Template {
            doc: Doc::none(),
            params: Vec::new(),
            decl: Box::new(cpp::Decl::Class(cpp::Class {
                doc: Doc::line("The codec of one topic type. DDS-RPC 1.0 §7.11.1.4.22."),
                is_struct: true,
                name: Ident::new("dds_type_traits"),
                // The dependent types of §7.11.1.4.22, which a
                // specialisation does not inherit from the primary template.
                bases: vec![cpp::Base {
                    access: cpp::Access::Public,
                    virtual_: false,
                    path: Path::single(&format!("dds_type_traits_base<{type_name}>")),
                }],
                sections: vec![cpp::Section {
                    access: cpp::Access::Public,
                    members: members.into_iter().collect(),
                }],
            })),
            specialisation: vec![topic_type],
        }));
    }
    Ok(cpp::Decl::Namespace(cpp::Namespace {
        doc: Doc::none(),
        name: Ident::new("dds"),
        decls: vec![cpp::Decl::Namespace(cpp::Namespace {
            doc: Doc::none(),
            name: Ident::new("rpc"),
            decls,
        })],
    }))
}