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 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 if (obj.isVirtual)
235 m_qml.writeBooleanBinding(S_IS_VIRTUAL, true);
236 if (obj.isOverride)
237 m_qml.writeBooleanBinding(S_IS_OVERRIDE, true);
238
239 if (obj.isConstant)
240 m_qml.writeBooleanBinding(S_IS_PROPERTY_CONSTANT, true);
241
242 if (obj.isRequired)
243 m_qml.writeBooleanBinding(S_IS_REQUIRED, true);
244
245 m_qml.writeEndObject();
246 }
247}
248
249void QmlTypesCreator::writeMethods(const Method::Container &methods, QLatin1StringView type)
250{
251 for (const Method &obj : methods) {
252 const QAnyStringView name = obj.name;
253 if (name.isEmpty())
254 continue;
255
256 const auto revision = obj.revision;
257 m_qml.writeStartObject(type);
258 m_qml.writeStringBinding(S_NAME, name);
259 if (revision.isValid())
260 m_qml.writeNumberBinding(S_REVISION, revision.toEncodedVersion<int>());
261 writeType(obj.returnType);
262
263 if (obj.isCloned)
264 m_qml.writeBooleanBinding(S_IS_CLONED, true);
265 if (obj.isConstructor)
266 m_qml.writeBooleanBinding(S_IS_CONSTRUCTOR, true);
267 if (obj.isJavaScriptFunction)
268 m_qml.writeBooleanBinding(S_IS_JAVASCRIPT_FUNCTION, true);
269 if (obj.isConst)
270 m_qml.writeBooleanBinding(S_IS_METHOD_CONSTANT, true);
271 const auto lineNumber = obj.lineNumber;
272 if (lineNumber != 0)
273 m_qml.writeNumberBinding(S_LINE_NUMBER, obj.lineNumber);
274
275 const Argument::Container &arguments = obj.arguments;
276 for (qsizetype i = 0, end = arguments.size(); i != end; ++i) {
277 const Argument &obj = arguments[i];
278 m_qml.writeStartObject(S_PARAMETER);
279 const QAnyStringView name = obj.name;
280 if (!name.isEmpty())
281 m_qml.writeStringBinding(S_NAME, name);
282 writeType(obj.type);
283 m_qml.writeEndObject();
284 }
285 m_qml.writeEndObject();
286 }
287}
288
289void QmlTypesCreator::writeEnums(const Enum::Container &enums)
290{
291 for (const Enum &obj : enums) {
292 m_qml.writeStartObject(S_ENUM);
293 m_qml.writeStringBinding(S_NAME, obj.name);
294 if (!obj.alias.isEmpty())
295 m_qml.writeStringBinding(S_ALIAS, obj.alias);
296 if (obj.isFlag)
297 m_qml.writeBooleanBinding(S_IS_FLAG, true);
298 if (obj.isClass)
299 m_qml.writeBooleanBinding(S_IS_SCOPED, true);
300 writeType(obj.type);
301 const auto lineNumber = obj.lineNumber;
302 if (lineNumber != 0)
303 m_qml.writeNumberBinding(S_LINE_NUMBER, obj.lineNumber);
304 m_qml.writeStringListBinding(S_VALUES, obj.values);
305 m_qml.writeEndObject();
306 }
307}
308
309void QmlTypesCreator::writeRootMethods(const MetaType &classDef)
310{
311 // Hide destroyed() signals
312 Method::Container componentSignals = classDef.sigs();
313 for (auto it = componentSignals.begin(); it != componentSignals.end();) {
314 if (it->name == "destroyed"_L1)
315 it = componentSignals.erase(it);
316 else
317 ++it;
318 }
319 writeMethods(componentSignals, S_SIGNAL);
320
321 // Hide deleteLater() methods
322 Method::Container componentMethods = classDef.methods();
323 for (auto it = componentMethods.begin(); it != componentMethods.end();) {
324 if (it->name == "deleteLater"_L1)
325 it = componentMethods.erase(it);
326 else
327 ++it;
328 }
329
330 // Add toString()
331 Method toStringMethod;
332 toStringMethod.index = -2; // See QV4::QObjectMethod
333 toStringMethod.name = "toString"_L1;
334 toStringMethod.access = Access::Public;
335 toStringMethod.returnType = "QString"_L1;
336 toStringMethod.isConst = true;
337 componentMethods.push_back(std::move(toStringMethod));
338
339 // Add destroy(int)
340 Method destroyMethodWithArgument;
341 destroyMethodWithArgument.index = -1; // See QV4::QObjectMethod
342 destroyMethodWithArgument.name = "destroy"_L1;
343 destroyMethodWithArgument.access = Access::Public;
344 Argument delayArgument;
345 delayArgument.name = "delay"_L1;
346 delayArgument.type = "int"_L1;
347 destroyMethodWithArgument.arguments.push_back(std::move(delayArgument));
348 componentMethods.push_back(std::move(destroyMethodWithArgument));
349
350 // Add destroy()
351 Method destroyMethod;
352 destroyMethod.index = -1; // See QV4::QObjectMethod
353 destroyMethod.name = "destroy"_L1;
354 destroyMethod.access = Access::Public;
355 destroyMethod.isCloned = true;
356 componentMethods.push_back(std::move(destroyMethod));
357
358 writeMethods(componentMethods, S_METHOD);
359};
360
361void QmlTypesCreator::writeComponent(const QmlTypesClassDescription &collector)
362{
363 m_qml.writeStartObject(S_COMPONENT);
364
365 writeClassProperties(collector);
366
367 if (const MetaType &classDef = collector.resolvedClass; !classDef.isEmpty()) {
368 writeEnums(classDef.enums());
369 writeProperties(classDef.properties());
370
371 if (collector.isRootClass) {
372 writeRootMethods(classDef);
373 } else {
374 writeMethods(classDef.sigs(), S_SIGNAL);
375 writeMethods(classDef.methods(), S_METHOD);
376 }
377
378 writeMethods(classDef.constructors(), S_METHOD);
379 }
380 m_qml.writeEndObject();
381}
382
383void QmlTypesCreator::writeComponents()
384{
385 for (const MetaType &component : std::as_const(m_ownTypes)) {
386 QmlTypesClassDescription collector;
387 collector.collect(component, m_ownTypes, m_foreignTypes,
388 QmlTypesClassDescription::TopLevel, m_version);
389
390 writeComponent(collector);
391
392 if (collector.resolvedClass != component
393 && std::binary_search(
394 m_referencedTypes.begin(), m_referencedTypes.end(),
395 component.qualifiedClassName())) {
396
397 // This type is referenced from elsewhere and has a QML_FOREIGN of its own. We need to
398 // also generate a description of the local type then. All the QML_* macros are
399 // ignored, and the result is an anonymous type.
400
401 QmlTypesClassDescription collector;
402 collector.collectLocalAnonymous(component, m_ownTypes, m_foreignTypes, m_version);
403 Q_ASSERT(!collector.isRootClass);
404
405 writeComponent(collector);
406 }
407 }
408}
409
410bool QmlTypesCreator::generate(const QString &outFileName)
411{
412 m_qml.writeStartDocument();
413 m_qml.writeLibraryImport("QtQuick.tooling", 1, 2);
414 m_qml.write(
415 "\n// This file describes the plugin-supplied types contained in the library."
416 "\n// It is used for QML tooling purposes only."
417 "\n//"
418 "\n// This file was auto-generated by qmltyperegistrar.\n\n");
419 m_qml.writeStartObject(S_MODULE);
420
421 writeComponents();
422
423 m_qml.writeEndObject();
424
425 QSaveFile file(outFileName);
426 if (!file.open(QIODevice::WriteOnly)) {
427 qCritical().nospace() << "Error: Failed to open " << file.fileName()
428 << " for writing: " << file.errorString();
429 return false;
430 }
431
432 if (file.write(m_output) != m_output.size())
433 return false;
434
435 return file.commit();
436}
437
438QT_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)