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
295static bool qualifiedClassNameLessThan(const MetaType &a, const MetaType &b)
296{
297 return a.qualifiedClassName() < b.qualifiedClassName();
298}
299
304
306{
307 switch (relation) {
308 case TypeRelation::Property: return "property"_L1;
309 case TypeRelation::Argument: return "argument"_L1;
310 case TypeRelation::Return: return "return"_L1;
311 case TypeRelation::Enum: return "enum"_L1;
312 case TypeRelation::Attached: return "attached"_L1;
313 case TypeRelation::SequenceValue: return "sequence value"_L1;
314 case TypeRelation::Extension: return "extension"_L1;
315 default:
316 break;
317 }
318
319 Q_UNREACHABLE_RETURN(QLatin1StringView());
320}
321
322void MetaTypesJsonProcessor::addRelatedTypes()
323{
324 QSet<QAnyStringView> processedRelatedNativeNames;
325 QSet<QAnyStringView> processedRelatedJavaScriptNames;
326 QSet<QAnyStringView> unresolvedForeignNames;
327 QQueue<MetaType> typeQueue;
328 typeQueue.append(m_types);
329
330 const auto addRelatedName
331 = [&](QAnyStringView relatedName, const QList<QAnyStringView> &namespaces) {
332 if (const FoundType related = QmlTypesClassDescription::findType(
333 m_types, m_foreignTypes, relatedName, namespaces)) {
334
335 if (!related.javaScript.isEmpty())
336 processedRelatedJavaScriptNames.insert(related.javaScript.qualifiedClassName());
337
338 if (!related.native.isEmpty())
339 processedRelatedNativeNames.insert(related.native.qualifiedClassName());
340
341 return true;
342 } else {
343 return false;
344 }
345 };
346
347 const auto addRelatedType = [&](const MetaType &type) {
348 const QAnyStringView qualifiedName = type.qualifiedClassName();
349 if (type.inputFile().isEmpty())
350 processedRelatedJavaScriptNames.insert(qualifiedName);
351 else
352 processedRelatedNativeNames.insert(qualifiedName);
353 };
354
355 // First mark all classes registered from this module as already processed.
356 for (const MetaType &type : std::as_const(m_types)) {
357 addRelatedType(type);
358 for (const ClassInfo &obj : type.classInfos()) {
359 if (obj.name == S_FOREIGN) {
360 const QAnyStringView foreign = obj.value;
361 if (!addRelatedName(foreign, namespaces(type)))
362 unresolvedForeignNames.insert(foreign);
363 break;
364 }
365 }
366 }
367
368 // Then mark all classes registered from other modules as already processed.
369 // We don't want to generate them again for this module.
370 for (const MetaType &foreignType : std::as_const(m_foreignTypes)) {
371 bool seenQmlPrefix = false;
372 for (const ClassInfo &obj : foreignType.classInfos()) {
373 const QAnyStringView name = obj.name;
374 if (!seenQmlPrefix && startsWith(name, "QML."_L1)) {
375 addRelatedType(foreignType);
376 seenQmlPrefix = true;
377 }
378 if (name == S_FOREIGN
379 || name == S_EXTENDED
380 || name == S_ATTACHED
381 || name == S_SEQUENCE) {
382 ResolvedTypeAlias foreign(obj.value, m_usingDeclarations);
383 if (!addRelatedName(foreign.type, namespaces(foreignType)))
384 unresolvedForeignNames.insert(foreign.type);
385 }
386 }
387 }
388
389 const auto addReference
390 = [&](const MetaType &type, QSet<QAnyStringView> *processedRelatedNames,
391 FoundType::Origin origin) {
392 if (type.isEmpty())
393 return;
394 QAnyStringView qualifiedName = type.qualifiedClassName();
395 m_referencedTypes.append(qualifiedName);
396 const qsizetype size = processedRelatedNames->size();
397 processedRelatedNames->insert(qualifiedName);
398
399 if (processedRelatedNames->size() == size)
400 return;
401
402 typeQueue.enqueue(type);
403
404 if (origin == FoundType::OwnTypes)
405 return;
406
407 // Add to own types since we need it for our registrations.
408 const auto insert = std::lower_bound(
409 m_types.constBegin(), m_types.constEnd(), type,
410 qualifiedClassNameLessThan);
411 m_types.insert(insert, type);
412
413 // We only add types to m_types of which we know we can reach them via the existing
414 // m_includes. We do not add to m_includes, because any further headers may not be
415 // #include'able.
416
417 // Remove from the foreign types to avoid the ODR warning.
418 const auto remove = std::equal_range(
419 m_foreignTypes.constBegin(), m_foreignTypes.constEnd(), type,
420 qualifiedClassNameLessThan);
421 for (auto it = remove.first; it != remove.second; ++it) {
422 if (*it == type) {
423 m_foreignTypes.erase(it);
424 break;
425 }
426 }
427 };
428
429 const auto addInterface
430 = [&](QAnyStringView typeName, const QList<QAnyStringView> &namespaces) {
431 if (const FoundType other = QmlTypesClassDescription::findType(
432 m_types, m_foreignTypes, typeName, namespaces)) {
433 if (!other.native.isEmpty()) {
434 addReference(other.native, &processedRelatedNativeNames, other.nativeOrigin);
435 return true;
436 }
437 } else {
438 // Do not warn about unresolved interfaces.
439 // They don't have to have Q_OBJECT or Q_GADGET.
440 unresolvedForeignNames.insert(typeName);
441 }
442
443 processedRelatedNativeNames.insert(typeName);
444 return false;
445 };
446
447 const auto doAddReferences = [&](QAnyStringView typeName,
448 const QList<QAnyStringView> &namespaces) {
449 if (const FoundType other = QmlTypesClassDescription::findType(
450 m_types, m_foreignTypes, typeName, namespaces)) {
451 addReference(other.native, &processedRelatedNativeNames, other.nativeOrigin);
452 addReference(
453 other.javaScript, &processedRelatedJavaScriptNames, other.javaScriptOrigin);
454 return true;
455 }
456
457 return false;
458 };
459
460 const auto addType = [&](const MetaType &context, QAnyStringView typeName,
461 const QList<QAnyStringView> &namespaces, TypeRelation relation) {
462 if (doAddReferences(typeName, namespaces))
463 return true;
464
465 // If it's an enum, add the surrounding type.
466 const QLatin1StringView separator("::");
467 if (const qsizetype index = lastIndexOf(typeName, separator); index > 0) {
468 if (const FoundType other = QmlTypesClassDescription::findType(
469 m_types, m_foreignTypes, typeName.left(index), namespaces)) {
470
471 const QAnyStringView enumName = typeName.mid(index + separator.length());
472
473 for (const Enum &enumerator : other.native.enums()) {
474 if (enumerator.name != enumName && enumerator.alias != enumName)
475 continue;
476
477 addReference(other.native, &processedRelatedNativeNames, other.nativeOrigin);
478 addReference(
479 other.javaScript, &processedRelatedJavaScriptNames,
480 other.javaScriptOrigin);
481 return true;
482 }
483 }
484 }
485
486 // If it's an enum of the context type itself, we don't have to do anything.
487 for (const Enum &enumerator : context.enums()) {
488 if (enumerator.name == typeName || enumerator.alias == typeName)
489 return true;
490 }
491
492 // If we've detected this type as unresolved foreign and it actually belongs to this module,
493 // we'll get to it again when we process it as foreign type. In that case we'll look at the
494 // special cases for sequences and extensions.
495 if (!unresolvedForeignNames.contains(typeName) && !isPrimitive(typeName)) {
496 warning(context) << typeName << "is used as" << typeRelationString(relation)
497 << "type but cannot be found.";
498 }
499
500 processedRelatedNativeNames.insert(typeName);
501 processedRelatedJavaScriptNames.insert(typeName);
502 return false;
503 };
504
505
506
507 const auto addSupers = [&](const MetaType &context, const QList<QAnyStringView> &namespaces) {
508 for (const Interface &iface : context.ifaces())
509 addInterface(interfaceName(iface), namespaces);
510
511 // We don't warn about missing bases for value types. They don't have to be registered.
512 bool warnAboutSupers = context.kind() != MetaType::Kind::Gadget;
513
514 QList<QAnyStringView> missingSupers;
515
516 for (const BaseType &superObject : context.superClasses()) {
517 if (superObject.access != Access::Public)
518 continue;
519
520 QAnyStringView typeName = superObject.name;
521 if (doAddReferences(typeName, namespaces))
522 warnAboutSupers = false;
523 else
524 missingSupers.append(typeName);
525 }
526
527 for (QAnyStringView typeName : std::as_const(missingSupers)) {
528 // If we've found one valid base type, don't complain about the others.
529 if (warnAboutSupers
530 && !unresolvedForeignNames.contains(typeName)
531 && !isPrimitive(typeName)) {
532 warning(context) << typeName << "is used as base type but cannot be found.";
533 }
534
535 processedRelatedNativeNames.insert(typeName);
536 processedRelatedJavaScriptNames.insert(typeName);
537 }
538 };
539
540 const auto addEnums = [&](const MetaType &context,
541 const QList<QAnyStringView> &namespaces) {
542 for (const Enum &enumerator : context.enums()) {
543 ResolvedTypeAlias resolved(enumerator.type, m_usingDeclarations);
544 if (!resolved.type.isEmpty())
545 addType(context, resolved.type, namespaces, TypeRelation::Enum);
546 }
547 };
548
549 const auto addRelation = [&](const MetaType &classDef, const ClassInfo &obj,
550 const QList<QAnyStringView> &namespaces) {
551 const QAnyStringView objNameValue = obj.name;
552 if (objNameValue == S_ATTACHED) {
553 addType(classDef, obj.value, namespaces, TypeRelation::Attached);
554 return true;
555 } else if (objNameValue == S_SEQUENCE) {
556 ResolvedTypeAlias value(obj.value, m_usingDeclarations);
557 addType(classDef, value.type, namespaces, TypeRelation::SequenceValue);
558 return true;
559 } else if (objNameValue == S_EXTENDED) {
560 const QAnyStringView value = obj.value;
561 addType(classDef, value, namespaces, TypeRelation::Extension);
562 return true;
563 }
564 return false;
565 };
566
567 // Then recursively iterate the super types and attached types, marking the
568 // ones we are interested in as related.
569 while (!typeQueue.isEmpty()) {
570 QAnyStringView unresolvedForeign;
571
572 const MetaType classDef = typeQueue.dequeue();
573 const QList<QAnyStringView> namespaces = MetaTypesJsonProcessor::namespaces(classDef);
574
575 for (const ClassInfo &obj : classDef.classInfos()) {
576 if (addRelation(classDef, obj, namespaces))
577 continue;
578 if (obj.name != S_FOREIGN)
579 continue;
580
581 const QAnyStringView foreignClassName = obj.value;
582
583 // A type declared as QML_FOREIGN will usually be a foreign type, but it can
584 // actually be an additional registration of a local type, too.
585 if (const FoundType found = QmlTypesClassDescription::findType(
586 m_foreignTypes, {}, foreignClassName, namespaces)) {
587 const MetaType other = found.select(classDef, "Foreign");
588 const QList<QAnyStringView> otherNamespaces
589 = MetaTypesJsonProcessor::namespaces(other);
590 addSupers(other, otherNamespaces);
591 addEnums(other, otherNamespaces);
592
593 for (const ClassInfo &obj : other.classInfos()) {
594 if (addRelation(classDef, obj, otherNamespaces))
595 break;
596 // No, you cannot chain S_FOREIGN declarations. Sorry.
597 }
598
599 m_foreignTypeMetaObjectHashes.insert(classDef.qualifiedClassName(),
600 found.native.metaObjectHash());
601 } else if (!QmlTypesClassDescription::findType(
602 m_types, {}, foreignClassName, namespaces)) {
603 unresolvedForeign = foreignClassName;
604 }
605 }
606
607 if (!unresolvedForeign.isEmpty() && !isPrimitive(unresolvedForeign)) {
608 warning(classDef)
609 << unresolvedForeign
610 << "is declared as foreign type, but cannot be found.";
611 }
612
613 addSupers(classDef, namespaces);
614 addEnums(classDef, namespaces);
615 }
616}
617
618void MetaTypesJsonProcessor::sortTypes(QList<MetaType> &types)
619{
620 std::sort(types.begin(), types.end(), qualifiedClassNameLessThan);
621}
622
623QString MetaTypesJsonProcessor::resolvedInclude(QAnyStringView include)
624{
625 if (!m_privateIncludes)
626 return include.toString();
627
628 if (endsWith(include, "_p.h"_L1))
629 return QLatin1String("private/") + include.toString();
630
631 if (startsWith(include, "qplatform"_L1) || startsWith(include, "qwindowsystem"_L1))
632 return QLatin1String("qpa/") + include.toString();
633
634 return include.toString();
635}
636
637void MetaTypesJsonProcessor::processTypes(const QCborMap &types)
638{
639 const QString include = resolvedInclude(types[S_INPUT_FILE].toStringView());
640 const QCborArray classes = types[S_CLASSES].toArray();
641 const QCborMap hashes = types[S_HASHES].toMap();
642 for (const QCborValue &cls : classes) {
643 const MetaType classDef(cls.toMap(), include, hashes);
644
645 const PreProcessResult preprocessed = preProcess(classDef, PopulateMode::Yes);
646 switch (preprocessed.mode) {
647 case NamespaceRegistration:
648 case GadgetRegistration:
649 case ObjectRegistration: {
650 if (!endsWith(include, QLatin1String(".h"))
651 && !endsWith(include, QLatin1String(".hpp"))
652 && !endsWith(include, QLatin1String(".hxx"))
653 && !endsWith(include, QLatin1String(".hh"))
654 && !endsWith(include, QLatin1String(".py"))
655 && contains(include, QLatin1Char('.'))) {
656 warning(include)
657 << "Class" << classDef.qualifiedClassName()
658 << "is declared in" << include << "which appears not to be a header."
659 << "The compilation of its registration to QML may fail.";
660 }
661 m_includes.append(include);
662 m_types.emplaceBack(classDef);
663 break;
664 }
665 case NoRegistration:
666 m_foreignTypes.emplaceBack(classDef);
667 break;
668 }
669
670 if (!preprocessed.foreignPrimitive.isEmpty()) {
671 m_primitiveTypes.emplaceBack(preprocessed.foreignPrimitive);
672 m_primitiveTypes.append(preprocessed.primitiveAliases);
673 }
674
675 if (preprocessed.usingDeclaration.isValid())
676 m_usingDeclarations.append(preprocessed.usingDeclaration);
677 }
678}
679
680void MetaTypesJsonProcessor::processForeignTypes(const QCborMap &types)
681{
682 const QString include = resolvedInclude(types[S_INPUT_FILE].toStringView());
683 const QCborArray classes = types[S_CLASSES].toArray();
684 const QCborMap hashes = types[S_HASHES].toMap();
685 for (const QCborValue &cls : classes) {
686 const MetaType classDef(cls.toMap(), include, hashes);
687 PreProcessResult preprocessed = preProcess(classDef, PopulateMode::No);
688
689 m_foreignTypes.emplaceBack(classDef);
690 if (!preprocessed.foreignPrimitive.isEmpty()) {
691 m_primitiveTypes.emplaceBack(preprocessed.foreignPrimitive);
692 m_primitiveTypes.append(preprocessed.primitiveAliases);
693 }
694
695 if (preprocessed.usingDeclaration.isValid())
696 m_usingDeclarations.append(preprocessed.usingDeclaration);
697 }
698}
699
700static QTypeRevision getRevision(const QCborMap &cbor)
701{
702 const auto it = cbor.find(S_REVISION);
703 return it == cbor.end()
704 ? QTypeRevision()
705 : QTypeRevision::fromEncodedVersion(it->toInteger());
706}
707
708static Access getAccess(const QCborMap &cbor)
709{
710 const QAnyStringView access = cbor[S_ACCESS].toStringView();
711 if (access == S_PUBLIC)
712 return Access::Public;
713 if (access == S_PROTECTED)
714 return Access::Protected;
715 return Access::Private;
716}
717
718BaseType::BaseType(const QCborMap &cbor)
721{
722}
723
724ClassInfo::ClassInfo(const QCborMap &cbor)
727{
728}
729
730Interface::Interface(const QCborValue &cbor)
731{
732 if (cbor.isArray()) {
733 QCborArray needlessWrapping = cbor.toArray();
734 className = needlessWrapping.size() > 0
735 ? needlessWrapping[0].toMap()[S_CLASS_NAME].toStringView()
736 : QAnyStringView();
737 } else {
738 className = cbor.toMap()[S_CLASS_NAME].toStringView();
739 }
740}
741
742Property::Property(const QCborMap &cbor)
752 , index(cbor[S_INDEX].toInteger(-1))
753 , lineNumber(cbor[S_LINENUMBER].toInteger(0))
755 , isFinal(cbor[S_FINAL].toBool())
756 , isVirtual(cbor[S_VIRTUAL].toBool())
757 , isOverride(cbor[S_OVERRIDE].toBool())
758 , isConstant(cbor[S_CONSTANT].toBool())
759 , isRequired(cbor[S_REQUIRED].toBool())
760{
761}
762
763Argument::Argument(const QCborMap &cbor)
766{
767}
768
769Method::Method(const QCborMap &cbor, bool isConstructor)
772 , index(cbor[S_INDEX].toInteger(InvalidIndex))
773 , lineNumber(cbor[S_LINENUMBER].toInteger(0))
776 , isCloned(cbor[S_IS_CLONED].toBool())
777 , isJavaScriptFunction(cbor[S_IS_JAVASCRIPT_FUNCTION].toBool())
778 , isConstructor(isConstructor || cbor[S_IS_CONSTRUCTOR].toBool())
779 , isConst(cbor[S_IS_CONST].toBool())
780{
781 const QCborArray args = cbor[S_ARGUMENTS].toArray();
782 for (const QCborValue &argument : args)
783 arguments.emplace_back(argument.toMap());
784
785 if (arguments.size() == 1) {
786 const QAnyStringView type = arguments[0].type;
787 if (type == "QQmlV4FunctionPtr"_L1 || type == "QQmlV4Function*"_L1) {
789 arguments.clear();
790 }
791 }
792}
793
794Enum::Enum(const QCborMap &cbor)
798 , lineNumber(cbor[S_LINENUMBER].toInteger(0))
799 , isFlag(cbor[S_IS_FLAG].toBool())
800 , isClass(cbor[S_IS_CLASS].toBool())
801{
802 const QCborArray vals = cbor[S_VALUES].toArray();
803 for (const QCborValue &value : vals)
804 values.emplace_back(value.toStringView());
805}
806
807MetaTypePrivate::MetaTypePrivate(const QCborMap &cbor, const QString &inputFile,
808 const QCborMap &hashes)
809 : cbor(cbor)
810 , hashes(hashes)
812{
813 className = cbor[S_CLASS_NAME].toStringView();
814 lineNumber = cbor[S_LINENUMBER].toInteger(0);
815 const QCborValue &qualifiedClassNameCborValue = cbor[S_QUALIFIED_CLASS_NAME];
816 qualifiedClassName = qualifiedClassNameCborValue.toStringView();
817
818 const QCborArray cborSuperClasses = cbor[S_SUPER_CLASSES].toArray();
819 for (const QCborValue &superClass : cborSuperClasses)
820 superClasses.emplace_back(superClass.toMap());
821
822 const QCborArray cborClassInfos = cbor[S_CLASS_INFOS].toArray();
823 for (const QCborValue &classInfo : cborClassInfos)
824 classInfos.emplace_back(classInfo.toMap());
825
826 const QCborArray cborIfaces = cbor[S_INTERFACES].toArray();
827 for (const QCborValue &iface : cborIfaces)
828 ifaces.emplace_back(iface);
829
830 const QCborArray cborProperties = cbor[S_PROPERTIES].toArray();
831 for (const QCborValue &property : cborProperties)
832 properties.emplace_back(property.toMap());
833
834 for (const QCborArray &cborMethods : { cbor[S_SLOTS].toArray(), cbor[S_METHODS].toArray() }) {
835 for (const QCborValue &method : cborMethods)
836 methods.emplace_back(method.toMap(), false);
837 }
838
839 const QCborArray cborSigs = cbor[S_SIGNALS].toArray();
840 for (const QCborValue &sig : cborSigs)
841 sigs.emplace_back(sig.toMap(), false);
842
843 const QCborArray cborConstructors = cbor[S_CONSTRUCTORS].toArray();
844 for (const QCborValue &constructor : cborConstructors)
845 constructors.emplace_back(constructor.toMap(), true);
846
847 const QCborArray cborEnums = cbor[S_ENUMS].toArray();
848 for (const QCborValue &enumerator : cborEnums)
849 enums.emplace_back(enumerator.toMap());
850
851 if (cbor[S_GADGET].toBool())
852 kind = Kind::Gadget;
853 else if (cbor[S_OBJECT].toBool())
854 kind = Kind::Object;
855 else if (cbor[S_NAMESPACE].toBool())
856 kind = Kind::Namespace;
857
858 metaObjectHash = hashes.value(qualifiedClassNameCborValue).toStringView();
859}
860
861MetaType::MetaType(const QCborMap &cbor, const QString &inputFile, const QCborMap &hashes)
863{}
864
865QT_END_NAMESPACE
Access
Definition access.h:11
MetaType(const QCborMap &cbor, const QString &inputFile, const QCborMap &hashes)
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)
@ 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, const QCborMap &hashes)
Method(const QCborMap &cbor, bool isConstructor)
static constexpr int InvalidIndex
Property(const QCborMap &cbor)