| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qqmltypecompiler_p.h" |
| 5 | |
| 6 | #include <private/qqmlobjectcreator_p.h> |
| 7 | #include <private/qqmlcustomparser_p.h> |
| 8 | #include <private/qqmlvmemetaobject_p.h> |
| 9 | #include <private/qqmlcomponent_p.h> |
| 10 | #include <private/qqmlpropertyresolver_p.h> |
| 11 | #include <private/qqmlcomponentandaliasresolver_p.h> |
| 12 | #include <private/qqmlsignalnames_p.h> |
| 13 | |
| 14 | #define COMPILE_EXCEPTION(token, desc) \ |
| 15 | { \ |
| 16 | recordError((token)->location, desc); \ |
| 17 | return false; \ |
| 18 | } |
| 19 | |
| 20 | QT_BEGIN_NAMESPACE |
| 21 | |
| 22 | DEFINE_BOOL_CONFIG_OPTION( |
| 23 | disableInternalDeferredProperties, QML_DISABLE_INTERNAL_DEFERRED_PROPERTIES); |
| 24 | |
| 25 | Q_LOGGING_CATEGORY(lcQmlTypeCompiler, "qt.qml.typecompiler" ); |
| 26 | |
| 27 | QQmlTypeCompiler::QQmlTypeCompiler( |
| 28 | QQmlTypeLoader *typeLoader, QQmlTypeData *typeData, QmlIR::Document *parsedQML, |
| 29 | QV4::CompiledData::ResolvedTypeReferenceMap *resolvedTypeCache, |
| 30 | const QV4::CompiledData::DependentTypesHasher &dependencyHasher) |
| 31 | : resolvedTypes(resolvedTypeCache) |
| 32 | , loader(typeLoader) |
| 33 | , dependencyHasher(dependencyHasher) |
| 34 | , document(parsedQML) |
| 35 | , typeData(typeData) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | QQmlRefPointer<QV4::CompiledData::CompilationUnit> QQmlTypeCompiler::compile() |
| 40 | { |
| 41 | // Build property caches and VME meta object data |
| 42 | |
| 43 | for (auto it = resolvedTypes->constBegin(), end = resolvedTypes->constEnd(); |
| 44 | it != end; ++it) { |
| 45 | QQmlCustomParser *customParser = (*it)->type().customParser(); |
| 46 | if (customParser) |
| 47 | customParsers.insert(key: it.key(), value: customParser); |
| 48 | } |
| 49 | |
| 50 | QQmlPendingGroupPropertyBindings pendingGroupPropertyBindings; |
| 51 | |
| 52 | |
| 53 | { |
| 54 | QQmlPropertyCacheCreator<QQmlTypeCompiler> propertyCacheBuilder( |
| 55 | &m_propertyCaches, &pendingGroupPropertyBindings, |
| 56 | loader, this, imports(), typeData->typeClassName()); |
| 57 | QQmlError cycleError = propertyCacheBuilder.verifyNoICCycle(); |
| 58 | if (cycleError.isValid()) { |
| 59 | recordError(e: cycleError); |
| 60 | return nullptr; |
| 61 | } |
| 62 | QQmlPropertyCacheCreatorBase::IncrementalResult result; |
| 63 | do { |
| 64 | result = propertyCacheBuilder.buildMetaObjectsIncrementally(); |
| 65 | const QQmlError &error = result.error; |
| 66 | if (error.isValid()) { |
| 67 | recordError(e: error); |
| 68 | return nullptr; |
| 69 | } else { |
| 70 | // Resolve component boundaries and aliases |
| 71 | |
| 72 | QQmlComponentAndAliasResolver resolver(this, &m_propertyCaches); |
| 73 | if (QQmlError error = resolver.resolve(root: result.processedRoot); error.isValid()) { |
| 74 | recordError(e: error); |
| 75 | return nullptr; |
| 76 | } |
| 77 | pendingGroupPropertyBindings.resolveMissingPropertyCaches(propertyCaches: &m_propertyCaches); |
| 78 | pendingGroupPropertyBindings.clear(); // anything that can be processed is now processed |
| 79 | } |
| 80 | } while (result.canResume); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | QQmlDefaultPropertyMerger merger(this); |
| 85 | merger.mergeDefaultProperties(); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | SignalHandlerResolver converter(this); |
| 90 | if (!converter.resolveSignalHandlerExpressions()) |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | { |
| 95 | QQmlEnumTypeResolver enumResolver(this); |
| 96 | if (!enumResolver.resolveEnumBindings()) |
| 97 | return nullptr; |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | QQmlCustomParserScriptIndexer cpi(this); |
| 102 | cpi.annotateBindingsWithScriptStrings(); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | QQmlAliasAnnotator annotator(this); |
| 107 | annotator.annotateBindingsToAliases(); |
| 108 | } |
| 109 | |
| 110 | { |
| 111 | QQmlDeferredAndCustomParserBindingScanner deferredAndCustomParserBindingScanner(this); |
| 112 | if (!deferredAndCustomParserBindingScanner.scanObject()) |
| 113 | return nullptr; |
| 114 | } |
| 115 | |
| 116 | if (!document->javaScriptCompilationUnit || !document->javaScriptCompilationUnit->unitData()) { |
| 117 | // Compile JS binding expressions and signal handlers if necessary |
| 118 | { |
| 119 | // We can compile script strings ahead of time, but they must be compiled |
| 120 | // without type optimizations as their scope is always entirely dynamic. |
| 121 | QQmlScriptStringScanner sss(this); |
| 122 | sss.scan(); |
| 123 | } |
| 124 | |
| 125 | Q_ASSERT(document->jsModule.fileName == typeData->urlString()); |
| 126 | Q_ASSERT(document->jsModule.finalUrl == typeData->finalUrlString()); |
| 127 | QmlIR::JSCodeGen v4CodeGenerator(document); |
| 128 | for (QmlIR::Object *object : std::as_const(t&: document->objects)) { |
| 129 | if (!v4CodeGenerator.generateRuntimeFunctions(object)) { |
| 130 | Q_ASSERT(v4CodeGenerator.hasError()); |
| 131 | recordError(message: v4CodeGenerator.error()); |
| 132 | return nullptr; |
| 133 | } |
| 134 | } |
| 135 | document->javaScriptCompilationUnit = v4CodeGenerator.generateCompilationUnit(/*generated unit data*/generateUnitData: false); |
| 136 | } |
| 137 | |
| 138 | // Generate QML compiled type data structures |
| 139 | |
| 140 | QmlIR::QmlUnitGenerator qmlGenerator; |
| 141 | qmlGenerator.generate(output&: *document, dependencyHasher); |
| 142 | |
| 143 | if (!errors.isEmpty()) |
| 144 | return nullptr; |
| 145 | |
| 146 | return std::move(document->javaScriptCompilationUnit); |
| 147 | } |
| 148 | |
| 149 | void QQmlTypeCompiler::recordError(const QV4::CompiledData::Location &location, const QString &description) |
| 150 | { |
| 151 | QQmlError error; |
| 152 | error.setLine(qmlConvertSourceCoordinate<quint32, int>(n: location.line())); |
| 153 | error.setColumn(qmlConvertSourceCoordinate<quint32, int>(n: location.column())); |
| 154 | error.setDescription(description); |
| 155 | error.setUrl(url()); |
| 156 | errors << error; |
| 157 | } |
| 158 | |
| 159 | void QQmlTypeCompiler::recordError(const QQmlJS::DiagnosticMessage &message) |
| 160 | { |
| 161 | QQmlError error; |
| 162 | error.setDescription(message.message); |
| 163 | error.setLine(qmlConvertSourceCoordinate<quint32, int>(n: message.loc.startLine)); |
| 164 | error.setColumn(qmlConvertSourceCoordinate<quint32, int>(n: message.loc.startColumn)); |
| 165 | error.setUrl(url()); |
| 166 | errors << error; |
| 167 | } |
| 168 | |
| 169 | void QQmlTypeCompiler::recordError(const QQmlError &e) |
| 170 | { |
| 171 | QQmlError error = e; |
| 172 | error.setUrl(url()); |
| 173 | errors << error; |
| 174 | } |
| 175 | |
| 176 | QString QQmlTypeCompiler::stringAt(int idx) const |
| 177 | { |
| 178 | return document->stringAt(index: idx); |
| 179 | } |
| 180 | |
| 181 | int QQmlTypeCompiler::registerString(const QString &str) |
| 182 | { |
| 183 | return document->jsGenerator.registerString(str); |
| 184 | } |
| 185 | |
| 186 | int QQmlTypeCompiler::registerConstant(QV4::ReturnedValue v) |
| 187 | { |
| 188 | return document->jsGenerator.registerConstant(v); |
| 189 | } |
| 190 | |
| 191 | const QV4::CompiledData::Unit *QQmlTypeCompiler::qmlUnit() const |
| 192 | { |
| 193 | return document->javaScriptCompilationUnit->unitData(); |
| 194 | } |
| 195 | |
| 196 | const QQmlImports *QQmlTypeCompiler::imports() const |
| 197 | { |
| 198 | return typeData->imports(); |
| 199 | } |
| 200 | |
| 201 | QVector<QmlIR::Object *> *QQmlTypeCompiler::qmlObjects() const |
| 202 | { |
| 203 | return &document->objects; |
| 204 | } |
| 205 | |
| 206 | QQmlPropertyCacheVector *QQmlTypeCompiler::propertyCaches() |
| 207 | { |
| 208 | return &m_propertyCaches; |
| 209 | } |
| 210 | |
| 211 | const QQmlPropertyCacheVector *QQmlTypeCompiler::propertyCaches() const |
| 212 | { |
| 213 | return &m_propertyCaches; |
| 214 | } |
| 215 | |
| 216 | QQmlJS::MemoryPool *QQmlTypeCompiler::memoryPool() |
| 217 | { |
| 218 | return document->jsParserEngine.pool(); |
| 219 | } |
| 220 | |
| 221 | QStringView QQmlTypeCompiler::newStringRef(const QString &string) |
| 222 | { |
| 223 | return document->jsParserEngine.newStringRef(text: string); |
| 224 | } |
| 225 | |
| 226 | const QV4::Compiler::StringTableGenerator *QQmlTypeCompiler::stringPool() const |
| 227 | { |
| 228 | return &document->jsGenerator.stringTable; |
| 229 | } |
| 230 | |
| 231 | QString QQmlTypeCompiler::bindingAsString(const QmlIR::Object *object, int scriptIndex) const |
| 232 | { |
| 233 | return object->bindingAsString(doc: document, scriptIndex); |
| 234 | } |
| 235 | |
| 236 | void QQmlTypeCompiler::addImport(const QString &module, const QString &qualifier, QTypeRevision version) |
| 237 | { |
| 238 | const quint32 moduleIdx = registerString(str: module); |
| 239 | const quint32 qualifierIdx = registerString(str: qualifier); |
| 240 | |
| 241 | for (int i = 0, count = document->imports.size(); i < count; ++i) { |
| 242 | const QV4::CompiledData::Import *existingImport = document->imports.at(i); |
| 243 | if (existingImport->type == QV4::CompiledData::Import::ImportLibrary |
| 244 | && existingImport->uriIndex == moduleIdx |
| 245 | && existingImport->qualifierIndex == qualifierIdx) |
| 246 | return; |
| 247 | } |
| 248 | auto pool = memoryPool(); |
| 249 | QV4::CompiledData::Import *import = pool->New<QV4::CompiledData::Import>(); |
| 250 | import->type = QV4::CompiledData::Import::ImportLibrary; |
| 251 | import->version = version; |
| 252 | import->uriIndex = moduleIdx; |
| 253 | import->qualifierIndex = qualifierIdx; |
| 254 | document->imports.append(t: import); |
| 255 | } |
| 256 | |
| 257 | QQmlType QQmlTypeCompiler::qmlTypeForComponent(const QString &inlineComponentName) const |
| 258 | { |
| 259 | return typeData->qmlType(inlineComponentName); |
| 260 | } |
| 261 | |
| 262 | QQmlCompilePass::QQmlCompilePass(QQmlTypeCompiler *typeCompiler) |
| 263 | : compiler(typeCompiler) |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | SignalHandlerResolver::SignalHandlerResolver(QQmlTypeCompiler *typeCompiler) |
| 268 | : QQmlCompilePass(typeCompiler) |
| 269 | , typeLoader(typeCompiler->typeLoader()) |
| 270 | , qmlObjects(*typeCompiler->qmlObjects()) |
| 271 | , imports(typeCompiler->imports()) |
| 272 | , customParsers(typeCompiler->customParserCache()) |
| 273 | , propertyCaches(typeCompiler->propertyCaches()) |
| 274 | { |
| 275 | } |
| 276 | |
| 277 | bool SignalHandlerResolver::resolveSignalHandlerExpressions() |
| 278 | { |
| 279 | for (int objectIndex = 0; objectIndex < qmlObjects.size(); ++objectIndex) { |
| 280 | const QmlIR::Object * const obj = qmlObjects.at(i: objectIndex); |
| 281 | QQmlPropertyCache::ConstPtr cache = propertyCaches->at(index: objectIndex); |
| 282 | if (!cache) |
| 283 | continue; |
| 284 | if (QQmlCustomParser *customParser = customParsers.value(key: obj->inheritedTypeNameIndex)) { |
| 285 | if (!(customParser->flags() & QQmlCustomParser::AcceptsSignalHandlers)) |
| 286 | continue; |
| 287 | } |
| 288 | const QString elementName = stringAt(idx: obj->inheritedTypeNameIndex); |
| 289 | if (!resolveSignalHandlerExpressions(obj, typeName: elementName, propertyCache: cache)) |
| 290 | return false; |
| 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool SignalHandlerResolver::resolveSignalHandlerExpressions( |
| 296 | const QmlIR::Object *obj, const QString &typeName, |
| 297 | const QQmlPropertyCache::ConstPtr &propertyCache, |
| 298 | QQmlPropertyResolver::RevisionCheck checkRevision) |
| 299 | { |
| 300 | // map from signal name defined in qml itself to list of parameters |
| 301 | QHash<QString, QStringList> customSignals; |
| 302 | |
| 303 | for (QmlIR::Binding *binding = obj->firstBinding(); binding; binding = binding->next) { |
| 304 | const QString bindingPropertyName = stringAt(idx: binding->propertyNameIndex); |
| 305 | // Attached property? |
| 306 | const QV4::CompiledData::Binding::Type bindingType = binding->type(); |
| 307 | if (bindingType == QV4::CompiledData::Binding::Type_AttachedProperty) { |
| 308 | const QmlIR::Object *attachedObj = qmlObjects.at(i: binding->value.objectIndex); |
| 309 | auto *typeRef = resolvedType(id: binding->propertyNameIndex); |
| 310 | QQmlType type = typeRef ? typeRef->type() : QQmlType(); |
| 311 | if (!type.isValid()) |
| 312 | imports->resolveType(typeLoader, type: bindingPropertyName, type_return: &type, version_return: nullptr, ns_return: nullptr); |
| 313 | |
| 314 | const QMetaObject *attachedType = type.attachedPropertiesType(typeLoader); |
| 315 | if (!attachedType) |
| 316 | COMPILE_EXCEPTION(binding, tr("Non-existent attached object" )); |
| 317 | QQmlPropertyCache::ConstPtr cache = QQmlMetaType::propertyCache(metaObject: attachedType); |
| 318 | |
| 319 | // Ignore revisions of signals on attached objects. They are not unqualified. |
| 320 | if (!resolveSignalHandlerExpressions( |
| 321 | obj: attachedObj, typeName: bindingPropertyName, propertyCache: cache, |
| 322 | checkRevision: QQmlPropertyResolver::IgnoreRevision)) { |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | QString qPropertyName; |
| 330 | QString signalName; |
| 331 | if (auto propertyName = |
| 332 | QQmlSignalNames::changedHandlerNameToPropertyName(handler: bindingPropertyName)) { |
| 333 | qPropertyName = *propertyName; |
| 334 | signalName = *QQmlSignalNames::changedHandlerNameToSignalName(changedHandler: bindingPropertyName); |
| 335 | } else { |
| 336 | signalName = QQmlSignalNames::handlerNameToSignalName(handler: bindingPropertyName) |
| 337 | .value_or(u: QString()); |
| 338 | } |
| 339 | if (signalName.isEmpty()) |
| 340 | continue; |
| 341 | |
| 342 | QQmlPropertyResolver resolver(propertyCache); |
| 343 | |
| 344 | bool notInRevision = false; |
| 345 | const QQmlPropertyData *const signal |
| 346 | = resolver.signal(name: signalName, notInRevision: ¬InRevision, check: checkRevision); |
| 347 | const QQmlPropertyData *const signalPropertyData |
| 348 | = resolver.property(name: signalName, /*notInRevision ptr*/notInRevision: nullptr, check: checkRevision); |
| 349 | const QQmlPropertyData *const qPropertyData = !qPropertyName.isEmpty() |
| 350 | ? resolver.property(name: qPropertyName, notInRevision: nullptr, check: checkRevision) |
| 351 | : nullptr; |
| 352 | QString finalSignalHandlerPropertyName = signalName; |
| 353 | QV4::CompiledData::Binding::Flag flag |
| 354 | = QV4::CompiledData::Binding::IsSignalHandlerExpression; |
| 355 | |
| 356 | const bool isPropertyObserver |
| 357 | = !signalPropertyData && qPropertyData && qPropertyData->notifiesViaBindable(); |
| 358 | if (signal && !(qPropertyData && qPropertyData->isAlias() && isPropertyObserver)) { |
| 359 | int sigIndex = propertyCache->methodIndexToSignalIndex(index: signal->coreIndex()); |
| 360 | sigIndex = propertyCache->originalClone(index: sigIndex); |
| 361 | |
| 362 | bool unnamedParameter = false; |
| 363 | |
| 364 | QList<QByteArray> parameterNames = propertyCache->signalParameterNames(index: sigIndex); |
| 365 | for (int i = 0; i < parameterNames.size(); ++i) { |
| 366 | const QString param = QString::fromUtf8(ba: parameterNames.at(i)); |
| 367 | if (param.isEmpty()) |
| 368 | unnamedParameter = true; |
| 369 | else if (unnamedParameter) { |
| 370 | COMPILE_EXCEPTION(binding, tr("Signal uses unnamed parameter followed by named parameter." )); |
| 371 | } else if (QV4::Compiler::Codegen::isNameGlobal(name: param)) { |
| 372 | COMPILE_EXCEPTION(binding, tr("Signal parameter \"%1\" hides global variable." ).arg(param)); |
| 373 | } |
| 374 | } |
| 375 | } else if (isPropertyObserver) { |
| 376 | finalSignalHandlerPropertyName = qPropertyName; |
| 377 | flag = QV4::CompiledData::Binding::IsPropertyObserver; |
| 378 | } else { |
| 379 | if (notInRevision) { |
| 380 | // Try assinging it as a property later |
| 381 | if (signalPropertyData) |
| 382 | continue; |
| 383 | |
| 384 | const QString &originalPropertyName = stringAt(idx: binding->propertyNameIndex); |
| 385 | |
| 386 | auto *typeRef = resolvedType(id: obj->inheritedTypeNameIndex); |
| 387 | const QQmlType type = typeRef ? typeRef->type() : QQmlType(); |
| 388 | if (type.isValid()) { |
| 389 | COMPILE_EXCEPTION(binding, tr("\"%1.%2\" is not available in %3 %4.%5." ) |
| 390 | .arg(typeName).arg(originalPropertyName).arg(type.module()) |
| 391 | .arg(type.version().majorVersion()) |
| 392 | .arg(type.version().minorVersion())); |
| 393 | } else { |
| 394 | COMPILE_EXCEPTION(binding, tr("\"%1.%2\" is not available due to component versioning." ).arg(typeName).arg(originalPropertyName)); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // Try to look up the signal parameter names in the object itself |
| 399 | |
| 400 | // build cache if necessary |
| 401 | if (customSignals.isEmpty()) { |
| 402 | for (const QmlIR::Signal *signal = obj->firstSignal(); signal; signal = signal->next) { |
| 403 | const QString &signalName = stringAt(idx: signal->nameIndex); |
| 404 | customSignals.insert(key: signalName, value: signal->parameterStringList(stringPool: compiler->stringPool())); |
| 405 | } |
| 406 | |
| 407 | for (const QmlIR::Property *property = obj->firstProperty(); property; property = property->next) { |
| 408 | const QString propName = stringAt(idx: property->nameIndex()); |
| 409 | customSignals.insert(key: propName, value: QStringList()); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | QHash<QString, QStringList>::ConstIterator entry = customSignals.constFind(key: signalName); |
| 414 | if (entry == customSignals.constEnd() && !qPropertyName.isEmpty()) |
| 415 | entry = customSignals.constFind(key: qPropertyName); |
| 416 | |
| 417 | if (entry == customSignals.constEnd()) { |
| 418 | // Can't find even a custom signal, then just don't do anything and try |
| 419 | // keeping the binding as a regular property assignment. |
| 420 | continue; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // Binding object to signal means connect the signal to the object's default method. |
| 425 | if (bindingType == QV4::CompiledData::Binding::Type_Object) { |
| 426 | binding->setFlag(QV4::CompiledData::Binding::IsSignalHandlerObject); |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | if (bindingType != QV4::CompiledData::Binding::Type_Script) { |
| 431 | if (bindingType < QV4::CompiledData::Binding::Type_Script) { |
| 432 | COMPILE_EXCEPTION(binding, tr("Cannot assign a value to a signal (expecting a script to be run)" )); |
| 433 | } else { |
| 434 | COMPILE_EXCEPTION(binding, tr("Incorrectly specified signal assignment" )); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | binding->propertyNameIndex = compiler->registerString(str: finalSignalHandlerPropertyName); |
| 439 | binding->setFlag(flag); |
| 440 | } |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | QQmlEnumTypeResolver::QQmlEnumTypeResolver(QQmlTypeCompiler *typeCompiler) |
| 445 | : QQmlCompilePass(typeCompiler) |
| 446 | , qmlObjects(*typeCompiler->qmlObjects()) |
| 447 | , propertyCaches(typeCompiler->propertyCaches()) |
| 448 | , imports(typeCompiler->imports()) |
| 449 | { |
| 450 | } |
| 451 | |
| 452 | bool QQmlEnumTypeResolver::resolveEnumBindings() |
| 453 | { |
| 454 | for (int i = 0; i < qmlObjects.size(); ++i) { |
| 455 | QQmlPropertyCache::ConstPtr propertyCache = propertyCaches->at(index: i); |
| 456 | if (!propertyCache) |
| 457 | continue; |
| 458 | const QmlIR::Object *obj = qmlObjects.at(i); |
| 459 | |
| 460 | QQmlPropertyResolver resolver(propertyCache); |
| 461 | |
| 462 | for (QmlIR::Binding *binding = obj->firstBinding(); binding; binding = binding->next) { |
| 463 | const QV4::CompiledData::Binding::Flags bindingFlags = binding->flags(); |
| 464 | if (bindingFlags & QV4::CompiledData::Binding::IsSignalHandlerExpression |
| 465 | || bindingFlags & QV4::CompiledData::Binding::IsSignalHandlerObject |
| 466 | || bindingFlags & QV4::CompiledData::Binding::IsPropertyObserver) |
| 467 | continue; |
| 468 | |
| 469 | if (binding->type() != QV4::CompiledData::Binding::Type_Script) |
| 470 | continue; |
| 471 | |
| 472 | const QString propertyName = stringAt(idx: binding->propertyNameIndex); |
| 473 | bool notInRevision = false; |
| 474 | const QQmlPropertyData *pd = resolver.property(name: propertyName, notInRevision: ¬InRevision); |
| 475 | if (!pd || pd->isQList()) |
| 476 | continue; |
| 477 | |
| 478 | if (!pd->isEnum() && pd->propType().id() != QMetaType::Int) |
| 479 | continue; |
| 480 | |
| 481 | if (!tryQualifiedEnumAssignment(obj, propertyCache, prop: pd, binding)) |
| 482 | return false; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | bool QQmlEnumTypeResolver::assignEnumToBinding(QmlIR::Binding *binding, QStringView, int enumValue, bool) |
| 490 | { |
| 491 | binding->setType(QV4::CompiledData::Binding::Type_Number); |
| 492 | binding->value.constantValueIndex = compiler->registerConstant(v: QV4::Encode((double)enumValue)); |
| 493 | // binding->setNumberValueInternal((double)enumValue); |
| 494 | binding->setFlag(QV4::CompiledData::Binding::IsResolvedEnum); |
| 495 | return true; |
| 496 | } |
| 497 | |
| 498 | bool QQmlEnumTypeResolver::tryQualifiedEnumAssignment( |
| 499 | const QmlIR::Object *obj, const QQmlPropertyCache::ConstPtr &propertyCache, |
| 500 | const QQmlPropertyData *prop, QmlIR::Binding *binding) |
| 501 | { |
| 502 | bool isIntProp = (prop->propType().id() == QMetaType::Int) && !prop->isEnum(); |
| 503 | if (!prop->isEnum() && !isIntProp) |
| 504 | return true; |
| 505 | |
| 506 | if (!prop->isWritable() |
| 507 | && !(binding->hasFlag(flag: QV4::CompiledData::Binding::InitializerForReadOnlyDeclaration))) { |
| 508 | COMPILE_EXCEPTION(binding, tr("Invalid property assignment: \"%1\" is a read-only property" ) |
| 509 | .arg(stringAt(binding->propertyNameIndex))); |
| 510 | } |
| 511 | |
| 512 | Q_ASSERT(binding->type() == QV4::CompiledData::Binding::Type_Script); |
| 513 | const QString string = compiler->bindingAsString(object: obj, scriptIndex: binding->value.compiledScriptIndex); |
| 514 | if (!string.constData()->isUpper()) |
| 515 | return true; |
| 516 | |
| 517 | // reject any "complex" expression (even simple arithmetic) |
| 518 | // we do this by excluding everything that is not part of a |
| 519 | // valid identifier or a dot |
| 520 | for (const QChar &c : string) |
| 521 | if (!(c.isLetterOrNumber() || c == u'.' || c == u'_' || c.isSpace())) |
| 522 | return true; |
| 523 | |
| 524 | // we support one or two '.' in the enum phrase: |
| 525 | // * <TypeName>.<EnumValue> |
| 526 | // * <TypeName>.<ScopedEnumName>.<EnumValue> |
| 527 | |
| 528 | int dot = string.indexOf(ch: QLatin1Char('.')); |
| 529 | if (dot == -1 || dot == string.size()-1) |
| 530 | return true; |
| 531 | |
| 532 | int dot2 = string.indexOf(ch: QLatin1Char('.'), from: dot+1); |
| 533 | if (dot2 != -1 && dot2 != string.size()-1) { |
| 534 | if (!string.at(i: dot+1).isUpper()) |
| 535 | return true; |
| 536 | if (string.indexOf(ch: QLatin1Char('.'), from: dot2+1) != -1) |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | QHashedStringRef typeName(string.constData(), dot); |
| 541 | const bool isQtObject = (typeName == QLatin1String("Qt" )); |
| 542 | const QStringView scopedEnumName = (dot2 != -1 ? QStringView{string}.mid(pos: dot + 1, n: dot2 - dot - 1) : QStringView()); |
| 543 | // ### consider supporting scoped enums in Qt namespace |
| 544 | const QStringView enumValue = QStringView{string}.mid(pos: !isQtObject && dot2 != -1 ? dot2 + 1 : dot + 1); |
| 545 | |
| 546 | if (isIntProp) { // ### C++11 allows enums to be other integral types. Should we support other integral types here? |
| 547 | // Allow enum assignment to ints. |
| 548 | bool ok; |
| 549 | int enumval = evaluateEnum(scope: typeName.toString(), enumName: scopedEnumName, enumValue, ok: &ok); |
| 550 | if (ok) { |
| 551 | if (!assignEnumToBinding(binding, enumValue, enumValue: enumval, isQtObject)) |
| 552 | return false; |
| 553 | } |
| 554 | return true; |
| 555 | } |
| 556 | QQmlType type; |
| 557 | imports->resolveType(typeLoader: compiler->typeLoader(), type: typeName, type_return: &type, version_return: nullptr, ns_return: nullptr); |
| 558 | |
| 559 | if (!type.isValid() && !isQtObject) |
| 560 | return true; |
| 561 | |
| 562 | int value = 0; |
| 563 | bool ok = false; |
| 564 | |
| 565 | auto *tr = resolvedType(id: obj->inheritedTypeNameIndex); |
| 566 | |
| 567 | // When these two match, we can short cut the search, unless... |
| 568 | bool useFastPath = type.isValid() && tr && tr->type() == type; |
| 569 | QMetaProperty mprop; |
| 570 | QMetaEnum ; |
| 571 | if (useFastPath) { |
| 572 | mprop = propertyCache->firstCppMetaObject()->property(index: prop->coreIndex()); |
| 573 | menum = mprop.enumerator(); |
| 574 | // ...the enumerator merely comes from a related metaobject, but the enum scope does not match |
| 575 | // the typename we resolved |
| 576 | if (!menum.isScoped() && scopedEnumName.isEmpty() && typeName != QString::fromUtf8(utf8: menum.scope())) |
| 577 | useFastPath = false;; |
| 578 | } |
| 579 | if (useFastPath) { |
| 580 | QByteArray enumName = enumValue.toUtf8(); |
| 581 | if (menum.isScoped() && !scopedEnumName.isEmpty() && enumName != scopedEnumName.toUtf8()) |
| 582 | return true; |
| 583 | |
| 584 | if (mprop.isFlagType()) { |
| 585 | value = menum.keysToValue(keys: enumName.constData(), ok: &ok); |
| 586 | } else { |
| 587 | value = menum.keyToValue(key: enumName.constData(), ok: &ok); |
| 588 | } |
| 589 | } else { |
| 590 | // Otherwise we have to search the whole type |
| 591 | if (type.isValid()) { |
| 592 | if (!scopedEnumName.isEmpty()) { |
| 593 | value = type.scopedEnumValue( |
| 594 | typeLoader: compiler->typeLoader(), scopedEnumName, enumValue, ok: &ok); |
| 595 | } else { |
| 596 | value = type.enumValue(typeLoader: compiler->typeLoader(), QHashedStringRef(enumValue), ok: &ok); |
| 597 | } |
| 598 | } else { |
| 599 | QByteArray enumName = enumValue.toUtf8(); |
| 600 | const QMetaObject *metaObject = &Qt::staticMetaObject; |
| 601 | for (int ii = metaObject->enumeratorCount() - 1; !ok && ii >= 0; --ii) { |
| 602 | QMetaEnum e = metaObject->enumerator(index: ii); |
| 603 | value = e.keyToValue(key: enumName.constData(), ok: &ok); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | if (!ok) |
| 609 | return true; |
| 610 | |
| 611 | return assignEnumToBinding(binding, enumValue, enumValue: value, isQtObject); |
| 612 | } |
| 613 | |
| 614 | int QQmlEnumTypeResolver::evaluateEnum(const QString &scope, QStringView enumName, QStringView enumValue, bool *ok) const |
| 615 | { |
| 616 | Q_ASSERT_X(ok, "QQmlEnumTypeResolver::evaluateEnum" , "ok must not be a null pointer" ); |
| 617 | *ok = false; |
| 618 | |
| 619 | if (scope != QLatin1String("Qt" )) { |
| 620 | QQmlType type; |
| 621 | imports->resolveType(typeLoader: compiler->typeLoader(), type: scope, type_return: &type, version_return: nullptr, ns_return: nullptr); |
| 622 | if (!type.isValid()) |
| 623 | return -1; |
| 624 | if (!enumName.isEmpty()) |
| 625 | return type.scopedEnumValue(typeLoader: compiler->typeLoader(), enumName, enumValue, ok); |
| 626 | return type.enumValue( |
| 627 | typeLoader: compiler->typeLoader(), |
| 628 | QHashedStringRef(enumValue.constData(), enumValue.size()), ok); |
| 629 | } |
| 630 | |
| 631 | const QMetaObject *mo = &Qt::staticMetaObject; |
| 632 | int i = mo->enumeratorCount(); |
| 633 | const QByteArray ba = enumValue.toUtf8(); |
| 634 | while (i--) { |
| 635 | int v = mo->enumerator(index: i).keyToValue(key: ba.constData(), ok); |
| 636 | if (*ok) |
| 637 | return v; |
| 638 | } |
| 639 | return -1; |
| 640 | } |
| 641 | |
| 642 | QQmlCustomParserScriptIndexer::QQmlCustomParserScriptIndexer(QQmlTypeCompiler *typeCompiler) |
| 643 | : QQmlCompilePass(typeCompiler) |
| 644 | , qmlObjects(*typeCompiler->qmlObjects()) |
| 645 | , customParsers(typeCompiler->customParserCache()) |
| 646 | { |
| 647 | } |
| 648 | |
| 649 | void QQmlCustomParserScriptIndexer::annotateBindingsWithScriptStrings() |
| 650 | { |
| 651 | scanObjectRecursively(/*root object*/objectIndex: 0); |
| 652 | for (int i = 0; i < qmlObjects.size(); ++i) |
| 653 | if (qmlObjects.at(i)->flags & QV4::CompiledData::Object::IsInlineComponentRoot) |
| 654 | scanObjectRecursively(objectIndex: i); |
| 655 | } |
| 656 | |
| 657 | void QQmlCustomParserScriptIndexer::scanObjectRecursively(int objectIndex, bool annotateScriptBindings) |
| 658 | { |
| 659 | const QmlIR::Object * const obj = qmlObjects.at(i: objectIndex); |
| 660 | if (!annotateScriptBindings) |
| 661 | annotateScriptBindings = customParsers.contains(key: obj->inheritedTypeNameIndex); |
| 662 | for (QmlIR::Binding *binding = obj->firstBinding(); binding; binding = binding->next) { |
| 663 | switch (binding->type()) { |
| 664 | case QV4::CompiledData::Binding::Type_Script: |
| 665 | if (annotateScriptBindings) { |
| 666 | binding->stringIndex = compiler->registerString( |
| 667 | str: compiler->bindingAsString(object: obj, scriptIndex: binding->value.compiledScriptIndex)); |
| 668 | } |
| 669 | break; |
| 670 | case QV4::CompiledData::Binding::Type_Object: |
| 671 | case QV4::CompiledData::Binding::Type_AttachedProperty: |
| 672 | case QV4::CompiledData::Binding::Type_GroupProperty: |
| 673 | scanObjectRecursively(objectIndex: binding->value.objectIndex, annotateScriptBindings); |
| 674 | break; |
| 675 | default: |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | QQmlAliasAnnotator::QQmlAliasAnnotator(QQmlTypeCompiler *typeCompiler) |
| 682 | : QQmlCompilePass(typeCompiler) |
| 683 | , qmlObjects(*typeCompiler->qmlObjects()) |
| 684 | , propertyCaches(typeCompiler->propertyCaches()) |
| 685 | { |
| 686 | } |
| 687 | |
| 688 | void QQmlAliasAnnotator::annotateBindingsToAliases() |
| 689 | { |
| 690 | for (int i = 0; i < qmlObjects.size(); ++i) { |
| 691 | QQmlPropertyCache::ConstPtr propertyCache = propertyCaches->at(index: i); |
| 692 | if (!propertyCache) |
| 693 | continue; |
| 694 | |
| 695 | const QmlIR::Object *obj = qmlObjects.at(i); |
| 696 | |
| 697 | QQmlPropertyResolver resolver(propertyCache); |
| 698 | const QQmlPropertyData *defaultProperty = obj->indexOfDefaultPropertyOrAlias != -1 ? propertyCache->parent()->defaultProperty() : propertyCache->defaultProperty(); |
| 699 | |
| 700 | for (QmlIR::Binding *binding = obj->firstBinding(); binding; binding = binding->next) { |
| 701 | if (!binding->isValueBinding()) |
| 702 | continue; |
| 703 | bool notInRevision = false; |
| 704 | const QQmlPropertyData *pd = binding->propertyNameIndex != quint32(0) ? resolver.property(name: stringAt(idx: binding->propertyNameIndex), notInRevision: ¬InRevision) : defaultProperty; |
| 705 | if (pd && pd->isAlias()) |
| 706 | binding->setFlag(QV4::CompiledData::Binding::IsBindingToAlias); |
| 707 | } |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | QQmlScriptStringScanner::QQmlScriptStringScanner(QQmlTypeCompiler *typeCompiler) |
| 712 | : QQmlCompilePass(typeCompiler) |
| 713 | , qmlObjects(*typeCompiler->qmlObjects()) |
| 714 | , propertyCaches(typeCompiler->propertyCaches()) |
| 715 | { |
| 716 | |
| 717 | } |
| 718 | |
| 719 | void QQmlScriptStringScanner::scan() |
| 720 | { |
| 721 | const QMetaType scriptStringMetaType = QMetaType::fromType<QQmlScriptString>(); |
| 722 | for (int i = 0; i < qmlObjects.size(); ++i) { |
| 723 | QQmlPropertyCache::ConstPtr propertyCache = propertyCaches->at(index: i); |
| 724 | if (!propertyCache) |
| 725 | continue; |
| 726 | |
| 727 | const QmlIR::Object *obj = qmlObjects.at(i); |
| 728 | |
| 729 | QQmlPropertyResolver resolver(propertyCache); |
| 730 | const QQmlPropertyData *defaultProperty = obj->indexOfDefaultPropertyOrAlias != -1 ? propertyCache->parent()->defaultProperty() : propertyCache->defaultProperty(); |
| 731 | |
| 732 | for (QmlIR::Binding *binding = obj->firstBinding(); binding; binding = binding->next) { |
| 733 | if (binding->type() != QV4::CompiledData::Binding::Type_Script) |
| 734 | continue; |
| 735 | bool notInRevision = false; |
| 736 | const QQmlPropertyData *pd = binding->propertyNameIndex != quint32(0) ? resolver.property(name: stringAt(idx: binding->propertyNameIndex), notInRevision: ¬InRevision) : defaultProperty; |
| 737 | if (!pd || pd->propType() != scriptStringMetaType) |
| 738 | continue; |
| 739 | |
| 740 | QString script = compiler->bindingAsString(object: obj, scriptIndex: binding->value.compiledScriptIndex); |
| 741 | binding->stringIndex = compiler->registerString(str: script); |
| 742 | } |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | template<> |
| 747 | void QQmlComponentAndAliasResolver<QQmlTypeCompiler>::allocateNamedObjects( |
| 748 | QmlIR::Object *object) const |
| 749 | { |
| 750 | object->namedObjectsInComponent.allocate(pool: m_compiler->memoryPool(), container: m_idToObjectIndex); |
| 751 | } |
| 752 | |
| 753 | template<> |
| 754 | bool QQmlComponentAndAliasResolver<QQmlTypeCompiler>::markAsComponent(int index) const |
| 755 | { |
| 756 | m_compiler->qmlObjects()->at(i: index)->flags |= QV4::CompiledData::Object::IsComponent; |
| 757 | return true; |
| 758 | } |
| 759 | |
| 760 | template<> |
| 761 | void QQmlComponentAndAliasResolver<QQmlTypeCompiler>::setObjectId(int index) const |
| 762 | { |
| 763 | m_compiler->qmlObjects()->at(i: index)->id = m_idToObjectIndex.size(); |
| 764 | } |
| 765 | |
| 766 | template<> |
| 767 | bool QQmlComponentAndAliasResolver<QQmlTypeCompiler>::wrapImplicitComponent(QmlIR::Binding *binding) |
| 768 | { |
| 769 | QQmlJS::MemoryPool *pool = m_compiler->memoryPool(); |
| 770 | QVector<QmlIR::Object *> *qmlObjects = m_compiler->qmlObjects(); |
| 771 | |
| 772 | // emulate "import QML 1.0" and then wrap the component in "QML.Component {}" |
| 773 | QQmlType componentType = QQmlMetaType::qmlType( |
| 774 | metaObject: &QQmlComponent::staticMetaObject, QStringLiteral("QML" ), |
| 775 | version: QTypeRevision::fromVersion(majorVersion: 1, minorVersion: 0)); |
| 776 | Q_ASSERT(componentType.isValid()); |
| 777 | const QString qualifier = QStringLiteral("QML" ); |
| 778 | |
| 779 | m_compiler->addImport(module: componentType.module(), qualifier, version: componentType.version()); |
| 780 | |
| 781 | QmlIR::Object *syntheticComponent = pool->New<QmlIR::Object>(); |
| 782 | syntheticComponent->init( |
| 783 | pool, |
| 784 | typeNameIndex: m_compiler->registerString( |
| 785 | str: qualifier + QLatin1Char('.') + componentType.elementName()), |
| 786 | idIndex: m_compiler->registerString(str: QString()), location: binding->valueLocation); |
| 787 | syntheticComponent->flags |= QV4::CompiledData::Object::IsComponent; |
| 788 | |
| 789 | if (!m_compiler->resolvedTypes->contains(key: syntheticComponent->inheritedTypeNameIndex)) { |
| 790 | auto typeRef = new QV4::ResolvedTypeReference; |
| 791 | typeRef->setType(componentType); |
| 792 | typeRef->setVersion(componentType.version()); |
| 793 | m_compiler->resolvedTypes->insert(key: syntheticComponent->inheritedTypeNameIndex, value: typeRef); |
| 794 | } |
| 795 | |
| 796 | qmlObjects->append(t: syntheticComponent); |
| 797 | const int componentIndex = qmlObjects->size() - 1; |
| 798 | // Keep property caches symmetric |
| 799 | QQmlPropertyCache::ConstPtr componentCache |
| 800 | = QQmlMetaType::propertyCache(metaObject: &QQmlComponent::staticMetaObject); |
| 801 | m_propertyCaches->append(cache: componentCache); |
| 802 | |
| 803 | QmlIR::Binding *syntheticBinding = pool->New<QmlIR::Binding>(); |
| 804 | *syntheticBinding = *binding; |
| 805 | |
| 806 | // The synthetic binding inside Component has no name. It's just "Component { Foo {} }". |
| 807 | syntheticBinding->propertyNameIndex = 0; |
| 808 | |
| 809 | syntheticBinding->setType(QV4::CompiledData::Binding::Type_Object); |
| 810 | QString error = syntheticComponent->appendBinding(b: syntheticBinding, /*isListBinding*/false); |
| 811 | Q_ASSERT(error.isEmpty()); |
| 812 | Q_UNUSED(error); |
| 813 | |
| 814 | binding->value.objectIndex = componentIndex; |
| 815 | |
| 816 | m_componentRoots.append(t: componentIndex); |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | template<> |
| 821 | void QQmlComponentAndAliasResolver<QQmlTypeCompiler>::resolveGeneralizedGroupProperty( |
| 822 | const CompiledObject &component, CompiledBinding *binding) |
| 823 | { |
| 824 | Q_UNUSED(component); |
| 825 | // We cannot make it fail here. It might be a custom-parsed property |
| 826 | const int targetObjectIndex = m_idToObjectIndex.value(key: binding->propertyNameIndex, defaultValue: -1); |
| 827 | if (targetObjectIndex != -1) |
| 828 | m_propertyCaches->set(index: binding->value.objectIndex, replacement: m_propertyCaches->at(index: targetObjectIndex)); |
| 829 | } |
| 830 | |
| 831 | template<> |
| 832 | typename QQmlComponentAndAliasResolver<QQmlTypeCompiler>::AliasResolutionResult |
| 833 | QQmlComponentAndAliasResolver<QQmlTypeCompiler>::resolveAliasesInObject( |
| 834 | const CompiledObject &component, int objectIndex, |
| 835 | QQmlPropertyCacheAliasCreator<QQmlTypeCompiler> *aliasCacheCreator, QQmlError *error) |
| 836 | { |
| 837 | // TODO: This method should not modify the aliases themselves. Rather, all information |
| 838 | // needed for handling them later should be stored in the property cache. |
| 839 | // Some of the information calculated here could be calculated already at compile time. |
| 840 | // See QTBUG-136572. |
| 841 | |
| 842 | Q_UNUSED(component); |
| 843 | |
| 844 | const QmlIR::Object * const obj = m_compiler->objectAt(index: objectIndex); |
| 845 | if (!obj->aliasCount()) |
| 846 | return AllAliasesResolved; |
| 847 | |
| 848 | int aliasIndex = 0; |
| 849 | int numSkippedAliases = 0; |
| 850 | |
| 851 | for (QmlIR::Alias *alias = obj->firstAlias(); alias; alias = alias->next, ++aliasIndex) { |
| 852 | if (resolvedAliases.contains(value: alias)) { |
| 853 | ++numSkippedAliases; |
| 854 | continue; |
| 855 | } |
| 856 | |
| 857 | |
| 858 | const int idIndex = alias->idIndex(); |
| 859 | const int targetObjectIndex = m_idToObjectIndex.value(key: idIndex, defaultValue: -1); |
| 860 | if (targetObjectIndex == -1) { |
| 861 | *error = qQmlCompileError( |
| 862 | location: alias->referenceLocation, |
| 863 | description: QQmlComponentAndAliasResolverBase::tr(sourceText: "Invalid alias reference. Unable to find id \"%1\"" ).arg(a: stringAt(idx: idIndex))); |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | const QmlIR::Object *targetObject = m_compiler->objectAt(index: targetObjectIndex); |
| 868 | Q_ASSERT(targetObject->id >= 0); |
| 869 | alias->setTargetObjectId(targetObject->id); |
| 870 | alias->setIsAliasToLocalAlias(false); |
| 871 | |
| 872 | const QString aliasPropertyValue = stringAt(idx: alias->propertyNameIndex); |
| 873 | |
| 874 | QStringView property; |
| 875 | QStringView subProperty; |
| 876 | |
| 877 | const int propertySeparator = aliasPropertyValue.indexOf(ch: QLatin1Char('.')); |
| 878 | if (propertySeparator != -1) { |
| 879 | property = QStringView{aliasPropertyValue}.left(n: propertySeparator); |
| 880 | subProperty = QStringView{aliasPropertyValue}.mid(pos: propertySeparator + 1); |
| 881 | } else |
| 882 | property = QStringView(aliasPropertyValue); |
| 883 | |
| 884 | QQmlPropertyIndex propIdx; |
| 885 | |
| 886 | if (property.isEmpty()) { |
| 887 | alias->setFlag(QV4::CompiledData::Alias::AliasPointsToPointerObject); |
| 888 | } else { |
| 889 | QQmlPropertyCache::ConstPtr targetCache = m_propertyCaches->at(index: targetObjectIndex); |
| 890 | if (!targetCache) { |
| 891 | *error = qQmlCompileError( |
| 892 | location: alias->referenceLocation, |
| 893 | description: QQmlComponentAndAliasResolverBase::tr(sourceText: "Invalid alias target location: %1" ).arg(a: property.toString())); |
| 894 | break; |
| 895 | } |
| 896 | |
| 897 | QQmlPropertyResolver resolver(targetCache); |
| 898 | |
| 899 | const QQmlPropertyData *targetProperty = resolver.property( |
| 900 | name: property.toString(), notInRevision: nullptr, check: QQmlPropertyResolver::IgnoreRevision); |
| 901 | |
| 902 | // If it's an alias that we haven't resolved yet, try again later. |
| 903 | if (!targetProperty) { |
| 904 | bool aliasPointsToOtherAlias = false; |
| 905 | int localAliasIndex = 0; |
| 906 | for (auto targetAlias = targetObject->aliasesBegin(), end = targetObject->aliasesEnd(); targetAlias != end; ++targetAlias, ++localAliasIndex) { |
| 907 | if (stringAt(idx: targetAlias->nameIndex()) == property) { |
| 908 | aliasPointsToOtherAlias = true; |
| 909 | break; |
| 910 | } |
| 911 | } |
| 912 | if (aliasPointsToOtherAlias) { |
| 913 | if (targetObjectIndex == objectIndex) { |
| 914 | alias->localAliasIndex = localAliasIndex; |
| 915 | alias->setIsAliasToLocalAlias(true); |
| 916 | if (!appendAliasToPropertyCache( |
| 917 | component: &component, alias, objectIndex, aliasIndex, encodedPropertyIndex: -1, |
| 918 | aliasCacheCreator, error)) { |
| 919 | break; |
| 920 | } |
| 921 | continue; |
| 922 | } |
| 923 | |
| 924 | // restore |
| 925 | alias->setIdIndex(idIndex); |
| 926 | // Try again later and resolve the target alias first. |
| 927 | return aliasIndex == numSkippedAliases ? NoAliasResolved : SomeAliasesResolved; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (!targetProperty || targetProperty->coreIndex() > 0x0000FFFF) { |
| 932 | *error = qQmlCompileError( |
| 933 | location: alias->referenceLocation, |
| 934 | description: QQmlComponentAndAliasResolverBase::tr(sourceText: "Invalid alias target location: %1" ).arg(a: property.toString())); |
| 935 | break; |
| 936 | } |
| 937 | |
| 938 | propIdx = QQmlPropertyIndex(targetProperty->coreIndex()); |
| 939 | |
| 940 | if (!subProperty.isEmpty()) { |
| 941 | const QMetaObject *valueTypeMetaObject = QQmlMetaType::metaObjectForValueType(type: targetProperty->propType()); |
| 942 | if (!valueTypeMetaObject) { |
| 943 | // could be a deep alias |
| 944 | bool isDeepAlias = subProperty.at(n: 0).isLower(); |
| 945 | if (isDeepAlias) { |
| 946 | isDeepAlias = false; |
| 947 | for (auto it = targetObject->bindingsBegin(); it != targetObject->bindingsEnd(); ++it) { |
| 948 | auto binding = *it; |
| 949 | if (m_compiler->stringAt(idx: binding.propertyNameIndex) == property) { |
| 950 | resolver = QQmlPropertyResolver(m_propertyCaches->at(index: binding.value.objectIndex)); |
| 951 | const QQmlPropertyData *actualProperty = resolver.property(name: subProperty.toString()); |
| 952 | if (actualProperty) { |
| 953 | propIdx = QQmlPropertyIndex(propIdx.coreIndex(), actualProperty->coreIndex()); |
| 954 | isDeepAlias = true; |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | if (!isDeepAlias) { |
| 960 | *error = qQmlCompileError( |
| 961 | location: alias->referenceLocation, |
| 962 | description: QQmlComponentAndAliasResolverBase::tr(sourceText: "Invalid alias target location: %1" ).arg(a: subProperty.toString())); |
| 963 | break; |
| 964 | } |
| 965 | } else { |
| 966 | |
| 967 | int valueTypeIndex = |
| 968 | valueTypeMetaObject->indexOfProperty(name: subProperty.toString().toUtf8().constData()); |
| 969 | if (valueTypeIndex == -1) { |
| 970 | *error = qQmlCompileError( |
| 971 | location: alias->referenceLocation, |
| 972 | description: QQmlComponentAndAliasResolverBase::tr(sourceText: "Invalid alias target location: %1" ).arg(a: subProperty.toString())); |
| 973 | break; |
| 974 | } |
| 975 | Q_ASSERT(valueTypeIndex <= 0x0000FFFF); |
| 976 | |
| 977 | propIdx = QQmlPropertyIndex(propIdx.coreIndex(), valueTypeIndex); |
| 978 | } |
| 979 | } else { |
| 980 | if (targetProperty->isQObject()) |
| 981 | alias->setFlag(QV4::CompiledData::Alias::AliasPointsToPointerObject); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | if (!appendAliasToPropertyCache( |
| 986 | component: &component, alias, objectIndex, aliasIndex, encodedPropertyIndex: propIdx.toEncoded(), |
| 987 | aliasCacheCreator, error)) { |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (numSkippedAliases == aliasIndex) |
| 993 | return NoAliasResolved; |
| 994 | |
| 995 | if (aliasIndex == obj->aliasCount()) |
| 996 | return AllAliasesResolved; |
| 997 | |
| 998 | return SomeAliasesResolved; |
| 999 | } |
| 1000 | |
| 1001 | QQmlDeferredAndCustomParserBindingScanner::QQmlDeferredAndCustomParserBindingScanner(QQmlTypeCompiler *typeCompiler) |
| 1002 | : QQmlCompilePass(typeCompiler) |
| 1003 | , qmlObjects(typeCompiler->qmlObjects()) |
| 1004 | , propertyCaches(typeCompiler->propertyCaches()) |
| 1005 | , customParsers(typeCompiler->customParserCache()) |
| 1006 | , _seenObjectWithId(false) |
| 1007 | { |
| 1008 | } |
| 1009 | |
| 1010 | bool QQmlDeferredAndCustomParserBindingScanner::scanObject() |
| 1011 | { |
| 1012 | for (int i = 0; i < qmlObjects->size(); ++i) { |
| 1013 | if ((qmlObjects->at(i)->flags & QV4::CompiledData::Object::IsInlineComponentRoot) |
| 1014 | && !scanObject(objectIndex: i, scopeDeferred: ScopeDeferred::False)) { |
| 1015 | return false; |
| 1016 | } |
| 1017 | } |
| 1018 | return scanObject(/*root object*/objectIndex: 0, scopeDeferred: ScopeDeferred::False); |
| 1019 | } |
| 1020 | |
| 1021 | bool QQmlDeferredAndCustomParserBindingScanner::scanObject( |
| 1022 | int objectIndex, ScopeDeferred scopeDeferred) |
| 1023 | { |
| 1024 | using namespace QV4::CompiledData; |
| 1025 | |
| 1026 | QmlIR::Object *obj = qmlObjects->at(i: objectIndex); |
| 1027 | if (obj->idNameIndex != 0) |
| 1028 | _seenObjectWithId = true; |
| 1029 | |
| 1030 | if (obj->flags & Object::IsComponent) { |
| 1031 | Q_ASSERT(obj->bindingCount() == 1); |
| 1032 | const Binding *componentBinding = obj->firstBinding(); |
| 1033 | Q_ASSERT(componentBinding->type() == Binding::Type_Object); |
| 1034 | // Components are separate from their surrounding scope. They cannot be deferred. |
| 1035 | return scanObject(objectIndex: componentBinding->value.objectIndex, scopeDeferred: ScopeDeferred::False); |
| 1036 | } |
| 1037 | |
| 1038 | QQmlPropertyCache::ConstPtr propertyCache = propertyCaches->at(index: objectIndex); |
| 1039 | if (!propertyCache) |
| 1040 | return true; |
| 1041 | |
| 1042 | QString defaultPropertyName; |
| 1043 | const QQmlPropertyData *defaultProperty = nullptr; |
| 1044 | if (obj->indexOfDefaultPropertyOrAlias != -1) { |
| 1045 | const QQmlPropertyCache *cache = propertyCache->parent().data(); |
| 1046 | defaultPropertyName = cache->defaultPropertyName(); |
| 1047 | defaultProperty = cache->defaultProperty(); |
| 1048 | } else { |
| 1049 | defaultPropertyName = propertyCache->defaultPropertyName(); |
| 1050 | defaultProperty = propertyCache->defaultProperty(); |
| 1051 | } |
| 1052 | |
| 1053 | QQmlCustomParser *customParser = customParsers.value(key: obj->inheritedTypeNameIndex); |
| 1054 | |
| 1055 | QQmlPropertyResolver propertyResolver(propertyCache); |
| 1056 | |
| 1057 | QStringList deferredPropertyNames; |
| 1058 | QStringList immediatePropertyNames; |
| 1059 | { |
| 1060 | const QMetaObject *mo = propertyCache->firstCppMetaObject(); |
| 1061 | const int deferredNamesIndex = mo->indexOfClassInfo(name: "DeferredPropertyNames" ); |
| 1062 | const int immediateNamesIndex = mo->indexOfClassInfo(name: "ImmediatePropertyNames" ); |
| 1063 | if (deferredNamesIndex != -1) { |
| 1064 | if (immediateNamesIndex != -1) { |
| 1065 | COMPILE_EXCEPTION(obj, tr("You cannot define both DeferredPropertyNames and " |
| 1066 | "ImmediatePropertyNames on the same type." )); |
| 1067 | } |
| 1068 | const QMetaClassInfo classInfo = mo->classInfo(index: deferredNamesIndex); |
| 1069 | deferredPropertyNames = QString::fromUtf8(utf8: classInfo.value()).split(sep: u','); |
| 1070 | } else if (immediateNamesIndex != -1) { |
| 1071 | const QMetaClassInfo classInfo = mo->classInfo(index: immediateNamesIndex); |
| 1072 | immediatePropertyNames = QString::fromUtf8(utf8: classInfo.value()).split(sep: u','); |
| 1073 | |
| 1074 | // If the property contains an empty string, all properties shall be deferred. |
| 1075 | if (immediatePropertyNames.isEmpty()) |
| 1076 | immediatePropertyNames.append(t: QString()); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | for (QmlIR::Binding *binding = obj->firstBinding(); binding; binding = binding->next) { |
| 1081 | QString name = stringAt(idx: binding->propertyNameIndex); |
| 1082 | |
| 1083 | if (customParser) { |
| 1084 | if (binding->type() == Binding::Type_AttachedProperty) { |
| 1085 | if (customParser->flags() & QQmlCustomParser::AcceptsAttachedProperties) { |
| 1086 | binding->setFlag(Binding::IsCustomParserBinding); |
| 1087 | obj->flags |= Object::HasCustomParserBindings; |
| 1088 | continue; |
| 1089 | } |
| 1090 | } else if (QQmlSignalNames::isHandlerName(signalName: name) |
| 1091 | && !(customParser->flags() & QQmlCustomParser::AcceptsSignalHandlers)) { |
| 1092 | obj->flags |= Object::HasCustomParserBindings; |
| 1093 | binding->setFlag(Binding::IsCustomParserBinding); |
| 1094 | continue; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | const bool hasPropertyData = [&]() { |
| 1099 | if (name.isEmpty()) { |
| 1100 | name = defaultPropertyName; |
| 1101 | if (defaultProperty) |
| 1102 | return true; |
| 1103 | } else if (name.constData()->isUpper()) { |
| 1104 | // Upper case names cannot be custom-parsed unless they are attached properties |
| 1105 | // and the custom parser explicitly accepts them. See above for that case. |
| 1106 | return false; |
| 1107 | } else { |
| 1108 | bool notInRevision = false; |
| 1109 | if (propertyResolver.property( |
| 1110 | name, notInRevision: ¬InRevision, check: QQmlPropertyResolver::CheckRevision)) { |
| 1111 | return true; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | if (!customParser) |
| 1116 | return false; |
| 1117 | |
| 1118 | const Binding::Flags bindingFlags = binding->flags(); |
| 1119 | if (bindingFlags & Binding::IsSignalHandlerExpression |
| 1120 | || bindingFlags & Binding::IsSignalHandlerObject |
| 1121 | || bindingFlags & Binding::IsPropertyObserver) { |
| 1122 | // These signal handlers cannot be custom-parsed. We have already established |
| 1123 | // that the signal exists. |
| 1124 | return false; |
| 1125 | } |
| 1126 | |
| 1127 | // If the property isn't found, we may want to custom-parse the binding. |
| 1128 | obj->flags |= Object::HasCustomParserBindings; |
| 1129 | binding->setFlag(Binding::IsCustomParserBinding); |
| 1130 | return false; |
| 1131 | }(); |
| 1132 | |
| 1133 | bool seenSubObjectWithId = false; |
| 1134 | bool isExternal = false; |
| 1135 | if (binding->type() >= Binding::Type_Object) { |
| 1136 | const bool isOwnProperty = hasPropertyData || binding->isAttachedProperty(); |
| 1137 | isExternal = !isOwnProperty && binding->isGroupProperty(); |
| 1138 | if (isOwnProperty || isExternal) { |
| 1139 | qSwap(value1&: _seenObjectWithId, value2&: seenSubObjectWithId); |
| 1140 | const bool subObjectValid = scanObject( |
| 1141 | objectIndex: binding->value.objectIndex, |
| 1142 | scopeDeferred: (isExternal || scopeDeferred == ScopeDeferred::True) |
| 1143 | ? ScopeDeferred::True |
| 1144 | : ScopeDeferred::False); |
| 1145 | qSwap(value1&: _seenObjectWithId, value2&: seenSubObjectWithId); |
| 1146 | if (!subObjectValid) |
| 1147 | return false; |
| 1148 | _seenObjectWithId |= seenSubObjectWithId; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | bool isDeferred = false; |
| 1153 | if (!immediatePropertyNames.isEmpty() && !immediatePropertyNames.contains(str: name)) { |
| 1154 | if (seenSubObjectWithId) { |
| 1155 | COMPILE_EXCEPTION(binding, tr("You cannot assign an id to an object assigned " |
| 1156 | "to a deferred property." )); |
| 1157 | } |
| 1158 | if (isExternal || !disableInternalDeferredProperties()) |
| 1159 | isDeferred = true; |
| 1160 | } else if (!deferredPropertyNames.isEmpty() && deferredPropertyNames.contains(str: name)) { |
| 1161 | if (!seenSubObjectWithId && binding->type() != Binding::Type_GroupProperty) { |
| 1162 | if (isExternal || !disableInternalDeferredProperties()) |
| 1163 | isDeferred = true; |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | if (binding->type() >= Binding::Type_Object) { |
| 1168 | if (isExternal && !isDeferred && !customParser) { |
| 1169 | COMPILE_EXCEPTION( |
| 1170 | binding, tr("Cannot assign to non-existent property \"%1\"" ).arg(name)); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | if (isDeferred) { |
| 1175 | binding->setFlag(Binding::IsDeferredBinding); |
| 1176 | obj->flags |= Object::HasDeferredBindings; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return true; |
| 1181 | } |
| 1182 | |
| 1183 | QQmlDefaultPropertyMerger::QQmlDefaultPropertyMerger(QQmlTypeCompiler *typeCompiler) |
| 1184 | : QQmlCompilePass(typeCompiler) |
| 1185 | , qmlObjects(*typeCompiler->qmlObjects()) |
| 1186 | , propertyCaches(typeCompiler->propertyCaches()) |
| 1187 | { |
| 1188 | |
| 1189 | } |
| 1190 | |
| 1191 | void QQmlDefaultPropertyMerger::mergeDefaultProperties() |
| 1192 | { |
| 1193 | for (int i = 0; i < qmlObjects.size(); ++i) |
| 1194 | mergeDefaultProperties(objectIndex: i); |
| 1195 | } |
| 1196 | |
| 1197 | void QQmlDefaultPropertyMerger::mergeDefaultProperties(int objectIndex) |
| 1198 | { |
| 1199 | QQmlPropertyCache::ConstPtr propertyCache = propertyCaches->at(index: objectIndex); |
| 1200 | if (!propertyCache) |
| 1201 | return; |
| 1202 | |
| 1203 | QmlIR::Object *object = qmlObjects.at(i: objectIndex); |
| 1204 | |
| 1205 | QString defaultProperty = object->indexOfDefaultPropertyOrAlias != -1 ? propertyCache->parent()->defaultPropertyName() : propertyCache->defaultPropertyName(); |
| 1206 | QmlIR::Binding *bindingsToReinsert = nullptr; |
| 1207 | QmlIR::Binding *tail = nullptr; |
| 1208 | |
| 1209 | QmlIR::Binding *previousBinding = nullptr; |
| 1210 | QmlIR::Binding *binding = object->firstBinding(); |
| 1211 | while (binding) { |
| 1212 | if (binding->propertyNameIndex == quint32(0) || stringAt(idx: binding->propertyNameIndex) != defaultProperty) { |
| 1213 | previousBinding = binding; |
| 1214 | binding = binding->next; |
| 1215 | continue; |
| 1216 | } |
| 1217 | |
| 1218 | QmlIR::Binding *toReinsert = binding; |
| 1219 | binding = object->unlinkBinding(before: previousBinding, binding); |
| 1220 | |
| 1221 | if (!tail) { |
| 1222 | bindingsToReinsert = toReinsert; |
| 1223 | tail = toReinsert; |
| 1224 | } else { |
| 1225 | tail->next = toReinsert; |
| 1226 | tail = tail->next; |
| 1227 | } |
| 1228 | tail->next = nullptr; |
| 1229 | } |
| 1230 | |
| 1231 | binding = bindingsToReinsert; |
| 1232 | while (binding) { |
| 1233 | QmlIR::Binding *toReinsert = binding; |
| 1234 | binding = binding->next; |
| 1235 | object->insertSorted(b: toReinsert); |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | QT_END_NAMESPACE |
| 1240 | |