Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmltype_p_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLTYPE_P_P_H
6#define QQMLTYPE_P_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmlengine_p.h>
20#include <private/qqmlmetatype_p.h>
21#include <private/qqmlpropertycache_p.h>
22#include <private/qqmlproxymetaobject_p.h>
23#include <private/qqmlrefcount_p.h>
24#include <private/qqmltype_p.h>
25#include <private/qqmltypeloader_p.h>
26#include <private/qstringhash_p.h>
27#include <private/qv4engine_p.h>
28#include <private/qv4executablecompilationunit_p.h>
29#include <private/qv4resolvedtypereference_p.h>
30
31#include <QAtomicInteger>
32#include <QtCore/qtyperevision.h>
33
34QT_BEGIN_NAMESPACE
35
36class QQmlTypePrivate final : public QQmlRefCounted<QQmlTypePrivate>
37{
38 Q_DISABLE_COPY_MOVE(QQmlTypePrivate)
39public:
40 struct ProxyMetaObjects
41 {
42 ~ProxyMetaObjects()
43 {
44 for (const QQmlProxyMetaObject::ProxyData &metaObject : std::as_const(data))
45 free(metaObject.metaObject);
46 }
47
48 QList<QQmlProxyMetaObject::ProxyData> data;
49 bool containsRevisionedAttributes = false;
50 };
51
52 struct Enums
53 {
54 enum Scoping { Scoped, Unscoped };
55 ~Enums()
56 {
57 qDeleteAll(scopedEnums);
58 qDeleteAll(unscopedEnums);
59 }
60
61 QStringHash<int> enums;
62 QStringHash<int> scopedEnumIndex; // maps from enum name to index in scopedEnums
63 QStringHash<int> unscopedEnumIndex; // maps from enum name to index in unscopedEnums
64 QList<QStringHash<int> *> scopedEnums;
65 QList<QStringHash<int> *> unscopedEnums;
66 };
67
68 QQmlTypePrivate(QQmlType::RegistrationType type);
69
70 const ProxyMetaObjects *init() const;
71
72 QUrl sourceUrl() const
73 {
74 switch (regType) {
75 case QQmlType::CompositeType:
76 return extraData.compositeTypeData;
77 case QQmlType::CompositeSingletonType:
78 return extraData.singletonTypeData->singletonInstanceInfo->url;
79 case QQmlType::InlineComponentType:
80 return extraData.inlineComponentTypeData;
81 case QQmlType::JavaScriptType:
82 return extraData.javaScriptTypeData;
83 default:
84 return QUrl();
85 }
86 }
87
88 const QQmlTypePrivate *attachedPropertiesBase(QQmlTypeLoader *typeLoader) const
89 {
90 for (const QQmlTypePrivate *d = this; d;
91 d = d->resolveCompositeBaseType(typeLoader).d.data()) {
92 if (d->regType == QQmlType::CppType)
93 return d->extraData.cppTypeData->attachedPropertiesType ? d : nullptr;
94
95 if (d->regType != QQmlType::CompositeType)
96 return nullptr;
97 }
98 return nullptr;
99 }
100
101 bool isComposite() const
102 {
103 return regType == QQmlType::CompositeType || regType == QQmlType::CompositeSingletonType;
104 }
105
106 bool isValueType() const
107 {
108 return regType == QQmlType::CppType && !(typeId.flags() & QMetaType::PointerToQObject);
109 }
110
111 QQmlType resolveCompositeBaseType(QQmlTypeLoader *typeLoader) const;
112 QQmlPropertyCache::ConstPtr compositePropertyCache(QQmlTypeLoader *typeLoader) const;
113
114 struct QQmlCppTypeData
115 {
116 int allocationSize;
117 void (*newFunc)(void *, void *);
118 void *userdata = nullptr;
119 QString noCreationReason;
120 QVariant (*createValueTypeFunc)(const QJSValue &);
121 int parserStatusCast;
122 QObject *(*extFunc)(QObject *);
123 const QMetaObject *extMetaObject;
124 QQmlCustomParser *customParser;
125 QQmlAttachedPropertiesFunc attachedPropertiesFunc;
126 const QMetaObject *attachedPropertiesType;
127 int propertyValueSourceCast;
128 int propertyValueInterceptorCast;
129 int finalizerCast;
130 bool registerEnumClassesUnscoped;
131 bool registerEnumsFromRelatedTypes;
132 bool constructValueType;
133 bool populateValueType;
134 };
135
136 struct QQmlSingletonTypeData
137 {
138 QQmlType::SingletonInstanceInfo::ConstPtr singletonInstanceInfo;
139 QObject *(*extFunc)(QObject *);
140 const QMetaObject *extMetaObject;
141 };
142
143 int index = -1;
144
145 union extraData {
146 extraData() {} // QQmlTypePrivate() does the actual construction.
147 ~extraData() {} // ~QQmlTypePrivate() does the actual destruction.
148
149 QQmlCppTypeData *cppTypeData;
150 QQmlSingletonTypeData *singletonTypeData;
151 QUrl compositeTypeData;
152 QUrl javaScriptTypeData;
153 QUrl inlineComponentTypeData;
154 QMetaSequence sequentialContainerTypeData;
155 const char *interfaceTypeData;
156 } extraData;
157 static_assert(sizeof(extraData) == sizeof(void *));
158
159 QHashedString module;
160 QString name;
161 QString elementName;
162 QMetaType typeId;
163 QMetaType listId;
164 QQmlType::RegistrationType regType;
165 QTypeRevision version;
166 QTypeRevision revision = QTypeRevision::zero();
167 const QMetaObject *baseMetaObject = nullptr;
168
169 void setName(const QString &uri, const QString &element);
170
171 template<typename String>
172 static int enumValue(
173 const QQmlRefPointer<const QQmlTypePrivate> &d, QQmlTypeLoader *typeLoader,
174 const String &name, bool *ok)
175 {
176 const auto *rv = doGetEnumOp<const int *>(
177 d, typeLoader, [&](const QQmlTypePrivate::Enums *enums) {
178 return enums->enums.value(name);
179 }, [](const int *p) { return !!p; }, ok);
180 return rv ? *rv : -1;
181 }
182
183 template<Enums::Scoping scoping, typename String>
184 static int enumIndex(
185 const QQmlRefPointer<const QQmlTypePrivate> &d, QQmlTypeLoader *typeLoader,
186 const String &name, bool *ok)
187 {
188 const auto *rv = doGetEnumOp<const int *> (
189 d, typeLoader, [&](const QQmlTypePrivate::Enums *enums) {
190 if constexpr (scoping == Enums::Scoped)
191 return enums->scopedEnumIndex.value(name);
192 else
193 return enums->unscopedEnumIndex.value(name);
194 }, [](const int *p) { return !!p; }, ok);
195 return rv ? *rv : -1;
196 }
197
198 template<Enums::Scoping scoping, typename String>
199 static int enumValue(
200 const QQmlRefPointer<const QQmlTypePrivate> &d, QQmlTypeLoader *typeLoader, int index,
201 const String &name, bool *ok)
202 {
203 const auto *rv = doGetEnumOp<const int *>(
204 d, typeLoader, [&](const QQmlTypePrivate::Enums *enums) {
205 if constexpr (scoping == Enums::Scoped) {
206 Q_ASSERT(index > -1 && index < enums->scopedEnums.size());
207 return enums->scopedEnums.at(index)->value(name);
208 } else {
209 Q_ASSERT(index > -1 && index < enums->unscopedEnums.size());
210 return enums->unscopedEnums.at(index)->value(name);
211 }
212 }, [](const int *p) { return !!p; }, ok);
213 return rv ? *rv : -1;
214 }
215
216 template<Enums::Scoping scoping, typename String1, typename String2>
217 static int enumValue(
218 const QQmlRefPointer<const QQmlTypePrivate> &d, QQmlTypeLoader *typeLoader,
219 const String1 &scopedEnumName, const String2 &name, bool *ok)
220 {
221 const auto *rv = doGetEnumOp<const int *>(
222 d, typeLoader, [&](const QQmlTypePrivate::Enums *enums) {
223 const QStringHash<int> *enumIndex;
224 const QList<QStringHash<int> *> *_enums;
225 if constexpr (scoping == Enums::Scoped) {
226 enumIndex = &enums->scopedEnumIndex;
227 _enums = &enums->scopedEnums;
228 } else {
229 enumIndex = &enums->unscopedEnumIndex;
230 _enums = &enums->unscopedEnums;
231 }
232
233 const int *rv = enumIndex->value(scopedEnumName);
234 if (!rv)
235 return static_cast<int *>(nullptr);
236
237 const int index = *rv;
238 Q_ASSERT(index > -1 && index < _enums->size());
239 return _enums->at(index)->value(name);
240 }, [](const int *p) { return !!p; }, ok);
241 return rv ? *rv : -1;
242 }
243
244 template<Enums::Scoping scoping>
245 static QString enumKey(
246 const QQmlRefPointer<const QQmlTypePrivate> &d, QQmlTypeLoader *typeLoader,
247 int index, int value, bool *ok)
248 {
249 return doGetEnumOp<QString>(d, typeLoader, [&](const QQmlTypePrivate::Enums *enums) {
250 const QList<QStringHash<int> *> *_enums;
251 if constexpr (scoping == Enums::Scoped)
252 _enums = &enums->scopedEnums;
253 else
254 _enums = &enums->unscopedEnums;
255
256 Q_ASSERT(index > -1 && index < _enums->size());
257 const auto hash = _enums->at(index);
258 for (auto it = hash->constBegin(), end = hash->constEnd(); it != end; ++it) {
259 if (it.value() == value)
260 return QString(it.key());
261 }
262 return QString();
263 }, [](const QString &s) { return !s.isEmpty(); }, ok);
264 }
265
266 template<Enums::Scoping scoping>
267 static QStringList enumKeys(
268 const QQmlRefPointer<const QQmlTypePrivate> &d, QQmlTypeLoader *typeLoader,
269 int index, int value, bool *ok)
270 {
271 return doGetEnumOp<QStringList>(d, typeLoader, [&](const QQmlTypePrivate::Enums *enums) {
272 const QList<QStringHash<int> *> *_enums;
273 if constexpr (scoping == Enums::Scoped)
274 _enums = &enums->scopedEnums;
275 else
276 _enums = &enums->unscopedEnums;
277
278 Q_ASSERT(index > -1 && index < _enums->size());
279 QStringList keys;
280 const auto hash = _enums->at(index);
281 for (auto it = hash->constBegin(), end = hash->constEnd(); it != end; ++it) {
282 if (it.value() == value)
283 keys.append(QString(it.key()));
284 }
285 std::reverse(keys.begin(), keys.end());
286 return keys;
287 }, [](const QStringList &l) { return !l.empty(); }, ok);
288 }
289
290 const QMetaObject *metaObject() const
291 {
292 if (isValueType())
293 return metaObjectForValueType();
294
295 const QQmlTypePrivate::ProxyMetaObjects *proxies = init();
296 return proxies->data.isEmpty()
297 ? baseMetaObject
298 : proxies->data.constFirst().metaObject;
299 }
300
301 const QMetaObject *metaObjectForValueType() const
302 {
303 Q_ASSERT(isValueType());
304
305 // Prefer the extension meta object, if any.
306 // Extensions allow registration of non-gadget value types.
307 if (const QMetaObject *extensionMetaObject = extraData.cppTypeData->extMetaObject) {
308 // This may be a namespace even if the original metaType isn't.
309 // You can do such things with QML_FOREIGN declarations.
310 if (extensionMetaObject->metaType().flags() & QMetaType::IsGadget)
311 return extensionMetaObject;
312 }
313
314 if (baseMetaObject) {
315 // This may be a namespace even if the original metaType isn't.
316 // You can do such things with QML_FOREIGN declarations.
317 if (baseMetaObject->metaType().flags() & QMetaType::IsGadget)
318 return baseMetaObject;
319 }
320
321 return nullptr;
322 }
323
324 static QQmlType visibleQmlTypeByName(
325 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit,
326 const QString &elementName, QQmlTypeLoader *typeLoader)
327 {
328 // Invalid speculative inline component types are either rejected at creation time
329 // (if the base type is already registered) or pruned when the base type registers.
330 return unit->typeNameCache->query<QQmlImport::AllowRecursion>(
331 elementName, typeLoader).type;
332 }
333
334 // Tries the base unit's resolvedTypes first. If successful, that is cheap
335 // because it's just a hash. Otherwise falls back to typeNameCache.
336 // typeNameCache is slower because it will do a generic type search on all imports.
337 // This can involve iterating all the types of an import or querying QQmlMetaType for
338 // further details.
339 // TODO: Not all referenced types are pre-resolved when loading. That should be fixed.
340 // In particular, types only used in function signatures are not resolved.
341 static QQmlType visibleQmlTypeByName(
342 const QV4::ExecutableCompilationUnit *unit, int elementNameId,
343 QQmlTypeLoader *typeLoader = nullptr)
344 {
345 const auto &base = unit->baseCompilationUnit();
346 const auto it = base->resolvedTypes.constFind(elementNameId);
347 if (it == base->resolvedTypes.constEnd()) {
348 return visibleQmlTypeByName(
349 base, base->stringAt(elementNameId),
350 typeLoader ? typeLoader : unit->engine->typeLoader());
351 }
352
353 return (*it)->type();
354 }
355
356private:
357 mutable QAtomicPointer<const ProxyMetaObjects> proxyMetaObjects;
358 mutable QAtomicPointer<const Enums> enums;
359
360 ~QQmlTypePrivate();
361 friend class QQmlRefCounted<QQmlTypePrivate>;
362
363 struct EnumInfo {
364 QStringList path;
365 QString metaObjectName;
366 QString enumName;
367 QString enumKey;
368 QString metaEnumScope;
369 bool scoped;
370 };
371
372 template<typename Ret, typename Op, typename Check>
373 static Ret doGetEnumOp(const QQmlRefPointer<const QQmlTypePrivate> &d,
374 QQmlTypeLoader *typeLoader, Op &&op, Check &&check, bool *ok)
375 {
376 Q_ASSERT(ok);
377 if (d) {
378 if (const QQmlTypePrivate::Enums *enums = d->initEnums(typeLoader)) {
379 if (Ret rv = op(enums); check(rv)) {
380 *ok = true;
381 return rv;
382 }
383 }
384 }
385
386 *ok = false;
387 return Ret();
388 }
389
390 const Enums *initEnums(QQmlTypeLoader *typeLoader) const;
391 void insertEnums(Enums *enums, const QMetaObject *metaObject) const;
392 void insertEnumsFromPropertyCache(Enums *enums, const QQmlPropertyCache::ConstPtr &cache) const;
393
394 void createListOfPossibleConflictingItems(const QMetaObject *metaObject, QList<EnumInfo> &enumInfoList, QStringList path) const;
395 void createEnumConflictReport(const QMetaObject *metaObject, const QString &conflictingKey) const;
396};
397
399
400#endif // QQMLTYPE_P_P_H
Combined button and popup list for selecting options.
static bool isPropertyRevisioned(const QMetaObject *mo, int index)
Definition qqmltype.cpp:171