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