Files
Dario Gabriel Lipicar 4d2bff1730 feat(optional): ?T is two-state, and the generators finally read it
No generator in any language read the optional flag — it had never been
implemented. `?T` was a HARD REJECT on the cdylib backend ("module not
cdylib-eligible"), `std::optional<T>` in an impl header fell through to the
opaque `any` with no diagnostic, and a `? name: T` field was emitted as a
required `T`. Three real contracts in the workspace already declare optionals
and were silently getting one of those three answers.

`?T` is TWO-state: a value of T, or empty. Never three — "one LIDL type <-> one
type per language" leaves nowhere for a third state, because every target has
exactly one empty inhabitant.

ONE MEANING, TWO SPELLINGS. `? name: T` (the field flag) and `name: ?T` (the
type kind) are the same declaration. Backends no longer answer that themselves:
logos-lidl's fieldIsOptional/fieldValueType are re-exported from lidl_compat.h
and every site THIS COMMIT TOUCHES reads them, so the two spellings emit
byte-identical code on the cdylib and client backends. That
caught a live drift on the way in — lidlRecordCollidesWithBytesTag read `f.type`
and so refused `? _bytes: tstr` while letting `_bytes: ?tstr` straight through,
one declaration with two answers.

THE WIRE RULE DEPENDS ON THE SLOT. Absent and explicit null are the SAME state
on decode and DIFFERENT on encode:
  - decode is liberal, by exactly one inhabitant: in an optional slot absent and
    null both mean empty; in a required slot both stay errors. A present value
    goes through the decoder a required T would get, so a wrong type still fails
    at the same path — optional widens the domain, it does not switch checking
    off. `?bstr` therefore keeps the LENIENT bytes decode a bare `bstr` gets,
    rather than silently becoming stricter in the optional slot.
  - encode has one canonical form: empty OMITS the key where the slot is NAMED
    (a record field) and is spelled null where it is POSITIONAL (an argument, a
    return, an event parameter — no key to omit, and arity must not change). Key
    omission lives in the record emitter because a Codec only ever sees a value,
    never the slot it sits in. A round trip therefore canonicalises.
  - `?any` collapses onto `any`: nlohmann::json already carries null, so
    std::optional<LogosMap> would give the slot two spellings of empty.

The dispatch gate now admits a missing trailing optional argument and
materialises it as null, exactly the way a missing record field already was. A
method with no optional parameter emits the byte-identical gate it always did.

Header-first: `std::optional<T>` <-> `?T`, composing with records and
containers. `std::optional<std::optional<T>>` has NO LIDL type (three C++ states
over a two-state wire), so it maps down to `?T` — which makes the author's own
declaration stop compiling against the generated codec, deliberately — and says
so at derivation time instead of leaving a conversion error in generated code.

The Qt/Lp consumer surface is NOT fixed and does not pretend to be. The wrappers
real modules get come from legacy/main.cpp, where the AST is flattened to a
single Qt type-name string per slot before optionality could be seen; Qt has no
optional metatype, so `?T` lands on QVariant — the right shape (an invalid
QVariant is Qt's empty inhabitant) with no type. The generator now prints a Note
naming every flattened slot so an affected build is never silent, and
docs/project.md records exactly what a Qt consumer will still do with an
optional field.

Verified by output equivalence, not by a green build: the generator was built
before and after and run over every .lidl in the workspace plus the impl-header
fixtures, in cdylib, consumer-qt, consumer-lp, client and header-first modes.
428 of 465 artefacts are byte-identical; all 37 that differ belong to one of the
four contracts that declare an optional (the 38th path is the manifest). The
harness's sensitivity is pinned by a negative control: qt vs lp output differs
in 45 files. The emitted codec was additionally compiled under -Wall -Wextra and
run against the rules above — omission, absent==null, required-still-rejects,
present-but-wrong-still-fails, and canonicalising round trip.

Tests: 199 pass, 0 fail (180 before, 19 new).

Requires logos-lidl's optionality accessors and logos-protocol's
Codec<std::optional<T>>.

NOT FIXED, AND IT IS THE PATH THAT MATTERS MOST. The legacy interface-wrapper
path is untouched, and it is the one every real module builds through
(buildPlugin.nix:145 -> logos-cpp-generator --general-only). There the two
spellings still diverge:

  ? maybe: tstr   ->  QString maybe{};   __m.value("maybe").toString()
  maybe: ?tstr    ->  QVariant maybe{};  __m.value("maybe")

and --api-style lp diverges too, neither side being std::optional. So R3 holds
on the backends below and NOT on the Qt consumer a shipping module actually
gets. logos-chat-module -- the contract that prompted this work -- uses the
field-flag spelling, so it lands on the branch that silently defaults.

The cause is upstream of codegen: legacy/main.cpp's moduleRecordsToJson and
moduleMethodsToJson flatten every TypeExpr to a single Qt TYPE-NAME STRING, so
optionality (along with nesting, map key types and descriptions) is gone before
generator_lib.cpp sees it. Widening that interface is a larger change and is
deliberately not attempted here. The only R3 test on a Qt surface covers
lidl_gen_client.cpp, which is on no live build path.
2026-07-31 00:56:22 -03:00

114 lines
4.8 KiB
C++

#ifndef LIDL_COMPAT_H
#define LIDL_COMPAT_H
// Bridge the cpp-generator's Qt-flavored codegen backends onto the canonical
// logos-lidl frontend. The lexer / parser / AST / serializer / validator now
// live in logos-lidl (std-typed, language-neutral); this header brings those
// types into the global scope the backends use unqualified and adds thin
// Qt-friendly shims so the existing emission code (QTextStream) keeps
// compiling. The backends themselves (impl-header parsing, gen_client,
// gen_cdylib) stay here — they are the C++/Qt-specific parts.
#include "lidl/ast.hpp"
#include "lidl/parser.hpp"
#include "lidl/serializer.hpp"
#include "lidl/validator.hpp"
#include <QString>
#include <QTextStream>
#include <string>
// The canonical AST, in the global scope the generator backends reference it
// from (they predate the logos-lidl extraction and use the unqualified names).
using lidl::TypeExpr;
using lidl::FieldDecl;
using lidl::ParamDecl;
using lidl::MethodDecl;
using lidl::EventDecl;
using lidl::TypeDecl;
using lidl::ModuleDecl;
// Optionality accessors, from the SAME header — never re-derived here.
//
// `?T` has two equivalent spellings for a record field: the flag (`? name: T`,
// which leaves `FieldDecl::type` as T and sets `FieldDecl::optional`) and the
// type kind (`name: ?T`, which leaves the flag false and makes the type an
// Optional). logos-lidl's docs/spec.md binds them to the same meaning, so they
// MUST emit identical code — and the only way that holds is if no backend
// answers the question itself. Reading `f.optional` alone is a bug; reading
// `f.type.kind == Optional` alone is the same bug from the other side.
// fieldIsOptional() / fieldValueType() are the answer.
using lidl::typeIsOptional;
using lidl::optionalValueType;
using lidl::fieldIsOptional;
using lidl::fieldValueType;
using lidl::paramIsOptional;
using lidl::paramValueType;
// std::string -> QString, and let QTextStream accept std::string directly so
// emission of AST string fields (`s << md.name`) keeps compiling unchanged.
inline QString qs(const std::string& s) { return QString::fromStdString(s); }
inline QTextStream& operator<<(QTextStream& s, const std::string& v)
{
return s << QString::fromStdString(v);
}
// Name-compatible shims over the canonical frontend so the call sites that used
// the deleted experimental lexer/parser/serializer/validator keep their shape.
using LidlParseResult = lidl::ParseResult;
using LidlValidationResult = lidl::ValidationResult;
inline lidl::ParseResult lidlParse(const QString& source)
{
return lidl::parse(source.toStdString());
}
inline QString lidlSerialize(const ModuleDecl& module)
{
return QString::fromStdString(lidl::serialize(module));
}
inline lidl::ValidationResult lidlValidate(const ModuleDecl& module)
{
return lidl::validate(module);
}
// A record whose ONLY field is a `tstr` named `_bytes` is indistinguishable on
// the wire from a canonical tagged byte string: `isTaggedBytes()` is checked
// BEFORE `is_object()` in both logos_codec.h and logos_json_convert.cpp, so
// such a record silently decodes as a byte string and the struct is gone. The
// ambiguity is inherent to the tagged form — the codec's own comment says not
// to name a map key `_bytes` — but a generator can at least refuse to emit the
// one shape that is guaranteed to misdecode, instead of leaving it to be
// discovered at runtime.
// Optionality does not rescue it: a PRESENT `? _bytes: tstr` still encodes to
// {"_bytes": "..."}, which is the ambiguous shape. So the check reads through
// the optional — via fieldValueType, not f.type — and refuses both spellings.
// Reading f.type here refused `? _bytes: tstr` (whose type stays Primitive
// tstr) while letting `_bytes: ?tstr` straight through: one declaration, two
// answers, which is the exact drift the accessors exist to prevent.
inline bool lidlRecordCollidesWithBytesTag(const TypeDecl& t)
{
if (t.fields.size() != 1 || t.fields[0].name != "_bytes")
return false;
const TypeExpr& vt = fieldValueType(t.fields[0]);
return vt.kind == TypeExpr::Primitive && vt.name == "tstr";
}
// Returns false and fills `error` when any declared record cannot round-trip.
inline bool lidlCheckRecords(const ModuleDecl& m, QString* error)
{
for (const TypeDecl& t : m.types) {
if (lidlRecordCollidesWithBytesTag(t)) {
if (error)
*error = QString("type '%1': a record whose only field is a tstr named "
"'_bytes' is wire-identical to a tagged byte string and "
"would decode as bytes, not as the record. Rename the "
"field or give the record another field.")
.arg(qs(t.name));
return false;
}
}
return true;
}
#endif // LIDL_COMPAT_H