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
qmetatypesjsonprocessor.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
12
13#include <QtCore/qcborarray.h>
14#include <QtCore/qcbormap.h>
15#include <QtCore/qdir.h>
16#include <QtCore/qfile.h>
17#include <QtCore/qjsondocument.h>
18#include <QtCore/qqueue.h>
19
21
22using namespace Qt::StringLiterals;
23using namespace Constants;
24using namespace Constants::MetatypesDotJson;
25using namespace Constants::MetatypesDotJson::Qml;
26using namespace QAnyStringViewUtils;
27
28const MetaTypePrivate MetaType::s_empty;
29
30// TODO: This could be optimized to store the objects in a more compact way.
31std::vector<std::unique_ptr<MetaTypePrivate>> s_pool;
32
33static QCborValue fromJson(const QByteArray &json, QJsonParseError *error)
34{
35 const QJsonDocument jsonValue = QJsonDocument::fromJson(json, error);
36 if (jsonValue.isArray())
37 return QCborValue::fromJsonValue(jsonValue.array());
38 if (jsonValue.isObject())
39 return QCborValue::fromJsonValue(jsonValue.object());
40 return QCborValue();
41}
42
43QList<QAnyStringView> MetaTypesJsonProcessor::namespaces(const MetaType &classDef)
44{
45 const QAnyStringView unqualified = classDef.className();
46 const QAnyStringView qualified = classDef.qualifiedClassName();
47
48 QList<QAnyStringView> namespaces;
49 if (qualified != unqualified) {
50 namespaces = split(qualified, "::"_L1);
51 Q_ASSERT(namespaces.last() == unqualified);
52 namespaces.pop_back();
53 }
54
55 return namespaces;
56}
57
58bool MetaTypesJsonProcessor::processTypes(const QStringList &files)
59{
60 for (const QString &source: files) {
61 if (m_seenMetaTypesFiles.hasSeen(QDir::cleanPath(source)))
62 continue;
63
64 QCborValue metaObjects;
65 {
66 QFile f(source);
67 if (!f.open(QIODevice::ReadOnly)) {
68 error(source) << "Cannot open file for reading";
69 return false;
70 }
71 QJsonParseError parseError = {0, QJsonParseError::NoError};
72 metaObjects = fromJson(f.readAll(), &parseError);
73 if (parseError.error != QJsonParseError::NoError) {
74 error(source)
75 << "Failed to parse JSON:" << parseError.error
76 << parseError.errorString();
77 return false;
78 }
79 }
80
81 if (metaObjects.isArray()) {
82 const QCborArray metaObjectsArray = metaObjects.toArray();
83 for (const QCborValue &metaObject : metaObjectsArray) {
84 if (!metaObject.isMap()) {
85 error(source) << "JSON is not an object";
86 return false;
87 }
88
89 processTypes(metaObject.toMap());
90 }
91 } else if (metaObjects.isMap()) {
92 processTypes(metaObjects.toMap());
93 } else {
94 error(source) << "JSON is not an object or an array";
95 return false;
96 }
97 }
98
99 return true;
100}
101
103{
104 QFile typesFile(types);
105 if (!typesFile.open(QIODevice::ReadOnly)) {
106 error(types) << "Cannot open foreign types file";
107 return false;
108 }
109
110 QJsonParseError parseError = {0, QJsonParseError::NoError};
111 QCborValue foreignMetaObjects = fromJson(typesFile.readAll(), &parseError);
112 if (parseError.error != QJsonParseError::NoError) {
113 error(types)
114 << "Failed to parse JSON:" << parseError.error
115 << parseError.errorString();
116 return false;
117 }
118
119 const QCborArray foreignObjectsArray = foreignMetaObjects.toArray();
120 for (const QCborValue &metaObject : foreignObjectsArray) {
121 if (!metaObject.isMap()) {
122 error(types) << "JSON is not an object";
123 return false;
124 }
125
126 processForeignTypes(metaObject.toMap());
127 }
128
129 return true;
130}
131
132bool MetaTypesJsonProcessor::processForeignTypes(const QStringList &foreignTypesFiles)
133{
134 bool success = true;
135
136 for (const QString &types : foreignTypesFiles) {
137 if (m_seenMetaTypesFiles.hasSeen(QDir::cleanPath(types)))
138 continue;
139
140 if (!processForeignTypes(types))
141 success = false;
142 }
143 return success;
144}
145
146template<typename String>
147static void sortStringList(QList<String> *list)
148{
149 std::sort(list->begin(), list->end());
150 const auto newEnd = std::unique(list->begin(), list->end());
151 list->erase(typename QList<String>::const_iterator(newEnd), list->constEnd());
152}
153
155{
156 sortTypes(m_types);
157}
158
160{
161 sortTypes(m_foreignTypes);
162 sortStringList(&m_primitiveTypes);
163 sortStringList(&m_usingDeclarations);
164 addRelatedTypes();
165 sortStringList(&m_referencedTypes);
166 sortStringList(&m_includes);
167}
168
170{
171 QString registrationHelper;
172 for (const auto &obj: m_types) {
173 const QString className = obj.className().toString();
174 const QString qualifiedClassName = obj.qualifiedClassName().toString();
175 const QString foreignClassName = className + u"Foreign";
176 QStringList qmlElements;
177 QString qmlUncreatable;
178 QString qmlAttached;
179 bool isSingleton = false;
180 bool isExplicitlyUncreatable = false;
181 bool isNamespace = obj.kind() == MetaType::Kind::Namespace;
182 for (const ClassInfo &entry: obj.classInfos()) {
183 const auto name = entry.name;
184 const auto value = entry.value;
185 if (name == S_ELEMENT) {
186 if (value == S_AUTO) {
187 qmlElements.append(u"QML_NAMED_ELEMENT("_s + className + u")"_s);
188 } else if (value == S_ANONYMOUS) {
189 qmlElements.append(u"QML_ANONYMOUS"_s);
190 } else {
191 qmlElements.append(u"QML_NAMED_ELEMENT("_s + value.toString() + u")");
192 }
193 } else if (name == S_CREATABLE && value == S_FALSE) {
194 isExplicitlyUncreatable = true;
195 } else if (name == S_UNCREATABLE_REASON) {
196 qmlUncreatable = u"QML_UNCREATABLE(\""_s + value.toString() + u"\")";
197 } else if (name == S_ATTACHED) {
198 qmlAttached = u"QML_ATTACHED("_s + value.toString() + u")";
199 } else if (name == S_SINGLETON) {
200 isSingleton = true;
201 }
202 }
203 if (qmlElements.isEmpty())
204 continue; // no relevant entries found
205 const QString spaces = u" "_s;
206 if (isNamespace) {
207 registrationHelper += u"\nnamespace "_s + foreignClassName + u"{\n Q_NAMESPACE\n"_s;
208 registrationHelper += spaces + u"QML_FOREIGN_NAMESPACE(" + qualifiedClassName + u")\n"_s;
209 } else {
210 registrationHelper += u"\nstruct "_s + foreignClassName + u"{\n Q_GADGET\n"_s;
211 registrationHelper += spaces + u"QML_FOREIGN(" + qualifiedClassName + u")\n"_s;
212 }
213 registrationHelper += spaces + qmlElements.join(u"\n"_s) + u"\n"_s;
214 if (isSingleton)
215 registrationHelper += spaces + u"QML_SINGLETON\n"_s;
216 if (isExplicitlyUncreatable) {
217 if (qmlUncreatable.isEmpty())
218 registrationHelper += spaces + uR"(QML_UNCREATABLE(""))" + u"n";
219 else
220 registrationHelper += spaces + qmlUncreatable + u"\n";
221 }
222 if (!qmlAttached.isEmpty())
223 registrationHelper += spaces + qmlAttached + u"\n";
224 registrationHelper += u"}";
225 if (!isNamespace)
226 registrationHelper += u";";
227 registrationHelper += u"\n";
228 }
229 return registrationHelper;
230}
231
232MetaTypesJsonProcessor::PreProcessResult MetaTypesJsonProcessor::preProcess(
233 const MetaType &classDef, PopulateMode populateMode)
234{
235 // If this type is a self-extending value type or a sequence type or has a JavaScript extension
236 // and is not the root object, then it's foreign type has no entry of its own.
237 // In that case we need to generate a "primitive" entry.
238
239 QList<QAnyStringView> primitiveAliases;
240 UsingDeclaration usingDeclaration;
241
242 RegistrationMode mode = NoRegistration;
243 bool isSelfExtendingValueType = false;
244 bool hasJavaScriptExtension = false;
245 bool isRootObject = false;
246 bool isSequence = false;
247
248 for (const ClassInfo &classInfo : classDef.classInfos()) {
249 if (classInfo.name == S_FOREIGN)
250 usingDeclaration.alias = classInfo.value;
251 else if (classInfo.name == S_PRIMITIVE_ALIAS)
252 primitiveAliases.append(classInfo.value);
253 else if (classInfo.name == S_EXTENSION_IS_JAVA_SCRIPT)
254 hasJavaScriptExtension = (classInfo.value == S_TRUE);
255 else if (classInfo.name == S_EXTENDED && classDef.kind() == MetaType::Kind::Gadget)
256 isSelfExtendingValueType = classInfo.value == classDef.className();
257 else if (classInfo.name == S_ROOT)
258 isRootObject = (classInfo.value == S_TRUE);
259 else if (classInfo.name == S_SEQUENCE)
260 isSequence = true;
261 else if (classInfo.name == S_USING)
262 usingDeclaration.original = classInfo.value;
263 else if (populateMode == PopulateMode::Yes && classInfo.name == S_ELEMENT) {
264 switch (classDef.kind()) {
265 case MetaType::Kind::Object:
266 mode = ObjectRegistration;
267 break;
268 case MetaType::Kind::Gadget:
269 mode = GadgetRegistration;
270 break;
271 case MetaType::Kind::Namespace:
272 mode = NamespaceRegistration;
273 break;
274 default:
275 warning(classDef)
276 << "Not registering a classInfo which is neither an object,"
277 << "nor a gadget, nor a namespace:"
278 << classInfo.name.toString();
279 break;
280 }
281 }
282 }
283
284 return PreProcessResult {
285 std::move(primitiveAliases),
286 usingDeclaration,
287 (!isRootObject && (isSequence || isSelfExtendingValueType || hasJavaScriptExtension))
288 ? usingDeclaration.alias
289 : QAnyStringView(),
290 mode
291 };
292
293}
294
295// TODO: Remove this when QAnyStringView gets a proper qHash()
296static size_t qHash(QAnyStringView string, size_t seed = 0)
297{
298 return string.visit([seed](auto view) {
299 if constexpr (std::is_same_v<decltype(view), QStringView>)
300 return qHash(view, seed);
301 if constexpr (std::is_same_v<decltype(view), QLatin1StringView>)
302 return qHash(view, seed);
303 if constexpr (std::is_same_v<decltype(view), QUtf8StringView>)
304 return qHash(QByteArrayView(view.data(), view.length()), seed);
305 });
306}
307
308static bool qualifiedClassNameLessThan(const MetaType &a, const MetaType &b)
309{
310 return a.qualifiedClassName() < b.qualifiedClassName();
311}
312
317
319{
320 switch (relation) {
321 case TypeRelation::Property: return "property"_L1;
322 case TypeRelation::Argument: return "argument"_L1;
323 case TypeRelation::Return: return "return"_L1;
324 case TypeRelation::Enum: return "enum"_L1;
325 case TypeRelation::Attached: return "attached"_L1;
326 case TypeRelation::SequenceValue: return "sequence value"_L1;
327 case TypeRelation::Extension: return "extension"_L1;
328 default:
329 break;
330 }
331
332 Q_UNREACHABLE_RETURN(QLatin1StringView());
333}
334
335void MetaTypesJsonProcessor::addRelatedTypes()
336{
337 QSet<QAnyStringView> processedRelatedNativeNames;
338 QSet<QAnyStringView> processedRelatedJavaScriptNames;
339 QSet<QAnyStringView> unresolvedForeignNames;
340 QQueue<MetaType> typeQueue;
341 typeQueue.append(m_types);
342
343 const auto addRelatedName
344 = [&](QAnyStringView relatedName, const QList<QAnyStringView> &namespaces) {
345 if (const FoundType related = QmlTypesClassDescription::findType(
346 m_types, m_foreignTypes, relatedName, namespaces)) {
347
348 if (!related.javaScript.isEmpty())
349 processedRelatedJavaScriptNames.insert(related.javaScript.qualifiedClassName());
350
351 if (!related.native.isEmpty())
352 processedRelatedNativeNames.insert(related.native.qualifiedClassName());
353
354 return true;
355 } else {
356 return false;
357 }
358 };
359
360 const auto addRelatedType = [&](const MetaType &type) {
361 const QAnyStringView qualifiedName = type.qualifiedClassName();
362 if (type.inputFile().isEmpty())
363 processedRelatedJavaScriptNames.insert(qualifiedName);
364 else
365 processedRelatedNativeNames.insert(qualifiedName);
366 };
367
368 // First mark all classes registered from this module as already processed.
369 for (const MetaType &type : std::as_const(m_types)) {
370 addRelatedType(type);
371 for (const ClassInfo &obj : type.classInfos()) {
372 if (obj.name == S_FOREIGN) {
373 const QAnyStringView foreign = obj.value;
374 if (!addRelatedName(foreign, namespaces(type)))
375 unresolvedForeignNames.insert(foreign);
376 break;
377 }
378 }
379 }
380
381 // Then mark all classes registered from other modules as already processed.
382 // We don't want to generate them again for this module.
383 for (const MetaType &foreignType : std::as_const(m_foreignTypes)) {
384 bool seenQmlPrefix = false;
385 for (const ClassInfo &obj : foreignType.classInfos()) {
386 const QAnyStringView name = obj.name;
387 if (!seenQmlPrefix && startsWith(name, "QML."_L1)) {
388 addRelatedType(foreignType);
389 seenQmlPrefix = true;
390 }
391 if (name == S_FOREIGN
392 || name == S_EXTENDED
393 || name == S_ATTACHED
394 || name == S_SEQUENCE) {
395 ResolvedTypeAlias foreign(obj.value, m_usingDeclarations);
396 if (!addRelatedName(foreign.type, namespaces(foreignType)))
397 unresolvedForeignNames.insert(foreign.type);
398 }
399 }
400 }
401
402 const auto addReference
403 = [&](const MetaType &type, QSet<QAnyStringView> *processedRelatedNames,
404 FoundType::Origin origin) {
405 if (type.isEmpty())
406 return;
407 QAnyStringView qualifiedName = type.qualifiedClassName();
408 m_referencedTypes.append(qualifiedName);
409 const qsizetype size = processedRelatedNames->size();
410 processedRelatedNames->insert(qualifiedName);
411
412 if (processedRelatedNames->size() == size)
413 return;
414
415 typeQueue.enqueue(type);
416
417 if (origin == FoundType::OwnTypes)
418 return;
419
420 // Add to own types since we need it for our registrations.
421 const auto insert = std::lower_bound(
422 m_types.constBegin(), m_types.constEnd(), type,
423 qualifiedClassNameLessThan);
424 m_types.insert(insert, type);
425
426 // We only add types to m_types of which we know we can reach them via the existing
427 // m_includes. We do not add to m_includes, because any further headers may not be
428 // #include'able.
429
430 // Remove from the foreign types to avoid the ODR warning.
431 const auto remove = std::equal_range(
432 m_foreignTypes.constBegin(), m_foreignTypes.constEnd(), type,
433 qualifiedClassNameLessThan);
434 for (auto it = remove.first; it != remove.second; ++it) {
435 if (*it == type) {
436 m_foreignTypes.erase(it);
437 break;
438 }
439 }
440 };
441
442 const auto addInterface
443 = [&](QAnyStringView typeName, const QList<QAnyStringView> &namespaces) {
444 if (const FoundType other = QmlTypesClassDescription::findType(
445 m_types, m_foreignTypes, typeName, namespaces)) {
446 if (!other.native.isEmpty()) {
447 addReference(other.native, &processedRelatedNativeNames, other.nativeOrigin);
448 return true;
449 }
450 } else {
451 // Do not warn about unresolved interfaces.
452 // They don't have to have Q_OBJECT or Q_GADGET.
453 unresolvedForeignNames.insert(typeName);
454 }
455
456 processedRelatedNativeNames.insert(typeName);
457 return false;
458 };
459
460 const auto doAddReferences = [&](QAnyStringView typeName,
461 const QList<QAnyStringView> &namespaces) {
462 if (const FoundType other = QmlTypesClassDescription::findType(
463 m_types, m_foreignTypes, typeName, namespaces)) {
464 addReference(other.native, &processedRelatedNativeNames, other.nativeOrigin);
465 addReference(
466 other.javaScript, &processedRelatedJavaScriptNames, other.javaScriptOrigin);
467 return true;
468 }
469
470 return false;
471 };
472
473 const auto addType = [&](const MetaType &context, QAnyStringView typeName,
474 const QList<QAnyStringView> &namespaces, TypeRelation relation) {
475 if (doAddReferences(typeName, namespaces))
476 return true;
477
478 // If it's an enum, add the surrounding type.
479 const QLatin1StringView separator("::");
480 if (const qsizetype index = lastIndexOf(typeName, separator); index > 0) {
481 if (const FoundType other = QmlTypesClassDescription::findType(
482 m_types, m_foreignTypes, typeName.left(index), namespaces)) {
483
484 const QAnyStringView enumName = typeName.mid(index + separator.length());
485
486 for (const Enum &enumerator : other.native.enums()) {
487 if (enumerator.name != enumName && enumerator.alias != enumName)
488 continue;
489
490 addReference(other.native, &processedRelatedNativeNames, other.nativeOrigin);
491 addReference(
492 other.javaScript, &processedRelatedJavaScriptNames,
493 other.javaScriptOrigin);
494 return true;
495 }
496 }
497 }
498
499 // If it's an enum of the context type itself, we don't have to do anything.
500 for (const Enum &enumerator : context.enums()) {
501 if (enumerator.name == typeName || enumerator.alias == typeName)
502 return true;
503 }
504
505 // If we've detected this type as unresolved foreign and it actually belongs to this module,
506 // we'll get to it again when we process it as foreign type. In that case we'll look at the
507 // special cases for sequences and extensions.
508 if (!unresolvedForeignNames.contains(typeName) && !isPrimitive(typeName)) {
509 warning(context) << typeName << "is used as" << typeRelationString(relation)
510 << "type but cannot be found.";
511 }
512
513 processedRelatedNativeNames.insert(typeName);
514 processedRelatedJavaScriptNames.insert(typeName);
515 return false;
516 };
517
518
519
520 const auto addSupers = [&](const MetaType &context, const QList<QAnyStringView> &namespaces) {
521 for (const Interface &iface : context.ifaces())
522 addInterface(interfaceName(iface), namespaces);
523
524 // We don't warn about missing bases for value types. They don't have to be registered.
525 bool warnAboutSupers = context.kind() != MetaType::Kind::Gadget;
526
527 QList<QAnyStringView> missingSupers;
528
529 for (const BaseType &superObject : context.superClasses()) {
530 if (superObject.access != Access::Public)
531 continue;
532
533 QAnyStringView typeName = superObject.name;
534 if (doAddReferences(typeName, namespaces))
535 warnAboutSupers = false;
536 else
537 missingSupers.append(typeName);
538 }
539
540 for (QAnyStringView typeName : std::as_const(missingSupers)) {
541 // If we've found one valid base type, don't complain about the others.
542 if (warnAboutSupers
543 && !unresolvedForeignNames.contains(typeName)
544 && !isPrimitive(typeName)) {
545 warning(context) << typeName << "is used as base type but cannot be found.";
546 }
547
548 processedRelatedNativeNames.insert(typeName);
549 processedRelatedJavaScriptNames.insert(typeName);
550 }
551 };
552
553 const auto addEnums = [&](const MetaType &context,
554 const QList<QAnyStringView> &namespaces) {
555 for (const Enum &enumerator : context.enums()) {
556 ResolvedTypeAlias resolved(enumerator.type, m_usingDeclarations);
557 if (!resolved.type.isEmpty())
558 addType(context, resolved.type, namespaces, TypeRelation::Enum);
559 }
560 };
561
562 const auto addRelation = [&](const MetaType &classDef, const ClassInfo &obj,
563 const QList<QAnyStringView> &namespaces) {
564 const QAnyStringView objNameValue = obj.name;
565 if (objNameValue == S_ATTACHED) {
566 addType(classDef, obj.value, namespaces, TypeRelation::Attached);
567 return true;
568 } else if (objNameValue == S_SEQUENCE) {
569 ResolvedTypeAlias value(obj.value, m_usingDeclarations);
570 addType(classDef, value.type, namespaces, TypeRelation::SequenceValue);
571 return true;
572 } else if (objNameValue == S_EXTENDED) {
573 const QAnyStringView value = obj.value;
574 addType(classDef, value, namespaces, TypeRelation::Extension);
575 return true;
576 }
577 return false;
578 };
579
580 // Then recursively iterate the super types and attached types, marking the
581 // ones we are interested in as related.
582 while (!typeQueue.isEmpty()) {
583 QAnyStringView unresolvedForeign;
584
585 const MetaType classDef = typeQueue.dequeue();
586 const QList<QAnyStringView> namespaces = MetaTypesJsonProcessor::namespaces(classDef);
587
588 for (const ClassInfo &obj : classDef.classInfos()) {
589 if (addRelation(classDef, obj, namespaces))
590 continue;
591 if (obj.name != S_FOREIGN)
592 continue;
593
594 const QAnyStringView foreignClassName = obj.value;
595
596 // A type declared as QML_FOREIGN will usually be a foreign type, but it can
597 // actually be an additional registration of a local type, too.
598 if (const FoundType found = QmlTypesClassDescription::findType(
599 m_foreignTypes, {}, foreignClassName, namespaces)) {
600 const MetaType other = found.select(classDef, "Foreign");
601 const QList<QAnyStringView> otherNamespaces
602 = MetaTypesJsonProcessor::namespaces(other);
603 addSupers(other, otherNamespaces);
604 addEnums(other, otherNamespaces);
605
606 for (const ClassInfo &obj : other.classInfos()) {
607 if (addRelation(classDef, obj, otherNamespaces))
608 break;
609 // No, you cannot chain S_FOREIGN declarations. Sorry.
610 }
611 } else if (!QmlTypesClassDescription::findType(
612 m_types, {}, foreignClassName, namespaces)) {
613 unresolvedForeign = foreignClassName;
614 }
615 }
616
617 if (!unresolvedForeign.isEmpty() && !isPrimitive(unresolvedForeign)) {
618 warning(classDef)
619 << unresolvedForeign
620 << "is declared as foreign type, but cannot be found.";
621 }
622
623 addSupers(classDef, namespaces);
624 addEnums(classDef, namespaces);
625 }
626}
627
628void MetaTypesJsonProcessor::sortTypes(QList<MetaType> &types)
629{
630 std::sort(types.begin(), types.end(), qualifiedClassNameLessThan);
631}
632
633QString MetaTypesJsonProcessor::resolvedInclude(QAnyStringView include)
634{
635 if (!m_privateIncludes)
636 return include.toString();
637
638 if (endsWith(include, "_p.h"_L1))
639 return QLatin1String("private/") + include.toString();
640
641 if (startsWith(include, "qplatform"_L1) || startsWith(include, "qwindowsystem"_L1))
642 return QLatin1String("qpa/") + include.toString();
643
644 return include.toString();
645}
646
647void MetaTypesJsonProcessor::processTypes(const QCborMap &types)
648{
649 const QString include = resolvedInclude(types[S_INPUT_FILE].toStringView());
650 const QCborArray classes = types[S_CLASSES].toArray();
651 for (const QCborValue &cls : classes) {
652 const MetaType classDef(cls.toMap(), include);
653
654 const PreProcessResult preprocessed = preProcess(classDef, PopulateMode::Yes);
655 switch (preprocessed.mode) {
656 case NamespaceRegistration:
657 case GadgetRegistration:
658 case ObjectRegistration: {
659 if (!endsWith(include, QLatin1String(".h"))
660 && !endsWith(include, QLatin1String(".hpp"))
661 && !endsWith(include, QLatin1String(".hxx"))
662 && !endsWith(include, QLatin1String(".hh"))
663 && !endsWith(include, QLatin1String(".py"))
664 && contains(include, QLatin1Char('.'))) {
665 warning(include)
666 << "Class" << classDef.qualifiedClassName()
667 << "is declared in" << include << "which appears not to be a header."
668 << "The compilation of its registration to QML may fail.";
669 }
670 m_includes.append(include);
671 m_types.emplaceBack(classDef);
672 break;
673 }
674 case NoRegistration:
675 m_foreignTypes.emplaceBack(classDef);
676 break;
677 }
678
679 if (!preprocessed.foreignPrimitive.isEmpty()) {
680 m_primitiveTypes.emplaceBack(preprocessed.foreignPrimitive);
681 m_primitiveTypes.append(preprocessed.primitiveAliases);
682 }
683
684 if (preprocessed.usingDeclaration.isValid())
685 m_usingDeclarations.append(preprocessed.usingDeclaration);
686 }
687}
688
689void MetaTypesJsonProcessor::processForeignTypes(const QCborMap &types)
690{
691 const QString include = resolvedInclude(types[S_INPUT_FILE].toStringView());
692 const QCborArray classes = types[S_CLASSES].toArray();
693 for (const QCborValue &cls : classes) {
694 const MetaType classDef(cls.toMap(), include);
695 PreProcessResult preprocessed = preProcess(classDef, PopulateMode::No);
696
697 m_foreignTypes.emplaceBack(classDef);
698 if (!preprocessed.foreignPrimitive.isEmpty()) {
699 m_primitiveTypes.emplaceBack(preprocessed.foreignPrimitive);
700 m_primitiveTypes.append(preprocessed.primitiveAliases);
701 }
702
703 if (preprocessed.usingDeclaration.isValid())
704 m_usingDeclarations.append(preprocessed.usingDeclaration);
705 }
706}
707
708static QTypeRevision getRevision(const QCborMap &cbor)
709{
710 const auto it = cbor.find(S_REVISION);
711 return it == cbor.end()
712 ? QTypeRevision()
713 : QTypeRevision::fromEncodedVersion(it->toInteger());
714}
715
716static Access getAccess(const QCborMap &cbor)
717{
718 const QAnyStringView access = cbor[S_ACCESS].toStringView();
719 if (access == S_PUBLIC)
720 return Access::Public;
721 if (access == S_PROTECTED)
722 return Access::Protected;
723 return Access::Private;
724}
725
726BaseType::BaseType(const QCborMap &cbor)
729{
730}
731
732ClassInfo::ClassInfo(const QCborMap &cbor)
735{
736}
737
738Interface::Interface(const QCborValue &cbor)
739{
740 if (cbor.isArray()) {
741 QCborArray needlessWrapping = cbor.toArray();
742 className = needlessWrapping.size() > 0
743 ? needlessWrapping[0].toMap()[S_CLASS_NAME].toStringView()
744 : QAnyStringView();
745 } else {
746 className = cbor.toMap()[S_CLASS_NAME].toStringView();
747 }
748}
749
750Property::Property(const QCborMap &cbor)
760 , index(cbor[S_INDEX].toInteger(-1))
761 , lineNumber(cbor[S_LINENUMBER].toInteger(0))
763 , isFinal(cbor[S_FINAL].toBool())
764 , isVirtual(cbor[S_VIRTUAL].toBool())
765 , isOverride(cbor[S_OVERRIDE].toBool())
766 , isConstant(cbor[S_CONSTANT].toBool())
767 , isRequired(cbor[S_REQUIRED].toBool())
768{
769}
770
771Argument::Argument(const QCborMap &cbor)
774{
775}
776
777Method::Method(const QCborMap &cbor, bool isConstructor)
780 , index(cbor[S_INDEX].toInteger(InvalidIndex))
781 , lineNumber(cbor[S_LINENUMBER].toInteger(0))
784 , isCloned(cbor[S_IS_CLONED].toBool())
785 , isJavaScriptFunction(cbor[S_IS_JAVASCRIPT_FUNCTION].toBool())
786 , isConstructor(isConstructor || cbor[S_IS_CONSTRUCTOR].toBool())
787 , isConst(cbor[S_IS_CONST].toBool())
788{
789 const QCborArray args = cbor[S_ARGUMENTS].toArray();
790 for (const QCborValue &argument : args)
791 arguments.emplace_back(argument.toMap());
792
793 if (arguments.size() == 1) {
794 const QAnyStringView type = arguments[0].type;
795 if (type == "QQmlV4FunctionPtr"_L1 || type == "QQmlV4Function*"_L1) {
797 arguments.clear();
798 }
799 }
800}
801
802Enum::Enum(const QCborMap &cbor)
806 , lineNumber(cbor[S_LINENUMBER].toInteger(0))
807 , isFlag(cbor[S_IS_FLAG].toBool())
808 , isClass(cbor[S_IS_CLASS].toBool())
809{
810 const QCborArray vals = cbor[S_VALUES].toArray();
811 for (const QCborValue &value : vals)
812 values.emplace_back(value.toStringView());
813}
814
815MetaTypePrivate::MetaTypePrivate(const QCborMap &cbor, const QString &inputFile)
816 : cbor(cbor)
818{
819 className = cbor[S_CLASS_NAME].toStringView();
820 lineNumber = cbor[S_LINENUMBER].toInteger(0);
821 qualifiedClassName = cbor[S_QUALIFIED_CLASS_NAME].toStringView();
822
823 const QCborArray cborSuperClasses = cbor[S_SUPER_CLASSES].toArray();
824 for (const QCborValue &superClass : cborSuperClasses)
825 superClasses.emplace_back(superClass.toMap());
826
827 const QCborArray cborClassInfos = cbor[S_CLASS_INFOS].toArray();
828 for (const QCborValue &classInfo : cborClassInfos)
829 classInfos.emplace_back(classInfo.toMap());
830
831 const QCborArray cborIfaces = cbor[S_INTERFACES].toArray();
832 for (const QCborValue &iface : cborIfaces)
833 ifaces.emplace_back(iface);
834
835 const QCborArray cborProperties = cbor[S_PROPERTIES].toArray();
836 for (const QCborValue &property : cborProperties)
837 properties.emplace_back(property.toMap());
838
839 for (const QCborArray &cborMethods : { cbor[S_SLOTS].toArray(), cbor[S_METHODS].toArray() }) {
840 for (const QCborValue &method : cborMethods)
841 methods.emplace_back(method.toMap(), false);
842 }
843
844 const QCborArray cborSigs = cbor[S_SIGNALS].toArray();
845 for (const QCborValue &sig : cborSigs)
846 sigs.emplace_back(sig.toMap(), false);
847
848 const QCborArray cborConstructors = cbor[S_CONSTRUCTORS].toArray();
849 for (const QCborValue &constructor : cborConstructors)
850 constructors.emplace_back(constructor.toMap(), true);
851
852 const QCborArray cborEnums = cbor[S_ENUMS].toArray();
853 for (const QCborValue &enumerator : cborEnums)
854 enums.emplace_back(enumerator.toMap());
855
856 if (cbor[S_GADGET].toBool())
857 kind = Kind::Gadget;
858 else if (cbor[S_OBJECT].toBool())
859 kind = Kind::Object;
860 else if (cbor[S_NAMESPACE].toBool())
861 kind = Kind::Namespace;
862}
863
864MetaType::MetaType(const QCborMap &cbor, const QString &inputFile)
866{}
867
868QT_END_NAMESPACE
Access
Definition access.h:11
MetaType(const QCborMap &cbor, const QString &inputFile)
bool processForeignTypes(const QString &foreignTypesFile)
bool processTypes(const QStringList &files)
Combined button and popup list for selecting options.
static Access getAccess(const QCborMap &cbor)
static QCborValue fromJson(const QByteArray &json, QJsonParseError *error)
static QLatin1StringView typeRelationString(TypeRelation relation)
static void sortStringList(QList< String > *list)
static QTypeRevision getRevision(const QCborMap &cbor)
static bool qualifiedClassNameLessThan(const MetaType &a, const MetaType &b)
static size_t qHash(QAnyStringView string, size_t seed=0)
@ Public
Definition access.h:11
@ Private
Definition access.h:11
@ Protected
Definition access.h:11
QDebug warning(const MetaType &classDef)
Argument(const QCborMap &cbor)
BaseType(const QCborMap &cbor)
ClassInfo(const QCborMap &cbor)
Enum(const QCborMap &cbor)
Interface(const QCborValue &cbor)
MetaTypePrivate(const QCborMap &cbor, const QString &inputFile)
Method(const QCborMap &cbor, bool isConstructor)
static constexpr int InvalidIndex
Property(const QCborMap &cbor)