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

140 lines
6.4 KiB
C++

#include "lidl_emit_common.h"
QString lidlToPascalCase(const QString& name)
{
QString out;
bool cap = true;
for (QChar c : name) {
if (!c.isLetterOrNumber()) { cap = true; continue; }
if (cap) { out.append(c.toUpper()); cap = false; }
else { out.append(c.toLower()); }
}
if (out.isEmpty()) return QString("Module");
return out;
}
QString lidlTypeToQt(const TypeExpr& te)
{
switch (te.kind) {
case TypeExpr::Primitive:
if (te.name == "void") return "void";
if (te.name == "tstr") return "QString";
if (te.name == "bstr") return "QByteArray";
// 64-bit, and unsigned stays unsigned. LIDL int/uint are int64_t/uint64_t
// everywhere else (C++ impls, Rust's i64/u64), so spelling them `int`
// here broke the 1-1 mapping and truncated: a Qt consumer reading a
// `uint` return got a SIGNED 32-bit value. qlonglong/qulonglong rather
// than qint64/quint64 so the generated introspection matches the names
// Qt's own metaobject normalisation produces.
if (te.name == "int") return "qlonglong";
if (te.name == "uint") return "qulonglong";
if (te.name == "float64") return "double";
if (te.name == "bool") return "bool";
if (te.name == "result") return "LogosResult";
if (te.name == "any") return "QVariant";
return "QVariant";
case TypeExpr::Named:
// A record declared by the contract: its generated struct. One LIDL
// type, one type per language — a record is not a QVariant blob.
return QString::fromStdString(te.name);
case TypeExpr::Array:
if (te.elements.size() == 1
&& te.elements[0].kind == TypeExpr::Primitive
&& te.elements[0].name == "tstr") {
return "QStringList";
}
// A list of records is a typed list: QVariantList could not hold a
// record without Q_DECLARE_METATYPE, and the point of a record is that
// the consumer gets the struct.
if (te.elements.size() == 1 && te.elements[0].kind == TypeExpr::Named)
return "QList<" + QString::fromStdString(te.elements[0].name) + ">";
return "QVariantList";
case TypeExpr::Map:
if (te.elements.size() == 2 && te.elements[1].kind == TypeExpr::Named)
return "QMap<QString, " + QString::fromStdString(te.elements[1].name) + ">";
return "QVariantMap";
case TypeExpr::Optional:
// `?T` on the QT surface, deliberately, and this is the one mapping in
// this table that LOSES the value type.
//
// Qt has no optional template, and the type this name is read for is a
// metatype: the legacy consumer path and the cdylib's getMethods()
// introspection both hand it to the host, which marshals a QVariant
// across the plugin boundary. There is no metatype called
// `std::optional<QString>` — emitting one would fail exactly the way
// emitting a record's struct name here once made the host SIGSEGV.
//
// QVariant is at least the RIGHT SHAPE: an invalid QVariant is Qt's
// single empty inhabitant, and the wire's `null` becomes precisely
// that. So `?T` is two-state here — it is just untyped, in the same way
// `any` is, which means a Qt consumer gets no compile-time check on the
// value and cannot tell `?tstr` from `?uint`. That is a real gap, not a
// finished mapping; see cpp-generator/docs/project.md ("Optionality").
return "QVariant";
}
return "QVariant";
}
bool lidlIsStdConvertible(const TypeExpr& te)
{
if (te.kind == TypeExpr::Primitive) {
return te.name == "tstr" || te.name == "bstr"
|| te.name == "int" || te.name == "uint"
|| te.name == "float64" || te.name == "bool";
}
if (te.kind == TypeExpr::Array && te.elements.size() == 1) {
const TypeExpr& elem = te.elements[0];
if (elem.kind == TypeExpr::Primitive) {
return elem.name == "tstr" || elem.name == "bstr"
|| elem.name == "int" || elem.name == "uint"
|| elem.name == "float64" || elem.name == "bool";
}
}
return false;
}
QString lidlTypeToStd(const TypeExpr& te)
{
if (te.kind == TypeExpr::Primitive) {
if (te.name == "tstr") return "std::string";
if (te.name == "bstr") return "std::vector<uint8_t>";
if (te.name == "int") return "int64_t";
if (te.name == "uint") return "uint64_t";
if (te.name == "float64") return "double";
if (te.name == "bool") return "bool";
if (te.name == "result") return "LogosResult";
if (te.name == "any") return "QVariant";
return "QVariant";
}
if (te.kind == TypeExpr::Array && te.elements.size() == 1) {
const TypeExpr& elem = te.elements[0];
if (elem.kind == TypeExpr::Primitive) {
if (elem.name == "tstr") return "std::vector<std::string>";
if (elem.name == "bstr") return "std::vector<std::vector<uint8_t>>";
if (elem.name == "int") return "std::vector<int64_t>";
if (elem.name == "uint") return "std::vector<uint64_t>";
if (elem.name == "float64") return "std::vector<double>";
if (elem.name == "bool") return "std::vector<bool>";
}
return "QVariantList";
}
if (te.kind == TypeExpr::Map) return "QVariantMap";
// `?T` -> std::optional<T>. The std surface HAS an optional, so unlike the
// Qt table above this one keeps the value type. std::nullopt is C++'s single
// empty inhabitant, which is what makes the mapping two-state; the encoder
// that pairs with it is logos-protocol's Codec<std::optional<T>>.
//
// Recurse through optionalValueType() rather than elements[0]: optionality
// is idempotent under the two-state rule, so `??T` denotes the same two
// states as `?T` and must not become std::optional<std::optional<T>>.
// A degenerate Optional carrying no element (unreachable from the parser,
// constructible by hand or over the JSON bridge) keeps the opaque fallback
// instead of recursing forever.
if (te.kind == TypeExpr::Optional) {
if (te.elements.empty()) return "QVariant";
return "std::optional<" + lidlTypeToStd(optionalValueType(te)) + ">";
}
if (te.kind == TypeExpr::Named) return "QVariant";
return "QVariant";
}