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
qqmltypescreator.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 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
10
11#include <QtCore/qset.h>
12#include <QtCore/qcborarray.h>
13#include <QtCore/qcbormap.h>
14#include <QtCore/qsavefile.h>
15#include <QtCore/qfile.h>
16
17#include <QtCore/private/qstringalgorithms_p.h>
18
20
21using namespace Qt::StringLiterals;
22using namespace Constants;
23using namespace Constants::DotQmltypes;
24using namespace QAnyStringViewUtils;
25
27{
28 // typical privateClass entry in MOC looks like: ClassName::d_func(), where
29 // ClassName is a non-private class name. we don't need "::d_func()" piece
30 // so that could be removed, but we need "Private" so that ClassName becomes
31 // ClassNamePrivate (at present, simply consider this correct)
32 return s.toString().replace("::d_func()"_L1, "Private"_L1);
33}
34
35void QmlTypesCreator::writeClassProperties(const QmlTypesClassDescription &collector)
36{
37 if (!collector.file.isEmpty()) {
38 m_qml.writeStringBinding(S_FILE, collector.file);
39 m_qml.writeNumberBinding(S_LINE_NUMBER, collector.lineNumber);
40 }
41 if (!collector.resolvedFile.isEmpty()) {
42 m_qml.writeStringBinding(S_RESOLVED_FILE, collector.resolvedFile);
43 m_qml.writeNumberBinding(S_LINE_NUMBER_IN_RESOLVED_FILE,
44 collector.lineNumberInResolvedFile);
45 }
46 m_qml.writeStringBinding(S_NAME, collector.className);
47
48 if (!collector.primitiveAliases.isEmpty())
49 m_qml.writeStringListBinding(S_ALIASES, collector.primitiveAliases);
50
51 if (!collector.accessSemantics.isEmpty())
52 m_qml.writeStringBinding(S_ACCESS_SEMANTICS, collector.accessSemantics);
53
54 if (!collector.defaultProp.isEmpty())
55 m_qml.writeStringBinding(S_DEFAULT_PROPERTY, collector.defaultProp);
56
57 if (!collector.parentProp.isEmpty())
58 m_qml.writeStringBinding(S_PARENT_PROPERTY, collector.parentProp);
59
60 if (!collector.superClass.isEmpty())
61 m_qml.writeStringBinding(S_PROTOTYPE, collector.superClass);
62
63 if (!collector.sequenceValueType.isEmpty()) {
64 const QAnyStringView name = collector.sequenceValueType.back() == '*'_L1
65 ? collector.sequenceValueType.chopped(1)
66 : collector.sequenceValueType;
67 m_qml.writeStringBinding(S_VALUE_TYPE, name);
68 }
69
70 if (m_generatingJSRoot)
71 m_qml.writeBooleanBinding(S_IS_JAVASCRIPT_BUILTIN, true);
72
73 if (collector.extensionIsJavaScript) {
74 if (!collector.javaScriptExtensionType.isEmpty()) {
75 m_qml.writeStringBinding(S_EXTENSION, collector.javaScriptExtensionType);
76 m_qml.writeBooleanBinding(S_EXTENSION_IS_JAVA_SCRIPT, true);
77 } else {
78 warning(collector.file)
79 << "JavaScript extension type for" << collector.className
80 << "does not exist";
81 }
82
83 if (collector.extensionIsNamespace) {
84 warning(collector.file)
85 << "Extension type for" << collector.className
86 << "cannot be both a JavaScript type and a namespace";
87 if (!collector.nativeExtensionType.isEmpty()) {
88 m_qml.writeStringBinding(S_EXTENSION, collector.nativeExtensionType);
89 m_qml.writeBooleanBinding(S_EXTENSION_IS_NAMESPACE, true);
90 }
91 }
92 } else if (!collector.nativeExtensionType.isEmpty()) {
93 m_qml.writeStringBinding(S_EXTENSION, collector.nativeExtensionType);
94 if (collector.extensionIsNamespace)
95 m_qml.writeBooleanBinding(S_EXTENSION_IS_NAMESPACE, true);
96 } else if (collector.extensionIsNamespace) {
97 warning(collector.file)
98 << "Extension namespace for" << collector.className << "does not exist";
99 m_qml.writeBooleanBinding(S_EXTENSION_IS_NAMESPACE, true);
100 }
101 if (collector.isSelfExtension)
102 m_qml.writeBooleanBinding(S_IS_SELF_EXTENSION, true);
103
104 if (!collector.implementsInterfaces.isEmpty())
105 m_qml.writeStringListBinding(S_INTERFACES, collector.implementsInterfaces);
106
107 if (!collector.deferredNames.isEmpty())
108 m_qml.writeStringListBinding(S_DEFERRED_NAMES, collector.deferredNames);
109
110 if (!collector.immediateNames.isEmpty())
111 m_qml.writeStringListBinding(S_IMMEDIATE_NAMES, collector.immediateNames);
112
113 if (collector.elementNames.isEmpty()) // e.g. if QML_ANONYMOUS
114 return;
115
116 if (!collector.sequenceValueType.isEmpty()) {
117 warning(collector.file) << "Ignoring names of sequential container:";
118 for (const QAnyStringView &name : std::as_const(collector.elementNames))
119 warning(collector.file) << " - " << name.toString();
120 warning(collector.file)
121 << "Sequential containers are anonymous. Use QML_ANONYMOUS to register them.";
122 return;
123 }
124
125 QByteArrayList exports;
126 QByteArrayList metaObjects;
127
128 for (auto it = collector.revisions.begin(), end = collector.revisions.end(); it != end; ++it) {
129 const QTypeRevision revision = *it;
130 if (revision < collector.addedInRevision)
131 continue;
132 if (collector.removedInRevision.isValid() && !(revision < collector.removedInRevision))
133 break;
134 if (revision.hasMajorVersion() && revision.majorVersion() > m_version.majorVersion())
135 break;
136
137 for (const QAnyStringView &elementName : std::as_const(collector.elementNames)) {
138 QByteArray exportEntry = m_module + '/';
139
140 elementName.visit([&](auto view) {
141 processAsUtf8(view, [&](QByteArrayView view) { exportEntry.append(view); });
142 });
143 exportEntry += ' ' + QByteArray::number(revision.hasMajorVersion()
144 ? revision.majorVersion()
145 : m_version.majorVersion());
146 exportEntry += '.' + QByteArray::number(revision.minorVersion());
147
148 exports.append(exportEntry);
149 }
150 metaObjects.append(QByteArray::number(revision.toEncodedVersion<quint16>()));
151 }
152
153 QList<QAnyStringView> exportStrings;
154 exportStrings.reserve(exports.length());
155 for (const QByteArray &entry: exports)
156 exportStrings.append(QUtf8StringView(entry));
157
158 m_qml.writeStringListBinding(S_EXPORTS, exportStrings);
159
160 if (!collector.isCreatable || collector.isSingleton)
161 m_qml.writeBooleanBinding(S_IS_CREATABLE, false);
162
163 if (collector.isStructured)
164 m_qml.writeBooleanBinding(S_IS_STRUCTURED, true);
165
166 if (collector.isSingleton)
167 m_qml.writeBooleanBinding(S_IS_SINGLETON, true);
168
169 if (collector.hasCustomParser)
170 m_qml.writeBooleanBinding(S_HAS_CUSTOM_PARSER, true);
171
172 if (collector.enforcesScopedEnums)
173 m_qml.writeBooleanBinding(S_ENFORCES_SCOPED_ENUMS, true);
174
175 m_qml.writeArrayBinding(S_EXPORT_META_OBJECT_REVISIONS, metaObjects);
176
177 if (!collector.attachedType.isEmpty())
178 m_qml.writeStringBinding(S_ATTACHED_TYPE, collector.attachedType);
179}
180
181void QmlTypesCreator::writeType(QAnyStringView type)
182{
183 ResolvedTypeAlias resolved(type, m_usingDeclarations);
184 if (resolved.type.isEmpty())
185 return;
186
187 m_qml.writeStringBinding(S_TYPE, resolved.type);
188 if (resolved.isList)
189 m_qml.writeBooleanBinding(S_IS_LIST, true);
190 if (resolved.isPointer)
191 m_qml.writeBooleanBinding(S_IS_POINTER, true);
192 if (resolved.isConstant)
193 m_qml.writeBooleanBinding(S_IS_TYPE_CONSTANT, true);
194}
195
196void QmlTypesCreator::writeProperties(const Property::Container &properties)
197{
198 for (const Property &obj : properties) {
199 const QAnyStringView name = obj.name;
200 m_qml.writeStartObject(S_PROPERTY);
201 m_qml.writeStringBinding(S_NAME, name);
202 if (obj.revision.isValid())
203 m_qml.writeNumberBinding(S_REVISION, obj.revision.toEncodedVersion<int>());
204
205 writeType(obj.type);
206
207 const auto bindable = obj.bindable;
208 if (!bindable.isEmpty())
209 m_qml.writeStringBinding(S_BINDABLE, bindable);
210 const auto read = obj.read;
211 if (!read.isEmpty())
212 m_qml.writeStringBinding(S_READ, read);
213 const auto write = obj.write;
214 if (!write.isEmpty())
215 m_qml.writeStringBinding(S_WRITE, write);
216 const auto reset = obj.reset;
217 if (!reset.isEmpty())
218 m_qml.writeStringBinding(S_RESET, reset);
219 const auto notify = obj.notify;
220 if (!notify.isEmpty())
221 m_qml.writeStringBinding(S_NOTIFY, notify);
222 const auto index = obj.index;
223 if (index != -1) {
224 m_qml.writeNumberBinding(S_INDEX, index);
225 }
226 const auto lineNumber = obj.lineNumber;
227 if (lineNumber != 0)
228 m_qml.writeNumberBinding(S_LINE_NUMBER, obj.lineNumber);
229
230 const auto privateClass = obj.privateClass;
231 if (!privateClass.isEmpty()) {
232 m_qml.writeStringBinding(
233 S_PRIVATE_CLASS, convertPrivateClassToUsableForm(privateClass));
234 }
235
236 if (obj.write.isEmpty() && obj.member.isEmpty())
237 m_qml.writeBooleanBinding(S_IS_READONLY, true);
238
239 if (obj.isFinal)
240 m_qml.writeBooleanBinding(S_IS_FINAL, true);
241 if (obj.isVirtual)
242 m_qml.writeBooleanBinding(S_IS_VIRTUAL, true);
243 if (obj.isOverride)
244 m_qml.writeBooleanBinding(S_IS_OVERRIDE, true);
245
246 if (obj.isConstant)
247 m_qml.writeBooleanBinding(S_IS_PROPERTY_CONSTANT, true);
248
249 if (obj.isRequired)
250 m_qml.writeBooleanBinding(S_IS_REQUIRED, true);
251
252 m_qml.writeEndObject();
253 }
254}
255
256void QmlTypesCreator::writeMethods(const Method::Container &methods, QLatin1StringView type)
257{
258 for (const Method &obj : methods) {
259 const QAnyStringView name = obj.name;
260 if (name.isEmpty())
261 continue;
262
263 const auto revision = obj.revision;
264 m_qml.writeStartObject(type);
265 m_qml.writeStringBinding(S_NAME, name);
266 if (revision.isValid())
267 m_qml.writeNumberBinding(S_REVISION, revision.toEncodedVersion<int>());
268 writeType(obj.returnType);
269
270 if (obj.isCloned)
271 m_qml.writeBooleanBinding(S_IS_CLONED, true);
272 if (obj.isConstructor)
273 m_qml.writeBooleanBinding(S_IS_CONSTRUCTOR, true);
274 if (obj.isJavaScriptFunction)
275 m_qml.writeBooleanBinding(S_IS_JAVASCRIPT_FUNCTION, true);
276 if (obj.isConst)
277 m_qml.writeBooleanBinding(S_IS_METHOD_CONSTANT, true);
278 const auto lineNumber = obj.lineNumber;
279 if (lineNumber != 0)
280 m_qml.writeNumberBinding(S_LINE_NUMBER, obj.lineNumber);
281
282 const Argument::Container &arguments = obj.arguments;
283 for (qsizetype i = 0, end = arguments.size(); i != end; ++i) {
284 const Argument &obj = arguments[i];
285 m_qml.writeStartObject(S_PARAMETER);
286 const QAnyStringView name = obj.name;
287 if (!name.isEmpty())
288 m_qml.writeStringBinding(S_NAME, name);
289 writeType(obj.type);
290 m_qml.writeEndObject();
291 }
292 m_qml.writeEndObject();
293 }
294}
295
296void QmlTypesCreator::writeEnums(const Enum::Container &enums)
297{
298 for (const Enum &obj : enums) {
299 m_qml.writeStartObject(S_ENUM);
300 m_qml.writeStringBinding(S_NAME, obj.name);
301 if (!obj.alias.isEmpty())
302 m_qml.writeStringBinding(S_ALIAS, obj.alias);
303 if (obj.isFlag)
304 m_qml.writeBooleanBinding(S_IS_FLAG, true);
305 if (obj.isClass)
306 m_qml.writeBooleanBinding(S_IS_SCOPED, true);
307 writeType(obj.type);
308 const auto lineNumber = obj.lineNumber;
309 if (lineNumber != 0)
310 m_qml.writeNumberBinding(S_LINE_NUMBER, obj.lineNumber);
311 m_qml.writeStringListBinding(S_VALUES, obj.values);
312 m_qml.writeEndObject();
313 }
314}
315
316void QmlTypesCreator::writeRootMethods(const MetaType &classDef)
317{
318 // Hide destroyed() signals
319 Method::Container componentSignals = classDef.sigs();
320 for (auto it = componentSignals.begin(); it != componentSignals.end();) {
321 if (it->name == "destroyed"_L1)
322 it = componentSignals.erase(it);
323 else
324 ++it;
325 }
326 writeMethods(componentSignals, S_SIGNAL);
327
328 // Hide deleteLater() methods
329 Method::Container componentMethods = classDef.methods();
330 for (auto it = componentMethods.begin(); it != componentMethods.end();) {
331 if (it->name == "deleteLater"_L1)
332 it = componentMethods.erase(it);
333 else
334 ++it;
335 }
336
337 // Add toString()
338 Method toStringMethod;
339 toStringMethod.index = -2; // See QV4::QObjectMethod
340 toStringMethod.name = "toString"_L1;
341 toStringMethod.access = Access::Public;
342 toStringMethod.returnType = "QString"_L1;
343 toStringMethod.isConst = true;
344 componentMethods.push_back(std::move(toStringMethod));
345
346 // Add destroy(int)
347 Method destroyMethodWithArgument;
348 destroyMethodWithArgument.index = -1; // See QV4::QObjectMethod
349 destroyMethodWithArgument.name = "destroy"_L1;
350 destroyMethodWithArgument.access = Access::Public;
351 Argument delayArgument;
352 delayArgument.name = "delay"_L1;
353 delayArgument.type = "int"_L1;
354 destroyMethodWithArgument.arguments.push_back(std::move(delayArgument));
355 componentMethods.push_back(std::move(destroyMethodWithArgument));
356
357 // Add destroy()
358 Method destroyMethod;
359 destroyMethod.index = -1; // See QV4::QObjectMethod
360 destroyMethod.name = "destroy"_L1;
361 destroyMethod.access = Access::Public;
362 destroyMethod.isCloned = true;
363 componentMethods.push_back(std::move(destroyMethod));
364
365 writeMethods(componentMethods, S_METHOD);
366};
367
368void QmlTypesCreator::writeComponent(const QmlTypesClassDescription &collector)
369{
370 m_qml.writeStartObject(S_COMPONENT);
371
372 writeClassProperties(collector);
373
374 if (const MetaType &classDef = collector.resolvedClass; !classDef.isEmpty()) {
375 writeEnums(classDef.enums());
376 writeProperties(classDef.properties());
377
378 if (collector.isRootClass) {
379 writeRootMethods(classDef);
380 } else {
381 writeMethods(classDef.sigs(), S_SIGNAL);
382 writeMethods(classDef.methods(), S_METHOD);
383 }
384
385 writeMethods(classDef.constructors(), S_METHOD);
386 }
387 m_qml.writeEndObject();
388}
389
390void QmlTypesCreator::writeOpaqueComponent(const QmlTypesClassDescription &collector)
391{
392 m_qml.writeStartObject(S_COMPONENT);
393
394 // we always mark the type as opaque, even if we have some data
395 m_qml.writeBooleanBinding(S_IS_TYPE_OPAQUE, true);
396 // compare writeClassProperties
397 if (!collector.file.isEmpty()) {
398 m_qml.writeStringBinding(S_FILE, collector.file);
399 m_qml.writeNumberBinding(S_LINE_NUMBER, collector.lineNumber);
400 }
401 m_qml.writeStringBinding(S_NAME, collector.className);
402 m_qml.writeStringBinding(S_ACCESS_SEMANTICS, collector.accessSemantics); // restrict instead?
403 // there's no sensible use case for defaultProp and parentProp
404 // as the type is not creatable
405 if (!collector.superClass.isEmpty())
406 m_qml.writeStringBinding(S_PROTOTYPE, collector.superClass);
407 // can't be JSRoot or extension, basically an anonymous type
408
409 if (const MetaType &classDef = collector.resolvedClass; !classDef.isEmpty()) {
410 writeEnums(classDef.enums());
411 writeProperties(classDef.properties());
412 writeMethods(classDef.sigs(), S_SIGNAL);
413 writeMethods(classDef.methods(), S_METHOD);
414 writeMethods(classDef.constructors(), S_METHOD);
415 }
416
417 m_qml.writeEndObject();
418}
419
420void QmlTypesCreator::writeComponents()
421{
422 for (const MetaType &component : std::as_const(m_ownTypes)) {
423 QmlTypesClassDescription collector;
424 collector.collect(component, m_ownTypes, m_foreignTypes,
425 QmlTypesClassDescription::TopLevel, m_version);
426
427 writeComponent(collector);
428
429 if (collector.resolvedClass != component
430 && std::binary_search(
431 m_referencedTypes.begin(), m_referencedTypes.end(),
432 component.qualifiedClassName())) {
433
434 // This type is referenced from elsewhere and has a QML_FOREIGN of its own. We need to
435 // also generate a description of the local type then. All the QML_* macros are
436 // ignored, and the result is an anonymous type.
437
438 QmlTypesClassDescription collector;
439 collector.collectLocalAnonymous(component, m_ownTypes, m_foreignTypes, m_version);
440 Q_ASSERT(!collector.isRootClass);
441
442 writeComponent(collector);
443 }
444 }
445 for (const MetaType &component : std::as_const(m_opaqueTypes)) {
446 QmlTypesClassDescription collector;
447 collector.collect(component, m_ownTypes, m_foreignTypes,
448 QmlTypesClassDescription::TopLevel, m_version);
449
450 writeOpaqueComponent(collector);
451 }
452}
453
454bool QmlTypesCreator::generate(const QString &outFileName)
455{
456 m_qml.writeStartDocument();
457 m_qml.writeLibraryImport("QtQuick.tooling", 1, 2);
458 m_qml.write(
459 "\n// This file describes the plugin-supplied types contained in the library."
460 "\n// It is used for QML tooling purposes only."
461 "\n//"
462 "\n// This file was auto-generated by qmltyperegistrar.\n\n");
463 m_qml.writeStartObject(S_MODULE);
464
465 writeComponents();
466
467 m_qml.writeEndObject();
468
469 QSaveFile file(outFileName);
470 if (!file.open(QIODevice::WriteOnly)) {
471 qCritical().nospace() << "Error: Failed to open " << file.fileName()
472 << " for writing: " << file.errorString();
473 return false;
474 }
475
476 if (file.write(m_output) != m_output.size())
477 return false;
478
479 return file.commit();
480}
481
482QT_END_NAMESPACE
bool generate(const QString &outFileName)
Combined button and popup list for selecting options.
static QString convertPrivateClassToUsableForm(QAnyStringView s)