7#include <private/qqmlobjectcreator_p.h>
8#include <private/qqmlcustomparser_p.h>
9#include <private/qqmlvmemetaobject_p.h>
10#include <private/qqmlcomponent_p.h>
11#include <private/qqmlpropertyresolver_p.h>
12#include <private/qqmlcomponentandaliasresolver_p.h>
13#include <private/qqmlsignalnames_p.h>
15#include <QtCore/qtyperevision.h>
19DEFINE_BOOL_CONFIG_OPTION(
20 disableInternalDeferredProperties, QML_DISABLE_INTERNAL_DEFERRED_PROPERTIES);
25 : document(parsedQML), typeData(typeData)
31 Q_ASSERT(!document->javaScriptCompilationUnit || !document->javaScriptCompilationUnit->unitData());
33 Q_ASSERT(document->jsModule.fileName == typeData->urlString());
34 Q_ASSERT(document->jsModule.finalUrl == typeData->finalUrlString());
35 QmlIR::JSCodeGen v4CodeGenerator(document);
36 for (QmlIR::Object *object : std::as_const(document->objects)) {
37 if (!v4CodeGenerator.generateRuntimeFunctions(object)) {
38 Q_ASSERT(v4CodeGenerator.hasError());
39 recordError(v4CodeGenerator.error());
43 document->javaScriptCompilationUnit = v4CodeGenerator.generateCompilationUnit(
false);
48 QmlIR::QmlUnitGenerator qmlGenerator;
49 qmlGenerator.generate(*document);
51 if (!errors.isEmpty())
54 return std::move(document->javaScriptCompilationUnit);
60 error.setDescription(message.message);
61 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(message.loc.startLine));
62 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(message.loc.startColumn));
63 error.setUrl(typeData->finalUrl());
69class RuntimeDataBuilder
72 RuntimeDataBuilder(QV4::CompiledData::CompilationUnit *unit, QQmlTypeLoader *typeLoader,
73 const QQmlImports *imports)
74 : unit(unit), typeLoader(typeLoader), imports(imports)
78 QList<QQmlError> build(
const QV4::CompiledData::DependentTypesHasher &dependencyHasher)
80 Q_ASSERT(unit->runtimeData);
81 for (
int i = 0, count = unit->objectCount(); i < count; ++i) {
82 if (!classifySignalHandlers(i))
86 for (
int i = 0, count = unit->objectCount(); i < count; ++i) {
87 if (unit->objectAt(i)->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot)) {
88 if (!scanObject(i, ScopeDeferred::False))
92 scanObject(0, ScopeDeferred::False);
93 if (!errors.isEmpty())
96 for (
int i = 0, count = unit->objectCount(); i < count; ++i) {
97 classifyBindingsToAliases(i);
98 computeBindingOrder(i);
102 if (dependencyHasher) {
103 const QByteArray checksum = dependencyHasher();
104 if (checksum.size() ==
sizeof(unit->runtimeData->dependencyMD5Checksum)) {
105 memcpy(unit->runtimeDataForWrite()->dependencyMD5Checksum, checksum.constData(),
106 sizeof(unit->runtimeData->dependencyMD5Checksum));
114 enum class ScopeDeferred :
bool { False, True };
116 bool isValueBinding(
int objectIndex,
int bindingIndex)
const
118 switch (unit->objectAt(objectIndex)->bindingTable()[bindingIndex].type()) {
119 case QV4::CompiledData::Binding::Type_AttachedProperty:
120 case QV4::CompiledData::Binding::Type_GroupedProperty:
123 return !unit->bindingIsSignalHandler(objectIndex, bindingIndex);
127 bool classifySignalHandlers(
int objectIndex)
129 const QQmlPropertyCache::ConstPtr cache = unit->propertyCaches.at(objectIndex);
133 const QV4::CompiledData::Object *obj = unit->objectAt(objectIndex);
134 if (
const QV4::ResolvedTypeReference *typeRef
135 = unit->resolvedType(obj->inheritedTypeNameIndex)) {
136 if (QQmlCustomParser *customParser = typeRef->type().customParser()) {
137 if (!(customParser->flags() & QQmlCustomParser::AcceptsSignalHandlers))
142 return classifySignalHandlers(
143 objectIndex, unit->stringAt(obj->inheritedTypeNameIndex), cache,
144 QQmlPropertyResolver::CheckRevision);
147 bool classifySignalHandlers(
148 int objectIndex,
const QString &typeName,
149 const QQmlPropertyCache::ConstPtr &propertyCache,
150 QQmlPropertyResolver::RevisionCheck checkRevision)
154 const Object *obj = unit->objectAt(objectIndex);
155 RuntimeBinding *runtimeBindings
156 = unit->runtimeDataForWrite()->objectAt(objectIndex)->bindingTable();
157 QSet<QString> customSignals;
159 const Binding *binding = obj->bindingTable();
160 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
163 if (unit->bindingIsSignalHandler(objectIndex, i))
166 const QString bindingPropertyName = unit->stringAt(binding->propertyNameIndex);
167 const Binding::Type bindingType = binding->type();
169 if (bindingType == Binding::Type_AttachedProperty) {
170 const QV4::ResolvedTypeReference *typeRef
171 = unit->resolvedType(binding->propertyNameIndex);
172 QQmlType type = typeRef ? typeRef->type() : QQmlType();
173 if (!type.isValid()) {
174 QQmlImportResolutionResult resolution;
175 imports->resolveType(typeLoader, bindingPropertyName, &resolution);
176 type = resolution.type;
178 const QMetaObject *attachedType = type.attachedPropertiesType(typeLoader);
180 return recordError(binding->location,
181 QQmlTypeCompiler::tr(
"Non-existent attached object"));
185 if (!classifySignalHandlers(binding->value.objectIndex, bindingPropertyName,
186 QQmlMetaType::propertyCache(attachedType),
187 QQmlPropertyResolver::IgnoreRevision)) {
193 const QString qPropertyName
194 = QQmlSignalNames::changedHandlerNameToPropertyName(bindingPropertyName)
195 .value_or(QString());
196 const QString signalName
197 = QQmlSignalNames::anyHandlerNameToSignalName(bindingPropertyName)
198 .value_or(QString());
199 if (signalName.isEmpty())
202 QQmlPropertyResolver resolver(propertyCache);
203 bool notInRevision =
false;
204 const QQmlPropertyData *
const signal
205 = resolver.signal(signalName, ¬InRevision, checkRevision);
206 const QQmlPropertyData *
const signalPropertyData
207 = resolver.property(signalName,
nullptr, checkRevision);
208 const QQmlPropertyData *
const qPropertyData = !qPropertyName.isEmpty()
209 ? resolver.property(qPropertyName,
nullptr, checkRevision)
212 RuntimeBinding::Flag flag = RuntimeBinding::IsSignalHandlerExpression;
213 const bool isPropertyObserver
214 = !signalPropertyData && qPropertyData && qPropertyData->notifiesViaBindable();
215 if (signal && !(qPropertyData && qPropertyData->isAlias() && isPropertyObserver)) {
216 int sigIndex = propertyCache->methodIndexToSignalIndex(signal->coreIndex());
217 sigIndex = propertyCache->originalClone(sigIndex);
218 bool unnamedParameter =
false;
219 const QList<QByteArray> parameterNames
220 = propertyCache->signalParameterNames(sigIndex);
221 for (
const QByteArray ¶meterName : parameterNames) {
222 const QString param = QString::fromUtf8(parameterName);
223 if (param.isEmpty()) {
224 unnamedParameter =
true;
225 }
else if (unnamedParameter) {
228 QQmlTypeCompiler::tr(
"Signal uses unnamed parameter followed by "
229 "named parameter."));
230 }
else if (QV4::Compiler::Codegen::isNameGlobal(param)) {
233 QQmlTypeCompiler::tr(
"Signal parameter \"%1\" hides global "
234 "variable.").arg(param));
237 }
else if (isPropertyObserver) {
238 flag = RuntimeBinding::IsPropertyObserver;
242 if (signalPropertyData)
245 if (
const QV4::ResolvedTypeReference *typeRef
246 = unit->resolvedType(obj->inheritedTypeNameIndex);
247 typeRef && typeRef->type().isValid()) {
248 const QQmlType type = typeRef->type();
251 QQmlTypeCompiler::tr(
"\"%1.%2\" is not available in %3 %4.%5.")
252 .arg(typeName, bindingPropertyName, type.module())
253 .arg(type.version().majorVersion())
254 .arg(type.version().minorVersion()));
258 QQmlTypeCompiler::tr(
"\"%1.%2\" is not available due to component "
259 "versioning.").arg(typeName, bindingPropertyName));
265 if (customSignals.isEmpty()) {
266 for (
int s = 0, sc = obj->signalCount(); s < sc; ++s)
267 customSignals.insert(unit->stringAt(obj->signalAt(s)->nameIndex));
268 const Property *properties = obj->propertyTable();
269 for (
int p = 0, pc = obj->propertyCount(); p < pc; ++p)
270 customSignals.insert(unit->stringAt(properties[p].nameIndex()));
274 if (!customSignals.contains(signalName)
275 && (qPropertyName.isEmpty() || !customSignals.contains(qPropertyName))) {
281 if (bindingType == Binding::Type_Object) {
282 runtimeBindings[i].flags |= RuntimeBinding::IsSignalHandlerObject;
286 if (bindingType != Binding::Type_Script) {
289 bindingType < Binding::Type_Script
290 ? QQmlTypeCompiler::tr(
"Cannot assign a value to a signal "
291 "(expecting a script to be run)")
292 : QQmlTypeCompiler::tr(
"Incorrectly specified signal assignment"));
295 runtimeBindings[i].flags |= flag;
300 void foldEnums(
int objectIndex)
303 const Object *obj = unit->objectAt(objectIndex);
304 RuntimeBinding *runtimeBindings
305 = unit->runtimeDataForWrite()->objectAt(objectIndex)->bindingTable();
306 const Binding *binding = obj->bindingTable();
307 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
308 if (binding->type() != Binding::Type_Script
309 || unit->bindingIsSignalHandler(objectIndex, i)) {
313 if (
const std::optional<
int> value = evaluateQualifiedEnum(objectIndex, binding)) {
314 runtimeBindings[i].flags |= RuntimeBinding::IsFoldedEnum;
315 runtimeBindings[i].enumValue = *value;
320 std::optional<
int> evaluateQualifiedEnum(
int objectIndex,
321 const QV4::CompiledData::Binding *binding)
323 const QQmlPropertyCache::ConstPtr cache = unit->propertyCaches.at(objectIndex);
327 QQmlPropertyResolver resolver(cache);
328 bool notInRevision =
false;
329 const QQmlPropertyData *prop =
330 resolver.property(unit->stringAt(binding->propertyNameIndex), ¬InRevision);
331 if (!prop || prop->isQList())
333 const bool isIntProp = (prop->propType().id() == QMetaType::Int) && !prop->isEnum();
334 if (!prop->isEnum() && !isIntProp)
337 if (!prop->isWritable()
338 && !(binding->hasFlag(QV4::CompiledData::Binding::InitializerForReadOnlyDeclaration))) {
339 recordError(binding->location,
340 QQmlTypeCompiler::tr(
341 "Invalid property assignment: \"%1\" is a read-only property")
342 .arg(unit->stringAt(binding->propertyNameIndex)));
346 const QString string = unit->stringAt(binding->stringIndex);
347 if (string.isEmpty() || !string.constData()->isUpper())
350 const int dot = string.indexOf(u'.');
351 if (dot == -1 || dot == string.size() - 1)
353 const int dot2 = string.indexOf(u'.', dot + 1);
354 if (dot2 != -1 && dot2 != string.size() - 1) {
355 if (!string.at(dot + 1).isUpper())
357 if (string.indexOf(u'.', dot2 + 1) != -1)
361 const QHashedStringRef typeName(string.constData(), dot);
362 const QStringView scopedEnumName =
363 (dot2 != -1 ? QStringView{ string }.mid(dot + 1, dot2 - dot - 1) : QStringView());
364 const QStringView enumValue = QStringView{ string }.mid(dot2 != -1 ? dot2 + 1 : dot + 1);
367 return evaluateEnum(typeName.toString(), scopedEnumName, enumValue);
369 QQmlImportResolutionResult resolution;
370 imports->resolveType(typeLoader, typeName, &resolution);
371 const QQmlType type = resolution.type;
375 QV4::ResolvedTypeReference *tr =
376 unit->resolvedType(unit->objectAt(objectIndex)->inheritedTypeNameIndex);
377 bool useFastPath = type.isValid() && tr && tr->type() == type;
381 mprop = cache->firstCppMetaObject()->property(prop->coreIndex());
382 menum = mprop.enumerator();
383 if (!menum.isScoped() && scopedEnumName.isEmpty()
384 && typeName != QString::fromUtf8(menum.scope())) {
392 const QByteArray enumName = enumValue.toUtf8();
393 if (menum.isScoped() && !scopedEnumName.isEmpty()
394 && enumName != scopedEnumName.toUtf8()) {
397 value = mprop.isFlagType() ? menum.keysToValue(enumName.constData(), &ok)
398 : menum.keyToValue(enumName.constData(), &ok);
400 value = scopedEnumName.isEmpty()
401 ? type.enumValue(typeLoader, enumValue, &ok)
402 : type.scopedEnumValue(typeLoader, scopedEnumName, enumValue, &ok);
404 return ok ? std::optional<
int>(value) : std::nullopt;
407 std::optional<
int> evaluateEnum(
const QString &scope, QStringView enumName,
408 QStringView enumValue)
411 QQmlImportResolutionResult resolution;
412 imports->resolveType(typeLoader, scope, &resolution);
413 const QQmlType type = resolution.type;
417 const int value = enumName.isEmpty()
418 ? type.enumValue(typeLoader,
419 QHashedStringRef(enumValue.constData(), enumValue.size()), &ok)
420 : type.scopedEnumValue(typeLoader, enumName, enumValue, &ok);
421 return ok ? std::optional<
int>(value) : std::nullopt;
424 void classifyBindingsToAliases(
int objectIndex)
427 const QQmlPropertyCache::ConstPtr propertyCache = unit->propertyCaches.at(objectIndex);
431 const Object *obj = unit->objectAt(objectIndex);
432 RuntimeBinding *runtimeBindings
433 = unit->runtimeDataForWrite()->objectAt(objectIndex)->bindingTable();
434 QQmlPropertyResolver resolver(propertyCache);
435 const QQmlPropertyData *defaultProperty = obj->indexOfDefaultPropertyOrAlias != -1
436 ? propertyCache->parent()->defaultProperty()
437 : propertyCache->defaultProperty();
439 const Binding *binding = obj->bindingTable();
440 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
441 if (!isValueBinding(objectIndex, i))
443 bool notInRevision =
false;
444 const QQmlPropertyData *pd = binding->propertyNameIndex != 0
445 ? resolver.property(unit->stringAt(binding->propertyNameIndex), ¬InRevision)
447 if (pd && pd->isAlias())
448 runtimeBindings[i].flags |= RuntimeBinding::IsBindingToAlias;
452 void computeBindingOrder(
int objectIndex)
455 const Object *obj = unit->objectAt(objectIndex);
456 const Binding *bindings = obj->bindingTable();
457 RuntimeObject *runtimeObject = unit->runtimeDataForWrite()->objectAt(objectIndex);
458 const RuntimeBinding *runtimeBindings = runtimeObject->bindingTable();
459 const auto isAlias = [&](quint32 b) {
460 return runtimeBindings[b].flags & RuntimeBinding::IsBindingToAlias;
463 RuntimeBindingIndex *order = runtimeObject->bindingOrder();
465 const auto collect = [&](
auto predicate) {
466 for (quint16 b = 0; b < obj->nBindings; ++b) {
471 collect([&](quint16 b) {
return isValueBinding(objectIndex, b) && !isAlias(b); });
472 collect([&](quint16 b) {
return unit->bindingIsSignalHandler(objectIndex, b); });
473 collect([&](quint16 b) {
return bindings[b].isAttachedProperty(); });
474 collect([&](quint16 b) {
return bindings[b].isGroupedProperty(); });
475 collect([&](quint16 b) {
return isValueBinding(objectIndex, b) && isAlias(b); });
476 Q_ASSERT(n == obj->nBindings);
482 error.setUrl(unit->finalUrl());
483 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(location.line()));
484 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(location.column()));
485 error.setDescription(description);
486 errors.append(error);
490 bool deferredAndImmediatePropertyNames(
491 const QV4::CompiledData::Object *obj,
const QQmlPropertyCache::ConstPtr &propertyCache,
492 QStringList *deferredPropertyNames, QStringList *immediatePropertyNames)
494 const QMetaObject *mo = propertyCache->firstCppMetaObject();
495 const int deferredNamesIndex = mo->indexOfClassInfo(
"DeferredPropertyNames");
496 const int immediateNamesIndex = mo->indexOfClassInfo(
"ImmediatePropertyNames");
498 if (deferredNamesIndex != -1) {
499 if (immediateNamesIndex != -1) {
500 return recordError(obj->location,
501 QQmlTypeCompiler::tr(
"You cannot define both DeferredPropertyNames "
502 "and ImmediatePropertyNames on the same type."));
504 *deferredPropertyNames
505 = QString::fromUtf8(mo->classInfo(deferredNamesIndex).value()).split(u',');
506 }
else if (immediateNamesIndex != -1) {
507 *immediatePropertyNames
508 = QString::fromUtf8(mo->classInfo(immediateNamesIndex).value()).split(u',');
510 if (immediatePropertyNames->isEmpty())
511 immediatePropertyNames->append(QString());
516 bool scanObject(
int objectIndex, ScopeDeferred scopeDeferred)
520 if (objectIndex >= unit->objectCount())
521 return scanObject(unit->resolvedIndex(objectIndex), scopeDeferred);
523 const Object *obj = unit->objectAt(objectIndex);
524 if (obj->idNameIndex != 0)
525 seenObjectWithId =
true;
527 if (unit->objectIsComponent(objectIndex)) {
528 Q_ASSERT(obj->nBindings == 1);
529 const Binding *componentBinding = obj->bindingTable();
530 Q_ASSERT(componentBinding->type() == Binding::Type_Object);
532 return scanObject(componentBinding->value.objectIndex, ScopeDeferred::False);
535 const QQmlPropertyCache::ConstPtr propertyCache = unit->propertyCaches.at(objectIndex);
539 QString defaultPropertyName;
540 const QQmlPropertyData *defaultProperty =
nullptr;
541 if (obj->indexOfDefaultPropertyOrAlias != -1) {
542 const QQmlPropertyCache *parentCache = propertyCache->parent().data();
543 defaultPropertyName = parentCache->defaultPropertyName();
544 defaultProperty = parentCache->defaultProperty();
546 defaultPropertyName = propertyCache->defaultPropertyName();
547 defaultProperty = propertyCache->defaultProperty();
550 QStringList deferredPropertyNames;
551 QStringList immediatePropertyNames;
552 if (!deferredAndImmediatePropertyNames(
553 obj, propertyCache, &deferredPropertyNames, &immediatePropertyNames)) {
557 QQmlCustomParser *customParser =
nullptr;
558 if (
const QV4::ResolvedTypeReference *typeRef
559 = unit->resolvedType(obj->inheritedTypeNameIndex)) {
560 const QQmlType type = typeRef->type();
562 customParser = type.customParser();
565 QQmlPropertyResolver propertyResolver(propertyCache);
567 RuntimeObject *runtimeObject = unit->runtimeDataForWrite()->objectAt(objectIndex);
569 const Binding *binding = obj->bindingTable();
570 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
571 QString name = unit->stringAt(binding->propertyNameIndex);
573 const auto markCustomParser = [&]() {
574 runtimeObject->flags |= RuntimeObject::HasCustomParserBindings;
575 runtimeObject->bindingTable()[i].flags |= RuntimeBinding::IsCustomParserBinding;
579 if (binding->type() == Binding::Type_AttachedProperty) {
580 if (customParser->flags() & QQmlCustomParser::AcceptsAttachedProperties) {
584 }
else if (QQmlSignalNames::isHandlerName(name)
585 && !(customParser->flags() & QQmlCustomParser::AcceptsSignalHandlers)) {
591 const bool hasPropertyData = [&]() {
592 if (name.isEmpty()) {
593 name = defaultPropertyName;
596 }
else if (name.constData()->isUpper()) {
601 bool notInRevision =
false;
602 if (propertyResolver.property(
603 name, ¬InRevision, QQmlPropertyResolver::CheckRevision)) {
612 if (unit->bindingIsSignalHandler(objectIndex, i))
620 bool seenSubObjectWithId =
false;
621 bool isExternal =
false;
622 if (binding->type() >= Binding::Type_Object) {
623 const bool isOwnProperty = hasPropertyData || binding->isAttachedProperty();
624 isExternal = !isOwnProperty && binding->isGroupedProperty();
625 if (isOwnProperty || isExternal) {
626 qSwap(seenObjectWithId, seenSubObjectWithId);
629 const bool isImplicitComponent = unit->implicitComponentForObject(
630 binding->value.objectIndex) != -1;
631 const ScopeDeferred objectScope =
633 && (isExternal || scopeDeferred == ScopeDeferred::True)
634 ? ScopeDeferred::True
635 : ScopeDeferred::False;
636 const bool subObjectValid = scanObject(binding->value.objectIndex, objectScope);
637 qSwap(seenObjectWithId, seenSubObjectWithId);
638 seenObjectWithId |= seenSubObjectWithId;
644 bool isDeferred =
false;
645 if (!immediatePropertyNames.isEmpty() && !immediatePropertyNames.contains(name)) {
646 if (seenSubObjectWithId) {
647 return recordError(binding->location,
648 QQmlTypeCompiler::tr(
"You cannot assign an id to an object "
649 "assigned to a deferred property."));
651 if (isExternal || !disableInternalDeferredProperties())
653 }
else if (!deferredPropertyNames.isEmpty() && deferredPropertyNames.contains(name)) {
654 if (!seenSubObjectWithId && binding->type() != Binding::Type_GroupedProperty) {
655 if (isExternal || !disableInternalDeferredProperties())
660 if (binding->type() >= Binding::Type_Object && isExternal && !isDeferred
664 QQmlTypeCompiler::tr(
"Cannot assign to non-existent property \"%1\"")
669 runtimeObject->bindingTable()[i].flags |= RuntimeBinding::IsDeferredBinding;
670 runtimeObject->flags |= RuntimeObject::HasDeferredBindings;
671 if (binding->type() == Binding::Type_AttachedProperty
672 || binding->type() == Binding::Type_GroupedProperty) {
673 unit->runtimeDataForWrite()->objectAt(binding->value.objectIndex)->flags
674 |= RuntimeObject::PopulatedByDeferredBinding;
682 QV4::CompiledData::CompilationUnit *unit;
683 QQmlTypeLoader *typeLoader;
684 const QQmlImports *imports;
685 QList<QQmlError> errors;
686 bool seenObjectWithId =
false;
692 QV4::CompiledData::CompilationUnit *unit, QQmlTypeLoader *typeLoader,
693 const QQmlImports *imports,
const QV4::CompiledData::DependentTypesHasher &dependencyHasher)
695 return RuntimeDataBuilder(unit, typeLoader, imports).build(dependencyHasher);
Combined button and popup list for selecting options.
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
QQmlRefPointer< QV4::CompiledData::CompilationUnit > compile()