aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-12-18 15:00:23 +0100
committerUlf Hermann <ulf.hermann@qt.io>2025-01-02 14:39:57 +0100
commitef7da0ea3ef71ac93a75cfcdb9401a6026ce1a1c (patch)
tree93a6fdc191c4fb97017e06e74d7cdf40089ee913
parent63a86bf71f5791d75c2e1ae9dffad5d694e44b58 (diff)
QmlCompiler: Drop some of the type shuffling on SetLookup
For primitive types we can use the register as-is. Also, explicitly name the type we are going to use to avoid integer range mismatches on coercion. Task-number: QTBUG-132345 Change-Id: I98d246b03e5194235246ee8e4ebcb0a8e0094a5b Reviewed-by: Olivier De Cannière <olivier.decanniere@qt.io> (cherry picked from commit 38d65db7aecee073099a5622ec3bc6e297041041) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> (cherry picked from commit c1231e6836c722f5707ff2cc02cd9b7b488ff6c9) Reviewed-by: Sami Shalayel <sami.shalayel@qt.io>
-rw-r--r--src/qmlcompiler/qqmljscodegenerator.cpp5
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt2
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/takenumber.cpp28
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/takenumber.h45
-rw-r--r--tests/auto/qml/qmlcppcodegen/data/takenumber.qml52
-rw-r--r--tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp69
6 files changed, 197 insertions, 4 deletions
diff --git a/src/qmlcompiler/qqmljscodegenerator.cpp b/src/qmlcompiler/qqmljscodegenerator.cpp
index 2a2caac465..c1df4d0f02 100644
--- a/src/qmlcompiler/qqmljscodegenerator.cpp
+++ b/src/qmlcompiler/qqmljscodegenerator.cpp
@@ -1593,8 +1593,7 @@ void QQmlJSCodeGenerator::generate_SetLookup(int index, int baseReg)
if (!m_typeResolver->equals(specific.storedType(), valueType)) {
if (m_typeResolver->isPrimitive(specific.storedType())
&& m_typeResolver->isPrimitive(valueType)) {
- // Preferably store in QJSPrimitiveValue since we need the content pointer below.
- property = property.storedIn(m_typeResolver->jsPrimitiveType());
+ // Nothing to do here. We can store all primitive types as is.
} else {
property = property.storedIn(m_typeResolver->merge(specific.storedType(), valueType));
}
@@ -1608,7 +1607,7 @@ void QQmlJSCodeGenerator::generate_SetLookup(int index, int baseReg)
QString argType;
if (!m_typeResolver->registerContains(
m_state.accumulatorIn(), m_typeResolver->containedType(property))) {
- m_body += u"auto converted = "_s
+ m_body += property.storedType()->augmentedInternalName() + u" converted = "_s
+ conversion(m_state.accumulatorIn(), property, consumedAccumulatorVariableIn())
+ u";\n"_s;
variableIn = contentPointer(property, u"converted"_s);
diff --git a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
index cba1bc3e3b..09092d7b08 100644
--- a/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
+++ b/tests/auto/qml/qmlcppcodegen/data/CMakeLists.txt
@@ -24,6 +24,7 @@ set(cpp_sources
sequenceToIterable.h
sequencetypeexample.cpp sequencetypeexample.h
state.h
+ takenumber.cpp takenumber.h
theme.cpp theme.h
timelinetheme.cpp timelinetheme.h
variantMapLookup.h
@@ -272,6 +273,7 @@ set(qml_files
stringLength.qml
stringToByteArray.qml
structuredValueType.qml
+ takenumber.qml
testlogger.js
text.qml
themerbad.qml
diff --git a/tests/auto/qml/qmlcppcodegen/data/takenumber.cpp b/tests/auto/qml/qmlcppcodegen/data/takenumber.cpp
new file mode 100644
index 0000000000..a018ebd04d
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/takenumber.cpp
@@ -0,0 +1,28 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include "takenumber.h"
+
+TakeNumber::TakeNumber(QObject *parent)
+ : QObject{parent}
+{}
+
+void TakeNumber::takeInt(int a)
+{
+ takenInt = a;
+}
+
+void TakeNumber::takeNegativeInt(int a)
+{
+ takenNegativeInt = a;
+}
+
+void TakeNumber::takeQSizeType(qsizetype a)
+{
+ takenQSizeType = a;
+}
+
+void TakeNumber::takeQLongLong(qlonglong a)
+{
+ takenQLongLong = a;
+}
diff --git a/tests/auto/qml/qmlcppcodegen/data/takenumber.h b/tests/auto/qml/qmlcppcodegen/data/takenumber.h
new file mode 100644
index 0000000000..2090a468d8
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/takenumber.h
@@ -0,0 +1,45 @@
+// Copyright (C) 2024 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#ifndef TAKENUMBER_H
+#define TAKENUMBER_H
+
+#include <QtCore/qobject.h>
+#include <QtQml/qqml.h>
+
+class TakeNumber : public QObject
+{
+ Q_OBJECT
+ QML_ELEMENT
+
+ Q_PROPERTY(int propertyInt MEMBER propertyInt NOTIFY propertyIntChanged)
+ Q_PROPERTY(int propertyNegativeInt MEMBER propertyNegativeInt NOTIFY propertyNegativeIntChanged)
+ Q_PROPERTY(qsizetype propertyQSizeType MEMBER propertyQSizeType NOTIFY propertyQSizeTypeChanged)
+ Q_PROPERTY(qlonglong propertyQLongLong MEMBER propertyQLongLong NOTIFY propertyQLongLongChanged)
+
+public:
+ explicit TakeNumber(QObject *parent = nullptr);
+
+ Q_INVOKABLE void takeInt(int a);
+ Q_INVOKABLE void takeNegativeInt(int a);
+ Q_INVOKABLE void takeQSizeType(qsizetype a);
+ Q_INVOKABLE void takeQLongLong(qlonglong a);
+
+ int takenInt = 0;
+ int takenNegativeInt = 0;
+ qsizetype takenQSizeType = 0;
+ qlonglong takenQLongLong = 0;
+
+ int propertyInt = 0;
+ int propertyNegativeInt = 0;
+ qsizetype propertyQSizeType = 0;
+ qsizetype propertyQLongLong = 0;
+
+signals:
+ void propertyIntChanged();
+ void propertyNegativeIntChanged();
+ void propertyQSizeTypeChanged();
+ void propertyQLongLongChanged();
+};
+
+#endif // TAKENUMBER_H
diff --git a/tests/auto/qml/qmlcppcodegen/data/takenumber.qml b/tests/auto/qml/qmlcppcodegen/data/takenumber.qml
new file mode 100644
index 0000000000..387ae25aa5
--- /dev/null
+++ b/tests/auto/qml/qmlcppcodegen/data/takenumber.qml
@@ -0,0 +1,52 @@
+pragma Strict
+import QtQml
+
+TakeNumber {
+ id: foo
+
+ function literal0() {
+ foo.takeInt(0)
+ foo.propertyInt = 0;
+ foo.takeNegativeInt(0)
+ foo.propertyNegativeInt = 0;
+ foo.takeQSizeType(0)
+ foo.propertyQSizeType = 0;
+ foo.takeQLongLong(0)
+ foo.propertyQLongLong = 0;
+ }
+
+ function literal56() {
+ foo.takeInt(56)
+ foo.propertyInt = 56;
+ foo.takeNegativeInt(-56)
+ foo.propertyNegativeInt = -56;
+ foo.takeQSizeType(56)
+ foo.propertyQSizeType = 56;
+ foo.takeQLongLong(56)
+ foo.propertyQLongLong = 56;
+ }
+
+ function variable0() {
+ var a = 0
+ foo.takeInt(a)
+ foo.propertyInt = a;
+ foo.takeNegativeInt(-a)
+ foo.propertyNegativeInt = -a;
+ foo.takeQSizeType(a)
+ foo.propertyQSizeType = a;
+ foo.takeQLongLong(a)
+ foo.propertyQLongLong = a;
+ }
+
+ function variable484() {
+ var a = 484
+ foo.takeInt(a)
+ foo.propertyInt = a;
+ foo.takeNegativeInt(-a)
+ foo.propertyNegativeInt = -a;
+ foo.takeQSizeType(a)
+ foo.propertyQSizeType = a;
+ foo.takeQLongLong(a)
+ foo.propertyQLongLong = a;
+ }
+}
diff --git a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
index 044e74e5df..28fa721afc 100644
--- a/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
+++ b/tests/auto/qml/qmlcppcodegen/tst_qmlcppcodegen.cpp
@@ -9,10 +9,11 @@
#include <data/getOptionalLookup.h>
#include <data/listprovider.h>
#include <data/objectwithmethod.h>
+#include <data/qmlusing.h>
#include <data/resettable.h>
+#include <data/takenumber.h>
#include <data/weathermoduleurl.h>
#include <data/withlength.h>
-#include <data/qmlusing.h>
#include <QtQml/private/qqmlengine_p.h>
#include <QtQml/private/qqmlpropertycachecreator_p.h>
@@ -236,6 +237,7 @@ private slots:
void stringLength();
void stringToByteArray();
void structuredValueType();
+ void takeNumbers();
void testIsnan();
void thisObject();
void throwObjectName();
@@ -4845,6 +4847,71 @@ void tst_QmlCppCodegen::structuredValueType()
QCOMPARE(o->property("w1").value<WeatherModelUrlDerived>(), w1);
}
+void tst_QmlCppCodegen::takeNumbers()
+{
+ QQmlEngine engine;
+ QQmlComponent c(&engine, QUrl(u"qrc:/qt/qml/TestTypes/takenumber.qml"_s));
+ QVERIFY2(c.isReady(), qPrintable(c.errorString()));
+ QScopedPointer<QObject> o(c.create());
+ QVERIFY(!o.isNull());
+
+ TakeNumber *takeNumber = qobject_cast<TakeNumber *>(o.data());
+ QVERIFY(takeNumber != nullptr);
+
+ QCOMPARE(takeNumber->takenInt, 0);
+ QCOMPARE(takeNumber->takenNegativeInt, 0);
+ QCOMPARE(takeNumber->takenQSizeType, 0);
+ QCOMPARE(takeNumber->takenQLongLong, 0);
+ QCOMPARE(takeNumber->propertyInt, 0);
+ QCOMPARE(takeNumber->propertyNegativeInt, 0);
+ QCOMPARE(takeNumber->propertyQSizeType, 0);
+ QCOMPARE(takeNumber->propertyQLongLong, 0);
+
+ o->metaObject()->invokeMethod(o.data(), "literal56");
+
+ QCOMPARE(takeNumber->takenInt, 56);
+ QCOMPARE(takeNumber->takenNegativeInt, -56);
+ QCOMPARE(takeNumber->takenQSizeType, 56);
+ QCOMPARE(takeNumber->takenQLongLong, 56);
+ QCOMPARE(takeNumber->propertyInt, 56);
+ QCOMPARE(takeNumber->propertyNegativeInt, -56);
+ QCOMPARE(takeNumber->propertyQSizeType, 56);
+ QCOMPARE(takeNumber->propertyQLongLong, 56);
+
+ o->metaObject()->invokeMethod(o.data(), "variable0");
+
+ QCOMPARE(takeNumber->takenInt, 0);
+ QCOMPARE(takeNumber->takenNegativeInt, 0);
+ QCOMPARE(takeNumber->takenQSizeType, 0);
+ QCOMPARE(takeNumber->takenQLongLong, 0);
+ QCOMPARE(takeNumber->propertyInt, 0);
+ QCOMPARE(takeNumber->propertyNegativeInt, 0);
+ QCOMPARE(takeNumber->propertyQSizeType, 0);
+ QCOMPARE(takeNumber->propertyQLongLong, 0);
+
+ o->metaObject()->invokeMethod(o.data(), "variable484");
+
+ QCOMPARE(takeNumber->takenInt, 484);
+ QCOMPARE(takeNumber->takenNegativeInt, -484);
+ QCOMPARE(takeNumber->takenQSizeType, 484);
+ QCOMPARE(takeNumber->takenQLongLong, 484);
+ QCOMPARE(takeNumber->propertyInt, 484);
+ QCOMPARE(takeNumber->propertyNegativeInt, -484);
+ QCOMPARE(takeNumber->propertyQSizeType, 484);
+ QCOMPARE(takeNumber->propertyQLongLong, 484);
+
+ o->metaObject()->invokeMethod(o.data(), "literal0");
+
+ QCOMPARE(takeNumber->takenInt, 0);
+ QCOMPARE(takeNumber->takenNegativeInt, 0);
+ QCOMPARE(takeNumber->takenQSizeType, 0);
+ QCOMPARE(takeNumber->takenQLongLong, 0);
+ QCOMPARE(takeNumber->propertyInt, 0);
+ QCOMPARE(takeNumber->propertyNegativeInt, 0);
+ QCOMPARE(takeNumber->propertyQSizeType, 0);
+ QCOMPARE(takeNumber->propertyQLongLong, 0);
+}
+
void tst_QmlCppCodegen::testIsnan()
{
QQmlEngine engine;