Skip to main content

Module c_mode

Module c_mode 

Source
Expand description

IDL → C99 codegen mode (vendor spec zerodds-xcdr2-c-1.0).

For each IDL specification, this module emits:

  • C99 typedef struct definitions per struct (with a module prefix in the type name, because C99 has no namespaces).
  • Static zerodds_typesupport_t tables with XCDR2 encoder/ decoder/key-hash/free function pointers.
  • Inline body implementations of the encoders/decoders that produce the XCDR2 wire format (XTypes 1.3 §7.4) byte-exactly.

§Scope

Supported:

  • Structs with @final/@appendable/@mutable extensibility.
  • Primitive types (boolean, octet, short/long/long long + unsigned, float, double).
  • string (unbounded).
  • sequence<T> (unbounded; nested sequences allowed).
  • Nested modules → type-name prefix with :: (cross-language convention) and identifier prefix with _ (C99-conformant).
  • @key members → key-hash routine via PlainCdr2BeKeyHolder.
  • @id(N) for @mutable.
  • Enumsint32_t alias + prefixed enumerator constants (Bug C).
  • Typedefs → resolved to the aliased type at every reference (Bug C).
  • Nested struct members → inline-embedded by value (Bug C).
  • Fixed arrays (T x[N][M]…) → C array, no length prefix (Bug C).
  • @optional members → <name>_present companion flag + a wire presence boolean (Bug C2; no longer flattened to mandatory).
  • Array bound by named constant (long v[N]) → the bound is resolved through the const symbol table (#43, const-array-bound).
  • sequence<T> for primitive, string, enum, nested struct and nested sequence<…> element types (#43, sequence widening).
  • map<K,V> → parallel keys[]/vals[] arrays + DHEADER/count codec (#43, map).
  • wstring → NUL-terminated uint16_t*, UTF-16-LE on the wire (XTypes §7.4.4.6) (#43, wstring).
  • Discriminated union → C tagged union struct { D _d; union {…} _u; } with a switch-on-discriminator encode/decode (#43, union). A top-level union additionally gets a TypeSupport so it can be a topic type.
  • typedef-to-aggregate (alias of a struct / sequence / map) → resolved to the aliased aggregate at every reference (#43, typedef-to-aggregate).
  • typedef ALIAS CHAIN + typedef-to-array (typedef A B; typedef long M[3][3];) → resolved through the chain to the root type; an array-alias contributes its dims at the use site (#43, typedefs).
  • bitmask → smallest holder uint typedef (default @bit_bound(32)uint32_t) + <LABEL> flag-bit constants; bitset → packed holder uint typedef + per-field SHIFT/MASK accessor macros. Both serialize as their holder integer (XTypes §7.3.1.2) (#43, bitset/bitmask).
  • Self-/mutually-recursive types (self-referential through a sequence, or mutual @external) → a heap-indirected C type (pointer-to-tag element) plus a runtime _write_body/_read_body helper that splices the recursion at RUNTIME (XTypes §7.4.5) (#43, recursion / forward-decl).
  • Forward-declared struct/union (then defined) → aggregate typedefs are emitted in by-value dependency order so a later definition is reachable.

Out of scope (errors at the codegen level, honest partial):

  • fixed<M,N> decimal, any, long double — these legitimately stay out of the C profile (no native C fixed-decimal / variant type).
  • A genuinely infinite-size type (a by-value self/mutual cycle, e.g. struct Node { Node n; };) is rejected cleanly — the only legal self-reference is heap-indirected (through a sequence).
  • @optional inside a nested struct, nested @appendable/@mutable struct splice (only the inline @final form is wired), nested-aggregate union arms inside a @mutable member, and freeing of heap-owning members reached only through a fixed array.

§Invocation

use zerodds_idl::config::ParserConfig;
use zerodds_idl_cpp::{generate_c_header, CGenOptions};

let ast = zerodds_idl::parse("@final struct Point { long x; long y; };",
                             &ParserConfig::default()).unwrap();
let header = generate_c_header(&ast, &CGenOptions::default()).unwrap();
assert!(header.contains("typedef struct Point_s"));
assert!(header.contains("Point_typesupport"));

Structs§

CGenOptions
Codegen options (parallel to CppGenOptions).

Functions§

generate_c_header
Produces a complete C99 header from an IDL specification.