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