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