Files
logos-cpp-sdk/cpp-generator/experimental/lidl_gen_client.cpp
T
Dario LipicarandClaude Opus 4.8 f0fe8cbfeb Make the base SDK Qt-free: Qt developer layer moves to logos-qt-sdk (#83)
* Extract the protocol layer into logos-protocol; consume it as a flake input

The transport/token/IPC layer (transports incl. QRO + plain TCP/TLS,
consumer core LogosAPIClient/LogosAPIConsumer with the capability
auto-requestModule flow, ModuleProxy, token manager, QVariant<->JSON
conversion, the abstract LogosProviderObject interface) now lives in the
logos-protocol repo behind the versioned lp_* C ABI.

This SDK keeps the typed C++ developer layer (LogosAPI, provider base
classes + Qt provider glue, module context, code generator) and still
compiles the protocol sources INTO liblogos_sdk.a from the flake input,
so the installed artifact (archive symbols, include/ + include/cpp
layouts, cmake config) stays byte-compatible: existing consumers need
no changes. Public headers are unchanged; logos_provider_object.h keeps
its name and now re-exports the abstract interface from
logos_provider_interface.h.

Transport/protocol component tests moved to logos-protocol with the
code; the remaining sdk/generator/experimental suites are unchanged
(432/432 green against the local protocol checkout).

* lock: add logos-protocol input

* Make the base SDK Qt-free: move the Qt developer layer to logos-qt-sdk

LogosAPI, LogosAPIProvider, LogosProviderBase/LOGOS_PROVIDER macros, the
QObject provider glue (QtProviderObject) and the legacy PluginInterface
(core/interface.h) move to the new logos-qt-sdk repo. The protocol
sources are no longer compiled into a monolithic archive — consumers
link logos-qt-sdk (which layers on logos-protocol) instead.

What remains here is header-only std C++: logos_module_context.h,
logos_result.h (StdLogosResult), logos_json.h — exported as the CMake
INTERFACE target logos-cpp-sdk::logos_headers — plus the code generator
(a build-time tool; its introspection mode now includes
logos_provider_interface.h from logos-protocol, where
LogosProviderPlugin moved).

Mechanically verified Qt-free: the logos-cpp-lib / logos-cpp-include
closures contain only nlohmann_json. Tests: 245/245 (module-context std
suite + generator + experimental).

* fix: accept the installed source-export layout in the protocol-root check

The fail-fast only tested <root>/cpp/logos_protocol.h, but the LP_SRC
selection right below (and the error message itself) support the
installed export layout <root>/include/cpp as well. Pointing
LOGOS_PROTOCOL_ROOT at an installed export tripped the FATAL_ERROR
before that fallback could apply.

Caught by Copilot review on #82.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* lock: pin logos-protocol to the qt-free-split branch head

The Qt-free SDK (and the cdylib backend stacked on it) reference
LogosProviderPlugin from protocol's logos_provider_interface.h, which
lands on feat/qt-free-split — the P1-branch pin no longer compiles
standalone. Temporary — drop when the chain PRs merge.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* doctest: pin the logoscore runtime via its {release} placeholder

The spec built logoscore-cli at bare master with only the cpp-sdk inputs
overridden — master's stack cannot compile against the qt-free SDK, so
the suite failed on the chain branches. With the placeholder, CI's
--release-for pins expand it to the workspace's logoscore commit (and
local runs without a pin still fall back to master, unchanged).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* doctest: override the nested module builders to {release} too

