From 2130353ba487fd1e54af3815b11c43858ea807f7 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Wed, 27 Jul 2022 15:17:43 +0200 Subject: Move common code parts to a function in QmltcCodeGenerator Pick the patch back to 6.4 to allow follow up changes to be cherry-picked without conflicts Change-Id: If46739d09cbe448a812bb9b5b7d583947b39d3e1 Reviewed-by: Fabian Kosmale (cherry picked from commit 4de4b105b5e46a66ec09b27d937b84b3a8b4cead) Reviewed-by: Ulf Hermann --- tools/qmltc/qmltccompilerpieces.h | 105 +++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 53 deletions(-) (limited to 'tools') diff --git a/tools/qmltc/qmltccompilerpieces.h b/tools/qmltc/qmltccompilerpieces.h index 100fe8bdad..ebdd5d048c 100644 --- a/tools/qmltc/qmltccompilerpieces.h +++ b/tools/qmltc/qmltccompilerpieces.h @@ -34,6 +34,11 @@ struct QmltcCodeGenerator const QQmlJSScope::ConstPtr &type) const; inline void generate_initCodeForTopLevelComponent(QmltcType ¤t, const QQmlJSScope::ConstPtr &type); + + inline void generate_qmltcInstructionCallCode(QmltcMethod *function, + const QQmlJSScope::ConstPtr &type, + const QString &baseInstructionArgs, + const QString &childInstructionArgs) const; inline void generate_endInitCode(QmltcType ¤t, const QQmlJSScope::ConstPtr &type) const; inline void generate_interfaceCallCode(QmltcMethod *function, const QQmlJSScope::ConstPtr &type, @@ -307,6 +312,49 @@ QmltcCodeGenerator::generate_initCodeForTopLevelComponent(QmltcType ¤t, current.init.body << u"}"_s; } +/*! + \internal + + A generic helper function that generates special qmltc instruction code + boilerplate, adding it to a passed \a function. This is a building block + used to generate e.g. QML_endInit code. +*/ +inline void QmltcCodeGenerator::generate_qmltcInstructionCallCode( + QmltcMethod *function, const QQmlJSScope::ConstPtr &type, + const QString &baseInstructionArgs, const QString &childInstructionArgs) const +{ + using namespace Qt::StringLiterals; + + const bool isDocumentRoot = type == visitor->result(); + if (auto base = type->baseType(); base->isComposite()) { + function->body << u"// call base's method"_s; + Q_ASSERT(!isDocumentRoot || visitor->qmlTypesWithQmlBases()[0] == visitor->result()); + const auto isCurrentType = [&](const QQmlJSScope::ConstPtr &qmlType) { + return qmlType == type; + }; + const QString creationOffset = generate_typeCount(isCurrentType); + function->body << u"{"_s; + function->body << u"QQmltcObjectCreationHelper subCreator(creator, %1);"_s.arg( + creationOffset); + function->body << u"%1::%2(&subCreator, %3);"_s.arg(base->internalName(), function->name, + baseInstructionArgs); + function->body << u"}"_s; + } + + if (!isDocumentRoot) // document root does all the work here + return; + + const auto types = visitor->pureQmlTypes(); + function->body << u"// call children's methods"_s; + for (qsizetype i = 1; i < types.size(); ++i) { + const auto &type = types[i]; + Q_ASSERT(!type->isComponentRootElement()); + function->body << u"creator->get<%1>(%2)->%3(%4);"_s.arg( + type->internalName(), QString::number(i), function->name, childInstructionArgs); + } + function->body << u"// call own method code"_s; +} + /*! \internal @@ -330,20 +378,8 @@ inline void QmltcCodeGenerator::generate_endInitCode(QmltcType ¤t, if (isDocumentRoot) current.endInit.body << u"Q_UNUSED(canFinalize);"_s; - if (auto base = type->baseType(); base->isComposite()) { - current.endInit.body << u"// call base's finalize method"_s; - Q_ASSERT(!isDocumentRoot || visitor->qmlTypesWithQmlBases()[0] == visitor->result()); - const auto isCurrentType = [&](const QQmlJSScope::ConstPtr &qmlType) { - return qmlType == type; - }; - const QString creationOffset = generate_typeCount(isCurrentType); - current.endInit.body << u"{"_s; - current.endInit.body << u"QQmltcObjectCreationHelper subCreator(creator, %1);"_s.arg( - creationOffset); - current.endInit.body << u"%1::%2(&subCreator, engine, /* finalize */ false);"_s.arg( - base->internalName(), current.endInit.name); - current.endInit.body << u"}"_s; - } + generate_qmltcInstructionCallCode(¤t.endInit, type, u"engine, /* finalize */ false"_s, + u"creator, engine"_s); if (visitor->hasDeferredBindings(type)) { current.endInit.body << u"{ // defer bindings"_s; @@ -357,19 +393,6 @@ inline void QmltcCodeGenerator::generate_endInitCode(QmltcType ¤t, QmltcCodeGenerator::urlMethodName()); current.endInit.body << u"}"_s; } - - if (!isDocumentRoot) // document root does all the work here - return; - - const auto types = visitor->pureQmlTypes(); - current.endInit.body << u"// finalize children"_s; - for (qsizetype i = 1; i < types.size(); ++i) { - const auto &type = types[i]; - Q_ASSERT(!type->isComponentRootElement()); - current.endInit.body << u"creator->get<%1>(%2)->%3(creator, engine);"_s.arg( - type->internalName(), QString::number(i), current.endInit.name); - } - current.endInit.body << u"// finalize self"_s; } /*! @@ -504,32 +527,8 @@ QmltcCodeGenerator::generate_handleOnCompletedCode(QmltcType ¤t, if (isDocumentRoot) current.handleOnCompleted.body << u"Q_UNUSED(canFinalize);"_s; - if (auto base = type->baseType(); base->isComposite()) { - current.handleOnCompleted.body << u"// call base's method"_s; - Q_ASSERT(!isDocumentRoot || visitor->qmlTypesWithQmlBases()[0] == visitor->result()); - const auto isCurrentType = [&](const QQmlJSScope::ConstPtr &qmlType) { - return qmlType == type; - }; - const QString creationOffset = generate_typeCount(isCurrentType); - current.handleOnCompleted.body << u"{"_s; - current.handleOnCompleted.body - << u"QQmltcObjectCreationHelper subCreator(creator, %1);"_s.arg(creationOffset); - current.handleOnCompleted.body << u"%1::%2(&subCreator, /* finalize */ false);"_s.arg( - base->internalName(), current.handleOnCompleted.name); - current.handleOnCompleted.body << u"}"_s; - } - - if (!isDocumentRoot) // document root does all the work here - return; - - const auto types = visitor->pureQmlTypes(); - current.handleOnCompleted.body << u"// call children's methods"_s; - for (qsizetype i = 1; i < types.size(); ++i) { - const auto &type = types[i]; - Q_ASSERT(!type->isComponentRootElement()); - current.handleOnCompleted.body << u"creator->get<%1>(%2)->%3(creator);"_s.arg( - type->internalName(), QString::number(i), current.handleOnCompleted.name); - } + generate_qmltcInstructionCallCode(¤t.handleOnCompleted, type, u"/* finalize */ false"_s, + u"creator"_s); } /*! -- cgit v1.2.3