From 5cc695b6fe36fd37e48f85161904f9db34518f17 Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Wed, 28 Oct 2020 13:46:47 +0100 Subject: QmlCompiler: Allow the specification of file scope code This can contain extra includes and code that should be added before any functions. Change-Id: Ida13d38ab7198c3986e134fe6f3786acd821927f Reviewed-by: Fabian Kosmale --- src/qmlcompiler/qqmljscompiler.cpp | 13 +++++++++---- src/qmlcompiler/qqmljscompiler_p.h | 4 +++- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src/qmlcompiler') diff --git a/src/qmlcompiler/qqmljscompiler.cpp b/src/qmlcompiler/qqmljscompiler.cpp index d508b290bd..938d1d68d6 100644 --- a/src/qmlcompiler/qqmljscompiler.cpp +++ b/src/qmlcompiler/qqmljscompiler.cpp @@ -40,6 +40,8 @@ Q_LOGGING_CATEGORY(lcAotCompiler, "qml.compiler.aot", QtWarningMsg); QT_BEGIN_NAMESPACE +static const int FileScopeCodeIndex = -1; + static QSet getIllegalNames() { QSet illegalNames; @@ -216,6 +218,8 @@ bool qCompileQmlFile(const QString &inputFileName, QQmlJSSaveFunction saveFuncti aotCompiler->setScopeObject(object); + aotFunctionsByIndex[FileScopeCodeIndex] = aotCompiler->globalCode(); + std::for_each(object->bindingsBegin(), object->bindingsEnd(), [&](const QmlIR::Binding &binding) { switch (binding.type) { @@ -381,10 +385,7 @@ bool qSaveQmlJSUnitAsCpp(const QString &inputFileName, const QString &outputFile return false; if (!aotFunctions.isEmpty()) { - QStringList includes = { - QStringLiteral("QtQml/qqmlcontext.h"), - QStringLiteral("type_traits") - }; + QStringList includes; for (const auto &function : aotFunctions) includes.append(function.includes); @@ -435,6 +436,7 @@ bool qSaveQmlJSUnitAsCpp(const QString &inputFileName, const QString &outputFile if (aotFunctions.isEmpty()) { writeStr("extern const QQmlPrivate::AOTCompiledFunction aotBuiltFunctions[] = { { 0, QMetaType::fromType(), nullptr } };"); } else { + writeStr(aotFunctions[FileScopeCodeIndex].code.toUtf8().constData()); writeStr(R"(template void wrapCall(QQmlContext *context, QObject *scopeObject, void *dataPtr, Binding &&binding) { using return_type = std::invoke_result_t; @@ -458,6 +460,9 @@ bool qSaveQmlJSUnitAsCpp(const QString &inputFileName, const QString &outputFile end = aotFunctions.constEnd(); func != end; ++func) { + if (func.key() == FileScopeCodeIndex) + continue; + QString function = header + func.value().code + footer; writeStr(QStringLiteral("{ %1, QMetaType::fromType<%2>(), %3 },") diff --git a/src/qmlcompiler/qqmljscompiler_p.h b/src/qmlcompiler/qqmljscompiler_p.h index f750fadbf4..6d40c28cc2 100644 --- a/src/qmlcompiler/qqmljscompiler_p.h +++ b/src/qmlcompiler/qqmljscompiler_p.h @@ -77,10 +77,12 @@ public: virtual void setScopeObject(const QmlIR::Object *object) = 0; virtual std::variant compileBinding( const QmlIR::Binding &binding) = 0; + + virtual QQmlJSAotFunction globalCode() const = 0; }; -using QQmlJSAotFunctionMap = QMap; +using QQmlJSAotFunctionMap = QMap; using QQmlJSSaveFunction = std::function; -- cgit v1.2.3