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