aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmltc
diff options
context:
space:
mode:
authorAndrei Golubev <andrei.golubev@qt.io>2021-12-17 15:52:17 +0100
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-12-23 09:27:03 +0000
commitceb872b603170293e95c5005d7ea8ec900511e01 (patch)
tree01430bc8e3209506423d330a3f43b89092b95431 /tools/qmltc
parentd5fa4a6bb7a1948cedbadceda6caa2ddfcf3db01 (diff)
Unify common logic around QQmlJSScope and friends
We have many things duplicated all over the place Change-Id: If929a5d683153781f6db630312240bf9c24ec777 Reviewed-by: Fawzi Mohamed <fawzi.mohamed@qt.io> (cherry picked from commit 1d1ad6cbc79ab5eb3fad5cb604f5d4f5da8f35cd) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tools/qmltc')
-rw-r--r--tools/qmltc/CMakeLists.txt1
-rw-r--r--tools/qmltc/prototype/codegenerator.cpp37
-rw-r--r--tools/qmltc/prototype/codegeneratorutil.h29
-rw-r--r--tools/qmltc/prototype/qml2cppdefaultpasses.cpp2
-rw-r--r--tools/qmltc/prototype/qml2cpppropertyutils.h8
-rw-r--r--tools/qmltc/qmltccompiler.cpp13
-rw-r--r--tools/qmltc/qmltccompilerpieces.h12
-rw-r--r--tools/qmltc/qmltccompilerutils.h66
-rw-r--r--tools/qmltc/qmltcpropertyutils.h13
9 files changed, 28 insertions, 153 deletions
diff --git a/tools/qmltc/CMakeLists.txt b/tools/qmltc/CMakeLists.txt
index b5ed382e3e..cf35319a3b 100644
--- a/tools/qmltc/CMakeLists.txt
+++ b/tools/qmltc/CMakeLists.txt
@@ -12,7 +12,6 @@ qt_internal_add_tool(${target_name}
qmltcvisitor.h qmltcvisitor.cpp
qmltccompiler.h qmltccompiler.cpp
qmltccompilerpieces.h
- qmltccompilerutils.h
qmltcpropertyutils.h
prototype/generatedcodeprimitives.h
diff --git a/tools/qmltc/prototype/codegenerator.cpp b/tools/qmltc/prototype/codegenerator.cpp
index 70fddf2fe8..ffe49c77ee 100644
--- a/tools/qmltc/prototype/codegenerator.cpp
+++ b/tools/qmltc/prototype/codegenerator.cpp
@@ -39,6 +39,8 @@
#include <QtCore/qloggingcategory.h>
+#include <private/qqmljsutils_p.h>
+
#include <optional>
#include <utility>
#include <numeric>
@@ -84,7 +86,7 @@ static QString figureReturnType(const QQmlJSMetaMethod &m)
if (isVoidMethod) {
type = u"void"_qs;
} else {
- type = CodeGeneratorUtility::getInternalNameAwareOfAccessSemantics(m.returnType());
+ type = m.returnType()->augmentedInternalName();
}
return type;
}
@@ -103,9 +105,8 @@ compileMethodParameters(const QStringList &names,
Q_ASSERT(allowUnnamed || !name.isEmpty()); // assume verified
if (name.isEmpty() && allowUnnamed)
name = u"unnamed_" + QString::number(i);
- paramList.emplaceBack(QQmlJSAotVariable {
- CodeGeneratorUtility::getInternalNameAwareOfAccessSemantics(types[i]), names[i],
- QString() });
+ paramList.emplaceBack(
+ QQmlJSAotVariable { types[i]->augmentedInternalName(), names[i], QString() });
}
return paramList;
}
@@ -809,9 +810,8 @@ void CodeGenerator::compileProperty(QQmlJSAotObject &current, const QQmlJSMetaPr
setter.returnType = u"void"_qs;
setter.name = compilationData.write;
// QQmlJSAotVariable
- setter.parameterList.emplaceBack(
- CodeGeneratorUtility::wrapInConstRefIfNotAPointer(underlyingType), name + u"_",
- u""_qs);
+ setter.parameterList.emplaceBack(QQmlJSUtils::constRefify(underlyingType), name + u"_",
+ u""_qs);
setter.body << variableName + u".setValue(" + name + u"_);";
setter.body << u"emit " + compilationData.notify + u"();";
current.functions.emplaceBack(setter);
@@ -873,7 +873,7 @@ void CodeGenerator::compileProperty(QQmlJSAotObject &current, const QQmlJSMetaPr
memberName + u"(QQmlListProperty<" + underlyingType + u">(this, std::addressof("
+ memberName + u"_storage)))");
underlyingType = u"QQmlListProperty<" + underlyingType + u">";
- } else if (isPointer(p)) {
+ } else if (p.type()->isReferenceType()) {
underlyingType += u'*';
}
@@ -895,8 +895,7 @@ void CodeGenerator::compileProperty(QQmlJSAotObject &current, const QQmlJSMetaPr
setter.returnType = u"void"_qs;
setter.name = compilationData.write;
// QQmlJSAotVariable
- setter.parameterList.emplaceBack(
- CodeGeneratorUtility::wrapInConstRefIfNotAPointer(underlyingType), inputName, u""_qs);
+ setter.parameterList.emplaceBack(QQmlJSUtils::constRefify(underlyingType), inputName, u""_qs);
setter.body << memberName + u".setValue(" + inputName + u"_);";
// TODO: Qt.binding() (and old bindings?) requires signal emission
setter.body << u"emit " + compilationData.notify + u"();";
@@ -1035,7 +1034,7 @@ void CodeGenerator::compileAlias(QQmlJSAotObject &current, const QQmlJSMetaPrope
info.underlyingType = resultingProperty.type()->internalName();
if (resultingProperty.isList()) {
info.underlyingType = u"QQmlListProperty<" + info.underlyingType + u">";
- } else if (isPointer(resultingProperty)) {
+ } else if (resultingProperty.type()->isReferenceType()) {
info.underlyingType += u"*"_qs;
}
// reset to generic type when having alias to id:
@@ -1080,9 +1079,8 @@ void CodeGenerator::compileAlias(QQmlJSAotObject &current, const QQmlJSMetaPrope
QList<QQmlJSMetaMethod> methods = type->methods(resultingProperty.write());
if (methods.isEmpty()) {
// QQmlJSAotVariable
- setter.parameterList.emplaceBack(
- CodeGeneratorUtility::wrapInConstRefIfNotAPointer(info.underlyingType),
- aliasName + u"_", u""_qs);
+ setter.parameterList.emplaceBack(QQmlJSUtils::constRefify(info.underlyingType),
+ aliasName + u"_", u""_qs);
} else {
setter.parameterList = compileMethodParameters(methods.at(0).parameterNames(),
methods.at(0).parameterTypes(), true);
@@ -1232,7 +1230,7 @@ void CodeGenerator::compileBinding(QQmlJSAotObject &current, const QmlIR::Bindin
}
case QmlIR::Binding::Type_String: {
const QString str = m_doc->stringAt(binding.stringIndex);
- addPropertyLine(propertyName, p, CodeGeneratorUtility::createStringLiteral(str));
+ addPropertyLine(propertyName, p, QQmlJSUtils::toLiteral(str));
break;
}
case QmlIR::Binding::Type_TranslationById: { // TODO: add test
@@ -1323,8 +1321,9 @@ void CodeGenerator::compileBinding(QQmlJSAotObject &current, const QmlIR::Bindin
if (!m_listReferencesCreated.contains(uniqueId)) {
m_listReferencesCreated.insert(uniqueId);
// TODO: figure if Unicode support is needed here
- current.endInit.body << u"QQmlListReference " + refName + u"(" + objectAddr
- + u", QByteArrayLiteral(\"" + propertyName + u"\"));";
+ current.endInit.body << u"QQmlListReference " + refName + u"(" + objectAddr + u", "
+ + QQmlJSUtils::toLiteral(propertyName, u"QByteArrayLiteral")
+ + u");";
current.endInit.body << u"Q_ASSERT(" + refName + u".canAppend());";
}
}
@@ -1641,8 +1640,8 @@ void CodeGenerator::compileScriptBinding(QQmlJSAotObject &current, const QmlIR::
if (!QmlIR::IRBuilder::isSignalPropertyName(propertyName))
return BindingKind::JustProperty;
- const auto offset = static_cast<uint>(strlen("on"));
- signalName = propertyName[offset].toLower() + propertyName.mid(offset + 1);
+ if (auto name = QQmlJSUtils::signalName(propertyName); name.has_value())
+ signalName = *name;
std::optional<QQmlJSMetaMethod> possibleSignal = signalByName(signalName);
if (possibleSignal) { // signal with signalName exists
diff --git a/tools/qmltc/prototype/codegeneratorutil.h b/tools/qmltc/prototype/codegeneratorutil.h
index 3730f29abd..d8b8defac8 100644
--- a/tools/qmltc/prototype/codegeneratorutil.h
+++ b/tools/qmltc/prototype/codegeneratorutil.h
@@ -62,35 +62,6 @@ struct CodeGeneratorUtility
return u"QUrl(QStringLiteral(\"qrc:" + s + u"\"))";
}
- static QString escapeString(QString s)
- {
- return s.replace(u"\\"_qs, u"\\\\"_qs)
- .replace(u"\""_qs, u"\\\""_qs)
- .replace(u"\n"_qs, u"\\n"_qs);
- }
-
- static QString createStringLiteral(const QString &s)
- {
- return u"QStringLiteral(\"" + escapeString(s) + u"\")";
- }
-
- static QString getInternalNameAwareOfAccessSemantics(const QQmlJSScope::ConstPtr &s)
- {
- // TODO: sequence is not supported yet
- Q_ASSERT(s->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence);
- const QString suffix = (s->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
- ? u" *"_qs
- : u""_qs;
- return s->internalName() + suffix;
- }
-
- static QString wrapInConstRefIfNotAPointer(QString s)
- {
- if (!s.endsWith(u'*'))
- s = u"const " + s + u"&";
- return s;
- }
-
static QString metaPropertyName(const QString &propertyVariableName)
{
return propertyVariableName + u"_meta";
diff --git a/tools/qmltc/prototype/qml2cppdefaultpasses.cpp b/tools/qmltc/prototype/qml2cppdefaultpasses.cpp
index b4be78de7e..37e95dc21e 100644
--- a/tools/qmltc/prototype/qml2cppdefaultpasses.cpp
+++ b/tools/qmltc/prototype/qml2cppdefaultpasses.cpp
@@ -188,7 +188,7 @@ void verifyTypes(const Qml2CppContext &context, QList<Qml2CppObject> &objects)
auto loc = findIrLocation(context.document, irObject->propertiesBegin(),
irObject->propertiesEnd(), name);
context.recordError(loc, u"Property '" + name + u"' of unknown type");
- if (property.isList() && !isPointer(property)) {
+ if (property.isList() && !property.type()->isReferenceType()) {
context.recordError(loc,
u"Property '" + name
+ u"' is a list type that contains non-pointer types");
diff --git a/tools/qmltc/prototype/qml2cpppropertyutils.h b/tools/qmltc/prototype/qml2cpppropertyutils.h
index f69e39dd83..4b3e513b90 100644
--- a/tools/qmltc/prototype/qml2cpppropertyutils.h
+++ b/tools/qmltc/prototype/qml2cpppropertyutils.h
@@ -31,18 +31,12 @@
#include <private/qqmljsmetatypes_p.h>
-inline bool isPointer(const QQmlJSMetaProperty &p)
-{
- Q_ASSERT(p.type());
- return p.type()->accessSemantics() == QQmlJSScope::AccessSemantics::Reference;
-}
-
inline QString getUnderlyingType(const QQmlJSMetaProperty &p)
{
QString underlyingType = p.type()->internalName();
if (p.isList()) {
underlyingType = u"QQmlListProperty<" + underlyingType + u">";
- } else if (isPointer(p)) {
+ } else if (p.type()->isReferenceType()) {
underlyingType += u"*"_qs;
}
return underlyingType;
diff --git a/tools/qmltc/qmltccompiler.cpp b/tools/qmltc/qmltccompiler.cpp
index 65029dbd32..6510a6d51d 100644
--- a/tools/qmltc/qmltccompiler.cpp
+++ b/tools/qmltc/qmltccompiler.cpp
@@ -29,11 +29,11 @@
#include "qmltccompiler.h"
#include "qmltcoutputir.h"
#include "qmltccodewriter.h"
-#include "qmltccompilerutils.h"
#include "qmltcpropertyutils.h"
#include "qmltccompilerpieces.h"
#include <QtCore/qloggingcategory.h>
+#include <private/qqmljsutils_p.h>
#include <algorithm>
@@ -284,7 +284,7 @@ compileMethodParameters(const QStringList &names,
Q_ASSERT(allowUnnamed || !name.isEmpty()); // assume verified
if (name.isEmpty() && allowUnnamed)
name = u"unnamed_" + QString::number(i);
- parameters.emplaceBack(augmentInternalName(types[i]), name, QString());
+ parameters.emplaceBack(types[i]->augmentedInternalName(), name, QString());
}
return parameters;
}
@@ -299,7 +299,7 @@ void QmltcCompiler::compileMethod(QmltcType &current, const QQmlJSMetaMethod &m)
if (isVoidMethod) {
type = u"void"_qs;
} else {
- type = augmentInternalName(m.returnType());
+ type = m.returnType()->augmentedInternalName();
}
return type;
};
@@ -370,7 +370,8 @@ void QmltcCompiler::compileProperty(QmltcType &current, const QQmlJSMetaProperty
setter.returnType = u"void"_qs;
setter.name = p.write();
// QQmlJSAotVariable
- setter.parameterList.emplaceBack(wrapInConstRef(underlyingType), name + u"_", u""_qs);
+ setter.parameterList.emplaceBack(QQmlJSUtils::constRefify(underlyingType), name + u"_",
+ u""_qs);
setter.body << variableName + u".setValue(" + name + u"_);";
current.functions.emplaceBack(setter);
mocPieces << u"WRITE"_qs << setter.name;
@@ -451,8 +452,8 @@ void QmltcCompiler::compileBinding(QmltcType &current, const QQmlJSMetaPropertyB
}
case QQmlJSMetaPropertyBinding::StringLiteral: {
const QString value = binding.literalValue().toString();
- generator.generate_assignToProperty(
- current, type, p, QmltcCodeGenerator::toStringLiteral(value), accessor.name);
+ generator.generate_assignToProperty(current, type, p, QQmlJSUtils::toLiteral(value),
+ accessor.name);
break;
}
case QQmlJSMetaPropertyBinding::Null: {
diff --git a/tools/qmltc/qmltccompilerpieces.h b/tools/qmltc/qmltccompilerpieces.h
index bdb732feb5..e0c587b5f3 100644
--- a/tools/qmltc/qmltccompilerpieces.h
+++ b/tools/qmltc/qmltccompilerpieces.h
@@ -51,18 +51,6 @@ struct QmltcCodeGenerator
QQmlJSScope::ConstPtr documentRoot;
- static QString escapeString(QString s)
- {
- return s.replace(u"\\"_qs, u"\\\\"_qs)
- .replace(u"\""_qs, u"\\\""_qs)
- .replace(u"\n"_qs, u"\\n"_qs);
- }
-
- static QString toStringLiteral(const QString &s)
- {
- return u"QStringLiteral(\"" + escapeString(s) + u"\")";
- }
-
/*!
\internal
diff --git a/tools/qmltc/qmltccompilerutils.h b/tools/qmltc/qmltccompilerutils.h
deleted file mode 100644
index 7af21532b9..0000000000
--- a/tools/qmltc/qmltccompilerutils.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2021 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the tools applications of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#ifndef QMLTCCOMPILERUTILS_H
-#define QMLTCCOMPILERUTILS_H
-
-#include <QtCore/qstring.h>
-#include <private/qqmljsscope_p.h>
-
-QT_BEGIN_NAMESPACE
-
-/*!
- \internal
-
- Wraps \a type into \c const and \c & if that is a "good" thing to do (e.g.
- the type is not a pointer type).
-*/
-inline QString wrapInConstRef(QString type)
-{
- if (!type.endsWith(u'*'))
- type = u"const " + type + u"&";
- return type;
-}
-
-/*!
- \internal
-
- Returns an internalName() of \a s, using the accessSemantics() to augment
- the result
-*/
-inline QString augmentInternalName(const QQmlJSScope::ConstPtr &s)
-{
- Q_ASSERT(s->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence);
- const QString suffix =
- (s->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) ? u" *"_qs : u""_qs;
- return s->internalName() + suffix;
-}
-
-QT_END_NAMESPACE
-
-#endif // QMLTCCOMPILERUTILS_H
diff --git a/tools/qmltc/qmltcpropertyutils.h b/tools/qmltc/qmltcpropertyutils.h
index caf031cfbc..4f8ea7fae6 100644
--- a/tools/qmltc/qmltcpropertyutils.h
+++ b/tools/qmltc/qmltcpropertyutils.h
@@ -36,17 +36,6 @@ QT_BEGIN_NAMESPACE
/*!
\internal
- Tells whether property \a p is a pointer type.
-*/
-inline bool isPointer(const QQmlJSMetaProperty &p)
-{
- Q_ASSERT(p.type());
- return p.type()->accessSemantics() == QQmlJSScope::AccessSemantics::Reference;
-}
-
-/*!
- \internal
-
Returns an underlying C++ type of \a p property.
*/
inline QString getUnderlyingType(const QQmlJSMetaProperty &p)
@@ -57,7 +46,7 @@ inline QString getUnderlyingType(const QQmlJSMetaProperty &p)
// would be both a list and a pointer (weird).
if (p.isList()) {
underlyingType = u"QQmlListProperty<" + underlyingType + u">";
- } else if (isPointer(p)) {
+ } else if (p.type()->isReferenceType()) {
underlyingType += u"*"_qs;
}
return underlyingType;