Files
logos-cpp-sdk/cpp-generator/experimental/impl_header_parser.cpp
T
Dario LipicarandClaude Opus 4.8 7b62ac2017 Per-event documentation + getPluginEvents introspection (#71)
* Add per-event documentation + getPluginEvents introspection

Mirror the per-method documentation pipeline for events. Events
(declared in a universal module's logos_events: section) now carry a
description parsed from their /// doc comments, and are introspectable
at runtime via a new getPluginEvents framework call.

- lidl_ast: EventDecl gains a description field.
- impl_header_parser: capture the event's doc comment (previously
  discarded) and an optional metadata.json events[].description.
- lidl_gen_provider: generated universal provider emits
  getEvents() override, mirroring getMethods() (name/signature/
  parameters/description; no returnType/isInvokable — events are void).
- logos_provider_object: default-empty virtual getEvents() so the
  legacy provider path and QtProviderObject inherit empty.
- module_proxy / qt_provider_object: intercept getPluginEvents next to
  the getPluginMethods special-case.
- docs: spec + README event-documentation notes.

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

* Add unit tests for event documentation + getEvents generation

Address review feedback (#71): cover the event-introspection paths that
previously only had method-side tests.

- impl_header_parser test: assert metadata.json events[].description is
  parsed; new documented_events fixture asserts `///` doc-comment
  capture on a logos_events: block (multi-line joined with \n,
  adjacent-only, plain // ignored).
- lidl_gen_provider test: assert the generated dispatch contains
  getEvents() emitting each event's name/signature/parameters and an
  escaped description, and that events carry no returnType/isInvokable.

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

* Fold event introspection into getMethods() to keep the provider ABI stable

The previous approach added a getEvents() virtual to LogosProviderObject,
which inserted a new vtable slot and shifted every later slot — an ABI
break that would misdispatch virtual calls whenever an old and new
host/module were mixed across the in-process plugin boundary.

Instead, report events INSIDE the existing getMethods() call: it now
returns the module's whole interface, with each entry tagged
type "method" or "event" (events omit returnType/isInvokable). The
provider vtable is therefore byte-for-byte unchanged, so old/new hosts
and modules stay binary-compatible — a new host reading an old module
sees no event entries (zero events), and an old host reading a new
module just ignores the "type" field (cosmetic). An entry with no
"type" is treated as a method.

- logos_provider_object.h: remove the getEvents() virtual; document that
  getMethods() carries both, and why.
- generator (lidl_gen_provider): emit events as type "event" entries
  inside getMethods(); tag methods type "method"; no getEvents() output.
- module_proxy / qt_provider_object: getPluginMethods()/getPluginEvents()
  are now type-filtered views of getMethods(), plus a new
  getPluginInterface() returning the whole list. (These are name-
  dispatched Q_INVOKABLEs, not vtable surface — adding them is safe.)
- tests: generator asserts events fold into getMethods() tagged "event";
  ModuleProxy asserts the three filtered views; parser tests unchanged.
- docs: spec/project/docs/README updated, incl. an ABI rationale note.

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

---------

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

441 lines
17 KiB
C++

#include "impl_header_parser.h"
#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QRegularExpression>
#include <QStringList>
// ---------------------------------------------------------------------------
// C++ type string → LIDL TypeExpr
// ---------------------------------------------------------------------------
static TypeExpr cppTypeToLidl(const QString& raw)
{
// Normalize: strip const, &, leading/trailing whitespace
QString t = raw.trimmed();
t.remove(QRegularExpression("^const\\s+"));
t.remove(QRegularExpression("\\s*&$"));
t = t.trimmed();
// Primitives
if (t == "bool") return { TypeExpr::Primitive, "bool", {} };
if (t == "int64_t") return { TypeExpr::Primitive, "int", {} };
if (t == "uint64_t") return { TypeExpr::Primitive, "uint", {} };
if (t == "double") return { TypeExpr::Primitive, "float64", {} };
if (t == "void") return { TypeExpr::Primitive, "void", {} };
// std::string
if (t == "std::string")
return { TypeExpr::Primitive, "tstr", {} };
// std::vector<T>
static QRegularExpression vecRe("^std::vector\\s*<\\s*(.+)\\s*>$");
QRegularExpressionMatch m = vecRe.match(t);
if (m.hasMatch()) {
QString inner = m.captured(1).trimmed();
if (inner == "std::string") {
TypeExpr elem = { TypeExpr::Primitive, "tstr", {} };
return { TypeExpr::Array, "", { elem } };
}
if (inner == "uint8_t") {
return { TypeExpr::Primitive, "bstr", {} };
}
if (inner == "int64_t") {
TypeExpr elem = { TypeExpr::Primitive, "int", {} };
return { TypeExpr::Array, "", { elem } };
}
if (inner == "uint64_t") {
TypeExpr elem = { TypeExpr::Primitive, "uint", {} };
return { TypeExpr::Array, "", { elem } };
}
if (inner == "double") {
TypeExpr elem = { TypeExpr::Primitive, "float64", {} };
return { TypeExpr::Array, "", { elem } };
}
if (inner == "bool") {
TypeExpr elem = { TypeExpr::Primitive, "bool", {} };
return { TypeExpr::Array, "", { elem } };
}
}
// Qt collection types — pass through directly (non-std-convertible)
if (t == "QVariantMap")
return { TypeExpr::Map, "", { {TypeExpr::Primitive, "tstr", {}}, {TypeExpr::Primitive, "any", {}} } };
if (t == "QVariantList")
return { TypeExpr::Array, "", { {TypeExpr::Primitive, "any", {}} } };
if (t == "QStringList")
return { TypeExpr::Array, "", { {TypeExpr::Primitive, "tstr", {}} } };
// LogosMap / LogosList — nlohmann::json aliases; same LIDL shape as the Qt types
// but flagged so the generator emits an nlohmann→Qt conversion in the glue.
if (t == "LogosMap")
return { TypeExpr::Map, "", { {TypeExpr::Primitive, "tstr", {}}, {TypeExpr::Primitive, "any", {}} } };
if (t == "LogosList")
return { TypeExpr::Array, "", { {TypeExpr::Primitive, "any", {}} } };
// StdLogosResult — pure C++ result type for universal impls. The generator
// emits a StdLogosResult→Qt LogosResult conversion in the glue layer.
if (t == "StdLogosResult")
return { TypeExpr::Primitive, "result", {} };
// Fallback: treat as opaque
return { TypeExpr::Primitive, "any", {} };
}
// ---------------------------------------------------------------------------
// Parse a single method declaration line
// ---------------------------------------------------------------------------
static bool parseMethodLine(const QString& line, MethodDecl& out)
{
// Find the parameter list: everything between the last '(' and ')'
int parenOpen = -1;
int parenClose = -1;
int depth = 0;
for (int i = line.size() - 1; i >= 0; --i) {
if (line[i] == ')') {
if (parenClose < 0) parenClose = i;
depth++;
} else if (line[i] == '(') {
depth--;
if (depth == 0) {
parenOpen = i;
break;
}
}
}
if (parenOpen < 0 || parenClose < 0)
return false;
QString paramStr = line.mid(parenOpen + 1, parenClose - parenOpen - 1).trimmed();
// Everything before '(' is "returnType methodName"
QString prefix = line.left(parenOpen).trimmed();
// The method name is the last identifier token in prefix
int nameEnd = prefix.size();
while (nameEnd > 0 && prefix[nameEnd - 1].isSpace())
nameEnd--;
int nameStart = nameEnd;
while (nameStart > 0 && (prefix[nameStart - 1].isLetterOrNumber() || prefix[nameStart - 1] == '_'))
nameStart--;
if (nameStart >= nameEnd)
return false;
out.name = prefix.mid(nameStart, nameEnd - nameStart);
// Reject if the extracted name is a C++ keyword — this filters out
// member variable declarations like "std::function<void(...)> onEvent"
// where the parser would mistakenly extract "void" as the method name.
static const QSet<QString> cppKeywords = {
"void", "int", "bool", "char", "short", "long", "double", "float",
"auto", "return", "if", "else", "for", "while", "do", "switch",
"case", "break", "continue", "const", "static", "inline", "virtual"
};
if (cppKeywords.contains(out.name))
return false;
QString retTypeStr = prefix.left(nameStart).trimmed();
out.returnType = cppTypeToLidl(retTypeStr);
// Flag methods whose impl returns LogosMap / LogosList so the generator
// can emit nlohmann→Qt conversion code in the glue layer.
out.jsonReturn = (retTypeStr == "LogosMap" || retTypeStr == "LogosList");
// Flag methods whose impl returns StdLogosResult so the generator can
// emit a StdLogosResult→Qt LogosResult conversion in the glue layer.
out.resultReturn = (retTypeStr == "StdLogosResult");
// Parse parameters
out.params.clear();
if (!paramStr.isEmpty()) {
// Split by comma, respecting template depth
QStringList parts;
int start = 0;
int tdepth = 0;
for (int i = 0; i < paramStr.size(); ++i) {
if (paramStr[i] == '<') tdepth++;
else if (paramStr[i] == '>') tdepth--;
else if (paramStr[i] == ',' && tdepth == 0) {
parts.append(paramStr.mid(start, i - start).trimmed());
start = i + 1;
}
}
parts.append(paramStr.mid(start).trimmed());
for (const QString& part : parts) {
if (part.isEmpty()) continue;
QString p = part.trimmed();
int pNameEnd = p.size();
while (pNameEnd > 0 && p[pNameEnd - 1].isSpace())
pNameEnd--;
int pNameStart = pNameEnd;
while (pNameStart > 0 && (p[pNameStart - 1].isLetterOrNumber() || p[pNameStart - 1] == '_'))
pNameStart--;
if (pNameStart >= pNameEnd) continue;
ParamDecl pd;
pd.name = p.mid(pNameStart, pNameEnd - pNameStart);
pd.type = cppTypeToLidl(p.left(pNameStart));
out.params.append(pd);
}
}
return true;
}
// ---------------------------------------------------------------------------
// Main entry point
// ---------------------------------------------------------------------------
// Join doc-comment lines preserving line breaks (drop leading/trailing blanks).
static QString joinDocLines(QStringList lines)
{
while (!lines.isEmpty() && lines.first().trimmed().isEmpty()) lines.removeFirst();
while (!lines.isEmpty() && lines.last().trimmed().isEmpty()) lines.removeLast();
return lines.join('\n');
}
ImplParseResult parseImplHeader(const QString& headerPath,
const QString& className,
const QString& metadataPath,
QTextStream& err)
{
ImplParseResult result;
// --- Read metadata.json ---
{
QFile mf(metadataPath);
if (!mf.open(QIODevice::ReadOnly | QIODevice::Text)) {
result.error = "Failed to open metadata file: " + metadataPath;
return result;
}
QJsonParseError pe;
QJsonDocument doc = QJsonDocument::fromJson(mf.readAll(), &pe);
if (pe.error != QJsonParseError::NoError) {
result.error = "Failed to parse metadata JSON: " + pe.errorString();
return result;
}
QJsonObject obj = doc.object();
result.module.name = obj.value("name").toString();
result.module.version = obj.value("version").toString();
result.module.description = obj.value("description").toString();
result.module.category = obj.value("category").toString();
QJsonArray deps = obj.value("dependencies").toArray();
for (const QJsonValue& v : deps)
result.module.depends.append(v.toString());
// Read events declared in metadata.json
QJsonArray events = obj.value("events").toArray();
for (const QJsonValue& ev : events) {
QJsonObject evObj = ev.toObject();
EventDecl ed;
ed.name = evObj.value("name").toString();
ed.description = evObj.value("description").toString();
QJsonArray params = evObj.value("params").toArray();
for (const QJsonValue& pv : params) {
QJsonObject po = pv.toObject();
ParamDecl pd;
pd.name = po.value("name").toString();
pd.type = cppTypeToLidl(po.value("type").toString());
ed.params.append(pd);
}
if (!ed.name.isEmpty())
result.module.events.append(ed);
}
}
// --- Read and parse header ---
QFile hf(headerPath);
if (!hf.open(QIODevice::ReadOnly | QIODevice::Text)) {
result.error = "Failed to open header file: " + headerPath;
return result;
}
QString source = QString::fromUtf8(hf.readAll());
hf.close();
QStringList lines = source.split('\n');
// State machine: find "class <className>", then collect declarations.
// `InLogosEvents` is entered by the literal `logos_events:` token
// (mirrors Qt's `signals:`) — methods declared there are parsed as
// EventDecls and appended to ModuleDecl.events instead of .methods.
enum State { LookingForClass, InClass, InPublic, InPrivate, InLogosEvents };
State state = LookingForClass;
int braceDepth = 0;
// Accumulates doc-comment lines adjacent to a method so the doc comment
// becomes the method's description. Reset on any blank / non-comment line.
QStringList pendingDoc;
bool inBlockComment = false;
QRegularExpression classRe("\\bclass\\s+" + QRegularExpression::escape(className) + "\\b");
QRegularExpression accessRe("^\\s*(public|private|protected)\\s*:");
QRegularExpression eventsRe("^\\s*logos_events\\s*:");
QRegularExpression ctorDtorRe("^\\s*~?" + QRegularExpression::escape(className) + "\\s*\\(");
for (const QString& rawLine : lines) {
QString line = rawLine.trimmed();
switch (state) {
case LookingForClass:
if (classRe.match(line).hasMatch()) {
state = InClass;
for (QChar c : line) {
if (c == '{') braceDepth++;
else if (c == '}') braceDepth--;
}
}
break;
case InClass:
case InPublic:
case InPrivate:
case InLogosEvents:
// Inside a multi-line /** ... */ doc-comment block: capture its
// text (skip brace counting — comments don't affect scope).
if (inBlockComment) {
QString t = line;
int end = t.indexOf("*/");
if (end >= 0) { t = t.left(end); inBlockComment = false; }
t.remove(QRegularExpression(R"(^\*+\s?)"));
t = t.trimmed();
pendingDoc.append(t);
break;
}
// Count braces only on real code lines. Braces inside a doc/line
// comment (e.g. `/// returns { "k": v }`) must not affect scope
// tracking, or an unbalanced brace in a comment would make the
// parser think the class ended early and drop later declarations.
if (!(line.startsWith("//") || line.startsWith("/*") || line.startsWith("*"))) {
for (QChar c : line) {
if (c == '{') braceDepth++;
else if (c == '}') braceDepth--;
}
if (braceDepth <= 0) {
state = LookingForClass;
goto done;
}
}
// `logos_events:` takes precedence over the standard access
// specifiers: it's a separate section that the codegen pulls
// event prototypes from. (At preprocess time, `logos_events`
// expands to `public`, but the raw source still carries the
// token we recognise here.)
if (eventsRe.match(line).hasMatch()) {
state = InLogosEvents;
pendingDoc.clear();
break;
}
{
QRegularExpressionMatch am = accessRe.match(line);
if (am.hasMatch()) {
QString spec = am.captured(1);
if (spec == "public") state = InPublic;
else state = InPrivate;
pendingDoc.clear();
break;
}
}
// Only doc comments (/// or /** ... */ / /*! ... */) accumulate as
// the pending description for the next method. Plain // and /*
// comments are ignored but leave pending doc intact; blank /
// preprocessor lines reset it so only *adjacent* comments attach.
if (line.startsWith("///")) {
QString text = line.mid(3);
if (text.startsWith('<')) text = text.mid(1); // ///< trailing form
text = text.trimmed();
pendingDoc.append(text);
break;
}
if (line.startsWith("/**") || line.startsWith("/*!")) {
QString text = line.mid(3);
int end = text.indexOf("*/");
if (end >= 0) text = text.left(end);
else inBlockComment = true;
text.remove(QRegularExpression(R"(^\*+\s?)"));
text = text.trimmed();
pendingDoc.append(text);
break;
}
if (line.startsWith("//") || line.startsWith("/*") || line.startsWith("*")) {
break; // non-doc comment: ignore, keep pending doc
}
if (line.isEmpty() || line.startsWith("#")) {
pendingDoc.clear();
break;
}
if (ctorDtorRe.match(line).hasMatch()) {
pendingDoc.clear();
break;
}
if (line.startsWith("typedef") || line.startsWith("using")
|| line.startsWith("friend") || line.startsWith("enum")
|| line.startsWith("struct")) {
pendingDoc.clear();
break;
}
if (state == InLogosEvents) {
// Inside `logos_events:` — every bare prototype is an event.
// Events are always void-returning by definition, so we
// re-use parseMethodLine to extract name + params and
// discard the return type.
if (line.endsWith(';')) {
QString decl = line.left(line.size() - 1).trimmed();
MethodDecl md;
if (parseMethodLine(decl, md)) {
EventDecl ed;
ed.name = md.name;
ed.params = md.params;
ed.description = joinDocLines(pendingDoc);
result.module.events.append(ed);
}
}
pendingDoc.clear();
break;
}
if (state != InPublic) { pendingDoc.clear(); break; }
if (line.contains("std::function<")) {
// A std::function member is not a method — skip it so the
// `parseMethodLine` path below doesn't choke on the nested
// parens in its type. (Events are declared in a typed
// `logos_events:` section, parsed above — there is no longer
// any special `std::function emitEvent` member to detect.)
pendingDoc.clear();
break;
}
if (line.endsWith(';')) {
QString decl = line.left(line.size() - 1).trimmed();
MethodDecl md;
if (parseMethodLine(decl, md)) {
md.description = joinDocLines(pendingDoc);
result.module.methods.append(md);
}
}
pendingDoc.clear();
break;
}
}
done:
if (result.module.methods.isEmpty()) {
err << "Warning: no public methods found in class " << className
<< " in " << headerPath << "\n";
}
return result;
}