1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qqmlpropertycachecreator_p.h"
41
42#include <private/qqmlengine_p.h>
43
44QT_BEGIN_NAMESPACE
45
46QAtomicInt QQmlPropertyCacheCreatorBase::classIndexCounter(0);
47
48
49int QQmlPropertyCacheCreatorBase::metaTypeForPropertyType(QV4::CompiledData::BuiltinType type)
50{
51 switch (type) {
52 case QV4::CompiledData::BuiltinType::Var: return QMetaType::QVariant;
53 case QV4::CompiledData::BuiltinType::Variant: return QMetaType::QVariant;
54 case QV4::CompiledData::BuiltinType::Int: return QMetaType::Int;
55 case QV4::CompiledData::BuiltinType::Bool: return QMetaType::Bool;
56 case QV4::CompiledData::BuiltinType::Real: return QMetaType::Double;
57 case QV4::CompiledData::BuiltinType::String: return QMetaType::QString;
58 case QV4::CompiledData::BuiltinType::Url: return QMetaType::QUrl;
59 case QV4::CompiledData::BuiltinType::Color: return QMetaType::QColor;
60 case QV4::CompiledData::BuiltinType::Font: return QMetaType::QFont;
61 case QV4::CompiledData::BuiltinType::Time: return QMetaType::QTime;
62 case QV4::CompiledData::BuiltinType::Date: return QMetaType::QDate;
63 case QV4::CompiledData::BuiltinType::DateTime: return QMetaType::QDateTime;
64 case QV4::CompiledData::BuiltinType::Rect: return QMetaType::QRectF;
65 case QV4::CompiledData::BuiltinType::Point: return QMetaType::QPointF;
66 case QV4::CompiledData::BuiltinType::Size: return QMetaType::QSizeF;
67 case QV4::CompiledData::BuiltinType::Vector2D: return QMetaType::QVector2D;
68 case QV4::CompiledData::BuiltinType::Vector3D: return QMetaType::QVector3D;
69 case QV4::CompiledData::BuiltinType::Vector4D: return QMetaType::QVector4D;
70 case QV4::CompiledData::BuiltinType::Matrix4x4: return QMetaType::QMatrix4x4;
71 case QV4::CompiledData::BuiltinType::Quaternion: return QMetaType::QQuaternion;
72 case QV4::CompiledData::BuiltinType::InvalidBuiltin: break;
73 };
74 return QMetaType::UnknownType;
75}
76
77QByteArray QQmlPropertyCacheCreatorBase::createClassNameTypeByUrl(const QUrl &url)
78{
79 const QString path = url.path();
80 int lastSlash = path.lastIndexOf(c: QLatin1Char('/'));
81 // Not a reusable type if we don't have an absolute Url
82 if (lastSlash <= -1)
83 return QByteArray();
84 // ### this might not be correct for .ui.qml files
85 const QStringRef nameBase = path.midRef(position: lastSlash + 1, n: path.length() - lastSlash - 5);
86 // Not a reusable type if it doesn't start with a upper case letter.
87 if (nameBase.isEmpty() || !nameBase.at(i: 0).isUpper())
88 return QByteArray();
89 return nameBase.toUtf8() + "_QMLTYPE_" +
90 QByteArray::number(classIndexCounter.fetchAndAddRelaxed(valueToAdd: 1));
91}
92
93QByteArray QQmlPropertyCacheCreatorBase::createClassNameForInlineComponent(const QUrl &baseUrl, int icId)
94{
95 QByteArray baseName = createClassNameTypeByUrl(url: baseUrl);
96 if (baseName.isEmpty())
97 baseName = QByteArray("ANON_QML_IC_") + QByteArray::number(classIndexCounter.fetchAndAddRelaxed(valueToAdd: 1));
98 baseName += "_" + QByteArray::number(icId);
99 return baseName;
100}
101
102QQmlBindingInstantiationContext::QQmlBindingInstantiationContext(int referencingObjectIndex, const QV4::CompiledData::Binding *instantiatingBinding,
103 const QString &instantiatingPropertyName, QQmlPropertyCache *referencingObjectPropertyCache)
104 : referencingObjectIndex(referencingObjectIndex)
105 , instantiatingBinding(instantiatingBinding)
106 , instantiatingPropertyName(instantiatingPropertyName)
107 , referencingObjectPropertyCache(referencingObjectPropertyCache)
108{
109}
110
111bool QQmlBindingInstantiationContext::resolveInstantiatingProperty()
112{
113 if (!instantiatingBinding || instantiatingBinding->type != QV4::CompiledData::Binding::Type_GroupProperty)
114 return true;
115
116 Q_ASSERT(referencingObjectIndex >= 0);
117 Q_ASSERT(referencingObjectPropertyCache);
118 Q_ASSERT(instantiatingBinding->propertyNameIndex != 0);
119
120 bool notInRevision = false;
121 instantiatingProperty = QQmlPropertyResolver(referencingObjectPropertyCache)
122 .property(name: instantiatingPropertyName, notInRevision: &notInRevision,
123 check: QQmlPropertyResolver::IgnoreRevision);
124 return instantiatingProperty != nullptr;
125}
126
127QQmlRefPointer<QQmlPropertyCache> QQmlBindingInstantiationContext::instantiatingPropertyCache(QQmlEnginePrivate *enginePrivate) const
128{
129 if (instantiatingProperty) {
130 if (instantiatingProperty->isQObject()) {
131 return enginePrivate->rawPropertyCacheForType(instantiatingProperty->propType(), minorVersion: instantiatingProperty->typeMinorVersion());
132 } else if (const QMetaObject *vtmo = QQmlValueTypeFactory::metaObjectForMetaType(type: instantiatingProperty->propType())) {
133 return enginePrivate->cache(metaObject: vtmo, minorVersion: instantiatingProperty->typeMinorVersion());
134 }
135 }
136 return QQmlRefPointer<QQmlPropertyCache>();
137}
138
139void QQmlPendingGroupPropertyBindings::resolveMissingPropertyCaches(QQmlEnginePrivate *enginePrivate, QQmlPropertyCacheVector *propertyCaches) const
140{
141 for (QQmlBindingInstantiationContext pendingBinding: *this) {
142 const int groupPropertyObjectIndex = pendingBinding.instantiatingBinding->value.objectIndex;
143
144 if (propertyCaches->at(index: groupPropertyObjectIndex))
145 continue;
146
147 if (!pendingBinding.resolveInstantiatingProperty())
148 continue;
149
150 auto cache = pendingBinding.instantiatingPropertyCache(enginePrivate);
151 propertyCaches->set(index: groupPropertyObjectIndex, replacement: cache);
152 }
153}
154
155QT_END_NAMESPACE
156

source code of qtdeclarative/src/qml/qml/qqmlpropertycachecreator.cpp