capability_module (via logoscore's lock) and the cloned accounts module
resolve module-builder from their own locks — pre-split revs whose
LogosModule.cmake still detects the SDK by logos_api.h, which the
qt-free SDK no longer ships ('logos-cpp-sdk not found'). Overriding the
builder itself to the workspace-pinned chain rev (keeping the nested
cpp-sdk override) builds both modules with the split-aware builder.
Verified end-to-end locally with the exact doctest command.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* doctest: apply the {release} + nested-builder overrides to all three specs

The runtime spec got the treatment in 210eea1; the composition and
worker-thread specs have the same logoscore/module build commands and
failed identically (pre-split builders from the modules' own locks).
All executed run: blocks now pin logoscore-cli{release} and override
the nested module builders to logos-module-builder{release}; the
displayed code_block: variants stay in their generic master form.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* codegen: typed wrappers throw on call failure; dispatch catches escapes

Generated sync client wrappers call the new err-out invokeRemoteMethod
overload and throw logos::LogosCallError when the call fails (e.g. the
bound module is missing) — previously the empty QVariant silently
degraded to the return type's default and a caller could not tell
failure from a legitimate 0 / "". Both generators (legacy + LIDL),
both API styles. Async paths unchanged.

Generated provider dispatch (universal qt glue + LOGOS_PROVIDER) wraps
the method body in a catch-all that logs and returns an invalid QVariant
— an escaped exception becomes an ordinary METHOD_FAILED instead of
unwinding through Qt event dispatch and killing the module process.

* codegen: CallError out-param instead of throwing wrappers

Per review, the generated sync wrappers expose the error channel as an
optional trailing parameter — add(a, b, &err) — rather than throwing:
explicit, stateless, works on temporaries, and existing call sites
compile unchanged (they keep default-on-failure, now with a qWarning so
failures are visible in the module log). The dispatch catch-all from the
previous commit stays: it contains author exceptions, it doesn't
introduce any.

* glue: fire onContextReady AFTER modules()/event wiring

The generated onInit set the context (which fires the impl's
onContextReady hook) before constructing the LogosModules aggregate and
wiring typed event emission — so an impl doing its documented one-time
setup there (typed dependency calls, event subscriptions) dereferenced
a null aggregate and crashed the module process (signal 11). Found by
the first module to subscribe to a dependency's typed event from
onContextReady. Context now goes last.

* ci: run workflows on stacked PRs + workflow_dispatch

Both workflows filtered pull_request to master-based PRs, so stacked PRs
(feat/qt-free-sdk -> feat/extract-logos-protocol, feat/cdylib-authoring
-> feat/qt-free-sdk) ran NO checks at all. Drop the base-branch filter
for pull_request and add workflow_dispatch for manual runs. Same fix as
logos-module-builder 232b8a2.

* lock: protocol at the typed-requestModule port (3de5398)

* ci: chain pins for the doc-tests (drop at merge)

In repo CI only cpp-sdk's {release} is the commit under test —
logoscore-cli and module-builder expanded to master, which doesn't link
against the chain SDK the specs override in ('Build the CLI with the SDK
override' failed on every run since the stacked-PR triggers were
enabled). Pin both to the extraction-chain heads; the workspace pipeline
is unaffected (it pins every repo itself).

* generator: distribute the LIDL frontend for external generators

First step of moving ALL Qt glue emission out of this repo into
logos-qt-sdk's logos-qt-generator (cpp-sdk's generator keeps only the
Qt-free outputs: std typed wrappers, logos_sdk umbrella, cdylib
impl-exports, LIDL derivation).

- Shared emit helpers (lidlToPascalCase, lidlTypeToQt, lidlTypeToStd,
  lidlIsStdConvertible) move to a new lidl_emit_common.{h,cpp} unit, used
  by both generators.
- The frontend set (AST, lexer, parser, serializer, validator,
  impl-header parser, emit-common) is installed under
  share/lidl-frontend/ — the qt generator compiles these sources in
  directly, so the two tools share one frontend without a binary ABI.

* lock: protocol#3 merged — pin advances to protocol master

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 19:56:19 -03:00

364 lines
17 KiB
C++

#include "lidl_gen_client.h"
#include "lidl_emit_common.h"
#include "lidl_parser.h"
#include "lidl_validator.h"
#include <QFile>
#include <QDir>
#include <QFileInfo>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QTextStream>
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
static bool isRefType(const QString& qt)
{
return qt == "QString" || qt == "QStringList" || qt == "QJsonArray"
|| qt == "QVariantList" || qt == "QVariantMap" || qt == "QByteArray";
}
static void emitParam(QTextStream& s, const QString& qtType, const QString& name)
{
if (isRefType(qtType))
s << "const " << qtType << "& " << name;
else
s << qtType << " " << name;
}
static QString returnConversion(const QString& qt)
{
if (qt == "bool") return "return _result.toBool();";
if (qt == "int") return "return _result.toInt();";
if (qt == "double") return "return _result.toDouble();";
if (qt == "float") return "return _result.toFloat();";
if (qt == "QString") return "return _result.toString();";
if (qt == "QStringList") return "return _result.toStringList();";
if (qt == "QJsonArray") return "return qvariant_cast<QJsonArray>(_result);";
if (qt == "QVariantList") return "return _result.toList();";
if (qt == "QVariantMap") return "return _result.toMap();";
if (qt == "LogosResult") return "return _result.value<LogosResult>();";
return "return _result;";
}
static QString asyncDefaultVal(const QString& qt)
{
if (qt == "bool") return "false";
if (qt == "int" || qt == "double" || qt == "float") return "0";
if (qt == "QString") return "QString()";
if (qt == "QStringList") return "QStringList()";
if (qt == "QJsonArray") return "QJsonArray()";
if (qt == "QVariantList") return "QVariantList()";
if (qt == "QVariantMap") return "QVariantMap()";
return qt + "{}";
}
// ---------------------------------------------------------------------------
// Header generation
// ---------------------------------------------------------------------------
QString lidlMakeHeader(const ModuleDecl& module, BindMode bindMode)
{
QString className = lidlToPascalCase(module.name);
QString h;
QTextStream s(&h);
s << "#pragma once\n";
s << "#include <QString>\n";
s << "#include <QVariant>\n";
s << "#include <QStringList>\n";
s << "#include <QJsonArray>\n";
s << "#include <QVariantList>\n";
s << "#include <QVariantMap>\n";
s << "#include <functional>\n";
s << "#include <utility>\n";
s << "#include \"logos_types.h\"\n";
s << "#include \"logos_api.h\"\n";
s << "#include \"logos_api_client.h\"\n";
s << "#include \"logos_call_error.h\"\n";
s << "#include \"logos_object.h\"\n\n";
s << "class " << className << " {\n";
s << "public:\n";
if (bindMode == BindMode::Bound)
s << " explicit " << className << "(LogosAPI* api, const QString& moduleName);\n\n";
else
s << " explicit " << className << "(LogosAPI* api);\n\n";
s << " using RawEventCallback = std::function<void(const QString&, const QVariantList&)>;\n";
s << " using EventCallback = std::function<void(const QVariantList&)>;\n\n";
s << " bool on(const QString& eventName, RawEventCallback callback);\n";
s << " bool on(const QString& eventName, EventCallback callback);\n";
s << " void setEventSource(LogosObject* source);\n";
s << " LogosObject* eventSource() const;\n";
s << " void trigger(const QString& eventName);\n";
s << " void trigger(const QString& eventName, const QVariantList& data);\n";
s << " template<typename... Args>\n";
s << " void trigger(const QString& eventName, Args&&... args) {\n";
s << " trigger(eventName, packVariantList(std::forward<Args>(args)...));\n";
s << " }\n";
s << " void trigger(const QString& eventName, LogosObject* source, const QVariantList& data);\n";
s << " template<typename... Args>\n";
s << " void trigger(const QString& eventName, LogosObject* source, Args&&... args) {\n";
s << " trigger(eventName, source, packVariantList(std::forward<Args>(args)...));\n";
s << " }\n\n";
for (const MethodDecl& md : module.methods) {
QString ret = lidlTypeToQt(md.returnType);
s << " " << ret << " " << md.name << "(";
for (int i = 0; i < md.params.size(); ++i) {
emitParam(s, lidlTypeToQt(md.params[i].type), md.params[i].name);
if (i + 1 < md.params.size()) s << ", ";
}
// Optional error out-channel: pass a logos::CallError* to distinguish
// a failed remote call from a legitimately default-valued result.
if (!md.params.isEmpty()) s << ", ";
s << "logos::CallError* err = nullptr);\n";
QString asyncCb = (ret == "void")
? QString("std::function<void()>")
: QString("std::function<void(") + ret + ")>";
s << " void " << md.name << "Async(";
for (int i = 0; i < md.params.size(); ++i) {
emitParam(s, lidlTypeToQt(md.params[i].type), md.params[i].name);
if (i + 1 < md.params.size()) s << ", ";
}
if (!md.params.isEmpty()) s << ", ";
s << asyncCb << " callback, Timeout timeout = Timeout());\n";
}
s << "\nprivate:\n";
s << " LogosObject* ensureReplica();\n";
s << " template<typename... Args>\n";
s << " static QVariantList packVariantList(Args&&... args) {\n";
s << " QVariantList list;\n";
s << " list.reserve(sizeof...(Args));\n";
s << " using Expander = int[];\n";
s << " (void)Expander{0, (list.append(QVariant::fromValue(std::forward<Args>(args))), 0)...};\n";
s << " return list;\n";
s << " }\n";
s << " LogosAPI* m_api;\n";
s << " LogosAPIClient* m_client;\n";
s << " QString m_moduleName;\n";
s << " LogosObject* m_eventReplica = nullptr;\n";
s << " LogosObject* m_eventSource = nullptr;\n";
s << "};\n";
return h;
}
// ---------------------------------------------------------------------------
// Source generation
// ---------------------------------------------------------------------------
QString lidlMakeSource(const ModuleDecl& module, BindMode bindMode)
{
QString className = lidlToPascalCase(module.name);
QString headerRel = module.name + "_api.h";
QString c;
QTextStream s(&c);
s << "#include \"" << headerRel << "\"\n\n";
s << "#include <QDebug>\n\n";
// Target expression for every remote call: a baked literal in Static
// mode, the runtime m_moduleName member in Bound (interface) mode.
const QString targetExpr = (bindMode == BindMode::Bound)
? QStringLiteral("m_moduleName")
: (QStringLiteral("\"") + module.name + QStringLiteral("\""));
if (bindMode == BindMode::Bound)
s << className << "::" << className << "(LogosAPI* api, const QString& moduleName) : m_api(api), m_client(api->getClient(moduleName)), m_moduleName(moduleName) {}\n\n";
else
s << className << "::" << className << "(LogosAPI* api) : m_api(api), m_client(api->getClient(\""
<< module.name << "\")), m_moduleName(QStringLiteral(\"" << module.name << "\")) {}\n\n";
s << "LogosObject* " << className << "::ensureReplica() {\n";
s << " if (!m_eventReplica) {\n";
s << " LogosObject* replica = m_client->requestObject(m_moduleName);\n";
s << " if (!replica) {\n";
s << " qWarning() << \"" << className << ": failed to acquire remote object for events on\" << m_moduleName;\n";
s << " return nullptr;\n";
s << " }\n";
s << " m_eventReplica = replica;\n";
s << " }\n";
s << " return m_eventReplica;\n";
s << "}\n\n";
s << "bool " << className << "::on(const QString& eventName, RawEventCallback callback) {\n";
s << " if (!callback) { qWarning() << \"" << className << ": ignoring empty event callback for\" << eventName; return false; }\n";
s << " LogosObject* origin = ensureReplica();\n";
s << " if (!origin) return false;\n";
s << " m_client->onEvent(origin, eventName, callback);\n";
s << " return true;\n";
s << "}\n\n";
s << "bool " << className << "::on(const QString& eventName, EventCallback callback) {\n";
s << " if (!callback) { qWarning() << \"" << className << ": ignoring empty event callback for\" << eventName; return false; }\n";
s << " return on(eventName, [callback](const QString&, const QVariantList& data) { callback(data); });\n";
s << "}\n\n";
s << "void " << className << "::setEventSource(LogosObject* source) { m_eventSource = source; }\n\n";
s << "LogosObject* " << className << "::eventSource() const { return m_eventSource; }\n\n";
s << "void " << className << "::trigger(const QString& eventName) { trigger(eventName, QVariantList{}); }\n\n";
s << "void " << className << "::trigger(const QString& eventName, const QVariantList& data) {\n";
s << " if (!m_eventSource) { qWarning() << \"" << className << ": no event source set for trigger\" << eventName; return; }\n";
s << " m_client->onEventResponse(m_eventSource, eventName, data);\n";
s << "}\n\n";
s << "void " << className << "::trigger(const QString& eventName, LogosObject* source, const QVariantList& data) {\n";
s << " if (!source) { qWarning() << \"" << className << ": cannot trigger\" << eventName << \"with null source\"; return; }\n";
s << " m_client->onEventResponse(source, eventName, data);\n";
s << "}\n\n";
for (const MethodDecl& md : module.methods) {
QString ret = lidlTypeToQt(md.returnType);
int nParams = md.params.size();
s << ret << " " << className << "::" << md.name << "(";
for (int i = 0; i < nParams; ++i) {
emitParam(s, lidlTypeToQt(md.params[i].type), md.params[i].name);
if (i + 1 < nParams) s << ", ";
}
if (nParams > 0) s << ", ";
s << "logos::CallError* err) {\n";
// Call through the err-out overload: with a logos::CallError* the
// caller can distinguish a failed remote call from a legitimately
// default-valued result; without it the historical default-on-failure
// behavior is kept, plus a warning in the module log.
s << " logos::CallError _err;\n";
if (ret != "void") s << " QVariant _result = ";
else s << " ";
s << "m_client->invokeRemoteMethod(" << targetExpr << ", \"" << md.name << "\", QVariantList{";
for (int i = 0; i < nParams; ++i) {
s << md.params[i].name;
if (i + 1 < nParams) s << ", ";
}
s << "}, Timeout(), &_err);\n";
s << " if (err) *err = _err;\n";
s << " else if (!_err.ok()) qWarning() << \"" << className << "::" << md.name
<< ": remote call failed:\" << QString::fromStdString(_err.message);\n";
if (ret != "void")
s << " " << returnConversion(ret) << "\n";
s << "}\n\n";
s << "void " << className << "::" << md.name << "Async(";
for (int i = 0; i < nParams; ++i) {
emitParam(s, lidlTypeToQt(md.params[i].type), md.params[i].name);
if (i + 1 < nParams) s << ", ";
}
if (nParams > 0) s << ", ";
s << "std::function<void(" << (ret == "void" ? "void" : ret) << ")> callback, Timeout timeout) {\n";
s << " if (!callback) return;\n";
s << " m_client->invokeRemoteMethodAsync(" << targetExpr << ", \"" << md.name << "\", ";
if (nParams == 0) {
s << "QVariantList()";
} else if (nParams == 1) {
s << "QVariantList() << " << md.params[0].name;
} else {
s << "QVariantList{";
for (int i = 0; i < nParams; ++i) {
s << md.params[i].name;
if (i + 1 < nParams) s << ", ";
}
s << "}";
}
s << ", [callback](QVariant v) {\n";
if (ret == "void") {
s << " callback();\n";
} else if (ret == "QVariant") {
s << " callback(v);\n";
} else {
s << " callback(v.isValid() ? qvariant_cast<" << ret << ">(v) : " << asyncDefaultVal(ret) << ");\n";
}
s << " }, timeout);\n";
s << "}\n\n";
}
return c;
}
// ---------------------------------------------------------------------------
// metadata.json
// ---------------------------------------------------------------------------
QString lidlGenerateMetadataJson(const ModuleDecl& module)
{
QJsonObject obj;
obj["name"] = module.name;
obj["version"] = module.version.isEmpty() ? "0.0.0" : module.version;
obj["type"] = "core";
obj["category"] = module.category.isEmpty() ? "general" : module.category;
obj["description"] = module.description;
obj["main"] = module.name + "_plugin";
QJsonArray deps;
for (const QString& d : module.depends)
deps.append(d);
obj["dependencies"] = deps;
QJsonDocument doc(obj);
return doc.toJson(QJsonDocument::Indented);
}
// ---------------------------------------------------------------------------
// Full pipeline (from .lidl file)
// ---------------------------------------------------------------------------
int lidlGenerateClientStubs(const QString& lidlPath, const QString& outputDir,
bool moduleOnly, QTextStream& out, QTextStream& err)
{
QFileInfo fi(lidlPath);
if (!fi.exists()) { err << "LIDL file does not exist: " << lidlPath << "\n"; return 2; }
QFile file(fi.canonicalFilePath().isEmpty() ? fi.absoluteFilePath() : fi.canonicalFilePath());
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { err << "Failed to open LIDL file: " << lidlPath << "\n"; return 3; }
QString source = QString::fromUtf8(file.readAll());
file.close();
LidlParseResult pr = lidlParse(source);
if (pr.hasError()) { err << lidlPath << ":" << pr.errorLine << ":" << pr.errorColumn << ": " << pr.error << "\n"; return 4; }
LidlValidationResult vr = lidlValidate(pr.module);
if (vr.hasErrors()) { for (const QString& e : vr.errors) err << lidlPath << ": " << e << "\n"; return 5; }
const ModuleDecl& mod = pr.module;
QString genDirPath = outputDir.isEmpty() ? QDir::current().filePath("logos-cpp-sdk/cpp/generated") : outputDir;
QDir().mkpath(genDirPath);
QString headerAbs = QDir(genDirPath).filePath(mod.name + "_api.h");
QString sourceAbs = QDir(genDirPath).filePath(mod.name + "_api.cpp");
{ QFile f(headerAbs); if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { err << "Failed to write: " << headerAbs << "\n"; return 6; } f.write(lidlMakeHeader(mod).toUtf8()); }
{ QFile f(sourceAbs); if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { err << "Failed to write: " << sourceAbs << "\n"; return 7; } f.write(lidlMakeSource(mod).toUtf8()); }
{ QString metaPath = QDir(genDirPath).filePath("metadata.json"); QFile f(metaPath); if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { err << "Failed to write: " << metaPath << "\n"; return 8; } f.write(lidlGenerateMetadataJson(mod).toUtf8()); }
out << "Generated: " << headerAbs << " and " << sourceAbs << "\n";
if (!moduleOnly) {
QDir genDir(genDirPath);
QStringList headers = genDir.entryList(QStringList() << "*_api.h", QDir::Files | QDir::Readable);
{ QString content; QTextStream ss(&content);
ss << "#pragma once\n#include \"logos_api.h\"\n#include \"logos_api_client.h\"\n\n";
for (const QString& h : headers) ss << "#include \"" << h << "\"\n";
ss << "\nstruct LogosModules {\n explicit LogosModules(LogosAPI* api) : api(api)";
for (const QString& h : headers) { QString base = h; base.chop(6); ss << ", \n " << base << "(api)"; }
ss << " {}\n LogosAPI* api;\n";
for (const QString& h : headers) { QString base = h; base.chop(6); ss << " " << lidlToPascalCase(base) << " " << base << ";\n"; }
ss << "};\n";
QFile f(genDir.filePath("logos_sdk.h")); if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { err << "Failed to write umbrella\n"; return 9; } f.write(content.toUtf8()); }
{ QStringList sources = genDir.entryList(QStringList() << "*_api.cpp", QDir::Files | QDir::Readable);
QString content; QTextStream ss(&content); ss << "#include \"logos_sdk.h\"\n\n";
for (const QString& c : sources) ss << "#include \"" << c << "\"\n"; ss << "\n";
QFile f(genDir.filePath("logos_sdk.cpp")); if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { err << "Failed to write umbrella\n"; return 10; } f.write(content.toUtf8()); }
out << "Generated: logos_sdk.h and logos_sdk.cpp\n";
}
out << "Generated: metadata.json\n";
out.flush();
return 0;
}