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