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