Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmltypecompiler.cpp
Go to the documentation of this file.
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// Qt-Security score:significant
4
6
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>
14
15#include <QtCore/qtyperevision.h>
16
18
19DEFINE_BOOL_CONFIG_OPTION(
20 disableInternalDeferredProperties, QML_DISABLE_INTERNAL_DEFERRED_PROPERTIES);
21
22Q_LOGGING_CATEGORY(lcQmlTypeCompiler, "qt.qml.typecompiler");
23
24QQmlTypeCompiler::QQmlTypeCompiler(QQmlTypeData *typeData, QmlIR::Document *parsedQML)
25 : document(parsedQML), typeData(typeData)
26{
27}
28
30{
31 Q_ASSERT(!document->javaScriptCompilationUnit || !document->javaScriptCompilationUnit->unitData());
32 {
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());
40 return nullptr;
41 }
42 }
43 document->javaScriptCompilationUnit = v4CodeGenerator.generateCompilationUnit(/*generated unit data*/false);
44 }
45
46 // Generate QML compiled type data structures
47
48 QmlIR::QmlUnitGenerator qmlGenerator;
49 qmlGenerator.generate(*document);
50
51 if (!errors.isEmpty())
52 return nullptr;
53
54 return std::move(document->javaScriptCompilationUnit);
55}
56
57void QQmlTypeCompiler::recordError(const QQmlJS::DiagnosticMessage &message)
58{
59 QQmlError error;
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());
64 errors << error;
65}
66
67namespace {
68
69class RuntimeDataBuilder
70{
71public:
72 RuntimeDataBuilder(QV4::CompiledData::CompilationUnit *unit, QQmlTypeLoader *typeLoader,
73 const QQmlImports *imports)
74 : unit(unit), typeLoader(typeLoader), imports(imports)
75 {
76 }
77
78 QList<QQmlError> build(const QV4::CompiledData::DependentTypesHasher &dependencyHasher)
79 {
80 Q_ASSERT(unit->runtimeData);
81 for (int i = 0, count = unit->objectCount(); i < count; ++i) {
82 if (!classifySignalHandlers(i))
83 return errors;
84 }
85
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))
89 return errors;
90 }
91 }
92 scanObject(/*root object*/0, ScopeDeferred::False);
93 if (!errors.isEmpty())
94 return errors;
95
96 for (int i = 0, count = unit->objectCount(); i < count; ++i) {
97 classifyBindingsToAliases(i);
98 computeBindingOrder(i);
99 foldEnums(i);
100 }
101
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));
107 }
108 }
109
110 return errors;
111 }
112
113private:
114 enum class ScopeDeferred : bool { False, True };
115
116 bool isValueBinding(int objectIndex, int bindingIndex) const
117 {
118 switch (unit->objectAt(objectIndex)->bindingTable()[bindingIndex].type()) {
119 case QV4::CompiledData::Binding::Type_AttachedProperty:
120 case QV4::CompiledData::Binding::Type_GroupedProperty:
121 return false;
122 default:
123 return !unit->bindingIsSignalHandler(objectIndex, bindingIndex);
124 }
125 }
126
127 bool classifySignalHandlers(int objectIndex)
128 {
129 const QQmlPropertyCache::ConstPtr cache = unit->propertyCaches.at(objectIndex);
130 if (!cache)
131 return true;
132
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))
138 return true;
139 }
140 }
141
142 return classifySignalHandlers(
143 objectIndex, unit->stringAt(obj->inheritedTypeNameIndex), cache,
144 QQmlPropertyResolver::CheckRevision);
145 }
146
147 bool classifySignalHandlers(
148 int objectIndex, const QString &typeName,
149 const QQmlPropertyCache::ConstPtr &propertyCache,
150 QQmlPropertyResolver::RevisionCheck checkRevision)
151 {
152 using namespace QV4::CompiledData;
153
154 const Object *obj = unit->objectAt(objectIndex);
155 RuntimeBinding *runtimeBindings
156 = unit->runtimeDataForWrite()->objectAt(objectIndex)->bindingTable();
157 QSet<QString> customSignals;
158
159 const Binding *binding = obj->bindingTable();
160 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
161 // Attached objects are visited twice: Once via the attached property and once on their
162 // own.
163 if (unit->bindingIsSignalHandler(objectIndex, i))
164 continue;
165
166 const QString bindingPropertyName = unit->stringAt(binding->propertyNameIndex);
167 const Binding::Type bindingType = binding->type();
168
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;
177 }
178 const QMetaObject *attachedType = type.attachedPropertiesType(typeLoader);
179 if (!attachedType) {
180 return recordError(binding->location,
181 QQmlTypeCompiler::tr("Non-existent attached object"));
182 }
183
184 // Ignore revisions of signals on attached objects. They are not unqualified.
185 if (!classifySignalHandlers(binding->value.objectIndex, bindingPropertyName,
186 QQmlMetaType::propertyCache(attachedType),
187 QQmlPropertyResolver::IgnoreRevision)) {
188 return false;
189 }
190 continue;
191 }
192
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())
200 continue;
201
202 QQmlPropertyResolver resolver(propertyCache);
203 bool notInRevision = false;
204 const QQmlPropertyData *const signal
205 = resolver.signal(signalName, &notInRevision, 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)
210 : nullptr;
211
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 &parameterName : parameterNames) {
222 const QString param = QString::fromUtf8(parameterName);
223 if (param.isEmpty()) {
224 unnamedParameter = true;
225 } else if (unnamedParameter) {
226 return recordError(
227 binding->location,
228 QQmlTypeCompiler::tr("Signal uses unnamed parameter followed by "
229 "named parameter."));
230 } else if (QV4::Compiler::Codegen::isNameGlobal(param)) {
231 return recordError(
232 binding->location,
233 QQmlTypeCompiler::tr("Signal parameter \"%1\" hides global "
234 "variable.").arg(param));
235 }
236 }
237 } else if (isPropertyObserver) {
238 flag = RuntimeBinding::IsPropertyObserver;
239 } else {
240 if (notInRevision) {
241 // Try assigning it as a property later
242 if (signalPropertyData)
243 continue;
244
245 if (const QV4::ResolvedTypeReference *typeRef
246 = unit->resolvedType(obj->inheritedTypeNameIndex);
247 typeRef && typeRef->type().isValid()) {
248 const QQmlType type = typeRef->type();
249 return recordError(
250 binding->location,
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()));
255 }
256 return recordError(
257 binding->location,
258 QQmlTypeCompiler::tr("\"%1.%2\" is not available due to component "
259 "versioning.").arg(typeName, bindingPropertyName));
260 }
261
262 // Try to look up the signal parameter names in the object itself
263
264 // build cache if necessary
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()));
271 }
272
273 // if not found, it's not a signal handler but a regular property assignment
274 if (!customSignals.contains(signalName)
275 && (qPropertyName.isEmpty() || !customSignals.contains(qPropertyName))) {
276 continue;
277 }
278 }
279
280 // Binding object to signal means connect the signal to the object's default method.
281 if (bindingType == Binding::Type_Object) {
282 runtimeBindings[i].flags |= RuntimeBinding::IsSignalHandlerObject;
283 continue;
284 }
285
286 if (bindingType != Binding::Type_Script) {
287 return recordError(
288 binding->location,
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"));
293 }
294
295 runtimeBindings[i].flags |= flag;
296 }
297 return true;
298 }
299
300 void foldEnums(int objectIndex)
301 {
302 using namespace QV4::CompiledData;
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)) {
310 continue;
311 }
312
313 if (const std::optional<int> value = evaluateQualifiedEnum(objectIndex, binding)) {
314 runtimeBindings[i].flags |= RuntimeBinding::IsFoldedEnum;
315 runtimeBindings[i].enumValue = *value;
316 }
317 }
318 }
319
320 std::optional<int> evaluateQualifiedEnum(int objectIndex,
321 const QV4::CompiledData::Binding *binding)
322 {
323 const QQmlPropertyCache::ConstPtr cache = unit->propertyCaches.at(objectIndex);
324 if (!cache)
325 return std::nullopt;
326
327 QQmlPropertyResolver resolver(cache);
328 bool notInRevision = false;
329 const QQmlPropertyData *prop =
330 resolver.property(unit->stringAt(binding->propertyNameIndex), &notInRevision);
331 if (!prop || prop->isQList())
332 return std::nullopt;
333 const bool isIntProp = (prop->propType().id() == QMetaType::Int) && !prop->isEnum();
334 if (!prop->isEnum() && !isIntProp)
335 return std::nullopt;
336
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)));
343 return std::nullopt;
344 }
345
346 const QString string = unit->stringAt(binding->stringIndex);
347 if (string.isEmpty() || !string.constData()->isUpper())
348 return std::nullopt;
349
350 const int dot = string.indexOf(u'.');
351 if (dot == -1 || dot == string.size() - 1)
352 return std::nullopt;
353 const int dot2 = string.indexOf(u'.', dot + 1);
354 if (dot2 != -1 && dot2 != string.size() - 1) {
355 if (!string.at(dot + 1).isUpper())
356 return std::nullopt;
357 if (string.indexOf(u'.', dot2 + 1) != -1)
358 return std::nullopt;
359 }
360
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);
365
366 if (isIntProp)
367 return evaluateEnum(typeName.toString(), scopedEnumName, enumValue);
368
369 QQmlImportResolutionResult resolution;
370 imports->resolveType(typeLoader, typeName, &resolution);
371 const QQmlType type = resolution.type;
372 if (!type.isValid())
373 return std::nullopt;
374
375 QV4::ResolvedTypeReference *tr =
376 unit->resolvedType(unit->objectAt(objectIndex)->inheritedTypeNameIndex);
377 bool useFastPath = type.isValid() && tr && tr->type() == type;
378 QMetaProperty mprop;
379 QMetaEnum menum;
380 if (useFastPath) {
381 mprop = cache->firstCppMetaObject()->property(prop->coreIndex());
382 menum = mprop.enumerator();
383 if (!menum.isScoped() && scopedEnumName.isEmpty()
384 && typeName != QString::fromUtf8(menum.scope())) {
385 useFastPath = false;
386 }
387 }
388
389 int value = 0;
390 bool ok = false;
391 if (useFastPath) {
392 const QByteArray enumName = enumValue.toUtf8();
393 if (menum.isScoped() && !scopedEnumName.isEmpty()
394 && enumName != scopedEnumName.toUtf8()) {
395 return std::nullopt;
396 }
397 value = mprop.isFlagType() ? menum.keysToValue(enumName.constData(), &ok)
398 : menum.keyToValue(enumName.constData(), &ok);
399 } else {
400 value = scopedEnumName.isEmpty()
401 ? type.enumValue(typeLoader, enumValue, &ok)
402 : type.scopedEnumValue(typeLoader, scopedEnumName, enumValue, &ok);
403 }
404 return ok ? std::optional<int>(value) : std::nullopt;
405 }
406
407 std::optional<int> evaluateEnum(const QString &scope, QStringView enumName,
408 QStringView enumValue)
409 {
410 bool ok = false;
411 QQmlImportResolutionResult resolution;
412 imports->resolveType(typeLoader, scope, &resolution);
413 const QQmlType type = resolution.type;
414 if (!type.isValid())
415 return std::nullopt;
416
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;
422 }
423
424 void classifyBindingsToAliases(int objectIndex)
425 {
426 using namespace QV4::CompiledData;
427 const QQmlPropertyCache::ConstPtr propertyCache = unit->propertyCaches.at(objectIndex);
428 if (!propertyCache)
429 return;
430
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();
438
439 const Binding *binding = obj->bindingTable();
440 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
441 if (!isValueBinding(objectIndex, i))
442 continue;
443 bool notInRevision = false;
444 const QQmlPropertyData *pd = binding->propertyNameIndex != 0
445 ? resolver.property(unit->stringAt(binding->propertyNameIndex), &notInRevision)
446 : defaultProperty;
447 if (pd && pd->isAlias())
448 runtimeBindings[i].flags |= RuntimeBinding::IsBindingToAlias;
449 }
450 }
451
452 void computeBindingOrder(int objectIndex)
453 {
454 using namespace QV4::CompiledData;
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;
461 };
462
463 RuntimeBindingIndex *order = runtimeObject->bindingOrder();
464 uint n = 0;
465 const auto collect = [&](auto predicate) {
466 for (quint16 b = 0; b < obj->nBindings; ++b) {
467 if (predicate(b))
468 order[n++] = b;
469 }
470 };
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);
477 }
478
479 bool recordError(const QV4::CompiledData::Location &location, const QString &description)
480 {
481 QQmlError error;
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);
487 return false;
488 }
489
490 bool deferredAndImmediatePropertyNames(
491 const QV4::CompiledData::Object *obj, const QQmlPropertyCache::ConstPtr &propertyCache,
492 QStringList *deferredPropertyNames, QStringList *immediatePropertyNames)
493 {
494 const QMetaObject *mo = propertyCache->firstCppMetaObject();
495 const int deferredNamesIndex = mo->indexOfClassInfo("DeferredPropertyNames");
496 const int immediateNamesIndex = mo->indexOfClassInfo("ImmediatePropertyNames");
497
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."));
503 }
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',');
509 // If the property contains an empty string, all properties shall be deferred.
510 if (immediatePropertyNames->isEmpty())
511 immediatePropertyNames->append(QString());
512 }
513 return true;
514 }
515
516 bool scanObject(int objectIndex, ScopeDeferred scopeDeferred)
517 {
518 using namespace QV4::CompiledData;
519
520 if (objectIndex >= unit->objectCount())
521 return scanObject(unit->resolvedIndex(objectIndex), scopeDeferred);
522
523 const Object *obj = unit->objectAt(objectIndex);
524 if (obj->idNameIndex != 0)
525 seenObjectWithId = true;
526
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);
531 // A Component is separate from its surrounding scope. Its contents cannot be deferred.
532 return scanObject(componentBinding->value.objectIndex, ScopeDeferred::False);
533 }
534
535 const QQmlPropertyCache::ConstPtr propertyCache = unit->propertyCaches.at(objectIndex);
536 if (!propertyCache)
537 return true;
538
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();
545 } else {
546 defaultPropertyName = propertyCache->defaultPropertyName();
547 defaultProperty = propertyCache->defaultProperty();
548 }
549
550 QStringList deferredPropertyNames;
551 QStringList immediatePropertyNames;
552 if (!deferredAndImmediatePropertyNames(
553 obj, propertyCache, &deferredPropertyNames, &immediatePropertyNames)) {
554 return false;
555 }
556
557 QQmlCustomParser *customParser = nullptr;
558 if (const QV4::ResolvedTypeReference *typeRef
559 = unit->resolvedType(obj->inheritedTypeNameIndex)) {
560 const QQmlType type = typeRef->type();
561 if (type.isValid())
562 customParser = type.customParser();
563 }
564
565 QQmlPropertyResolver propertyResolver(propertyCache);
566
567 RuntimeObject *runtimeObject = unit->runtimeDataForWrite()->objectAt(objectIndex);
568
569 const Binding *binding = obj->bindingTable();
570 for (quint32 i = 0; i < obj->nBindings; ++i, ++binding) {
571 QString name = unit->stringAt(binding->propertyNameIndex);
572
573 const auto markCustomParser = [&]() {
574 runtimeObject->flags |= RuntimeObject::HasCustomParserBindings;
575 runtimeObject->bindingTable()[i].flags |= RuntimeBinding::IsCustomParserBinding;
576 };
577
578 if (customParser) {
579 if (binding->type() == Binding::Type_AttachedProperty) {
580 if (customParser->flags() & QQmlCustomParser::AcceptsAttachedProperties) {
581 markCustomParser();
582 continue;
583 }
584 } else if (QQmlSignalNames::isHandlerName(name)
585 && !(customParser->flags() & QQmlCustomParser::AcceptsSignalHandlers)) {
586 markCustomParser();
587 continue;
588 }
589 }
590
591 const bool hasPropertyData = [&]() {
592 if (name.isEmpty()) {
593 name = defaultPropertyName;
594 if (defaultProperty)
595 return true;
596 } else if (name.constData()->isUpper()) {
597 // Upper case names cannot be custom-parsed unless they are attached properties
598 // and the custom parser explicitly accepts them. See above for that case.
599 return false;
600 } else {
601 bool notInRevision = false;
602 if (propertyResolver.property(
603 name, &notInRevision, QQmlPropertyResolver::CheckRevision)) {
604 return true;
605 }
606 }
607
608 if (!customParser)
609 return false;
610
611 // These signal handlers cannot be custom-parsed. The signal exists.
612 if (unit->bindingIsSignalHandler(objectIndex, i))
613 return false;
614
615 // If the property isn't found, we may want to custom-parse the binding.
616 markCustomParser();
617 return false;
618 }();
619
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);
627
628 // Implicit component wrappers are scope boundaries.
629 const bool isImplicitComponent = unit->implicitComponentForObject(
630 binding->value.objectIndex) != -1;
631 const ScopeDeferred objectScope =
632 !isImplicitComponent
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;
639 if (!subObjectValid)
640 return false;
641 }
642 }
643
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."));
650 }
651 if (isExternal || !disableInternalDeferredProperties())
652 isDeferred = true;
653 } else if (!deferredPropertyNames.isEmpty() && deferredPropertyNames.contains(name)) {
654 if (!seenSubObjectWithId && binding->type() != Binding::Type_GroupedProperty) {
655 if (isExternal || !disableInternalDeferredProperties())
656 isDeferred = true;
657 }
658 }
659
660 if (binding->type() >= Binding::Type_Object && isExternal && !isDeferred
661 && !customParser) {
662 return recordError(
663 binding->location,
664 QQmlTypeCompiler::tr("Cannot assign to non-existent property \"%1\"")
665 .arg(name));
666 }
667
668 if (isDeferred) {
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;
675 }
676 }
677 }
678
679 return true;
680 }
681
682 QV4::CompiledData::CompilationUnit *unit;
683 QQmlTypeLoader *typeLoader;
684 const QQmlImports *imports;
685 QList<QQmlError> errors;
686 bool seenObjectWithId = false;
687};
688
689} // namespace
690
691QList<QQmlError> QQmlTypeCompiler::computeRuntimeData(
692 QV4::CompiledData::CompilationUnit *unit, QQmlTypeLoader *typeLoader,
693 const QQmlImports *imports, const QV4::CompiledData::DependentTypesHasher &dependencyHasher)
694{
695 return RuntimeDataBuilder(unit, typeLoader, imports).build(dependencyHasher);
696}
697
698QT_END_NAMESPACE
Combined button and popup list for selecting options.
Definition qjsvalue.h:24
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
QQmlRefPointer< QV4::CompiledData::CompilationUnit > compile()