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
qqmltypedata.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
5#include <private/qqmlcomponentandaliasresolver_p.h>
6#include <private/qqmlengine_p.h>
7#include <private/qqmlirbuilder_p.h>
8#include <private/qqmlirloader_p.h>
9#include <private/qqmlpropertycachecreator_p.h>
10#include <private/qqmlpropertyvalidator_p.h>
11#include <private/qqmlscriptblob_p.h>
12#include <private/qqmlscriptdata_p.h>
13#include <private/qqmltypecompiler_p.h>
14#include <private/qqmltypedata_p.h>
15#include <private/qqmltypeloaderqmldircontent_p.h>
16
17#include <QtCore/qloggingcategory.h>
18#include <QtCore/qcryptographichash.h>
19
20#include <memory>
21
23
24Q_LOGGING_CATEGORY(lcCycle, "qt.qml.typeresolution.cycle", QtWarningMsg)
25
26QString QQmlTypeData::TypeReference::qualifiedName() const
27{
28 QString result;
29 if (!prefix.isEmpty()) {
30 result = prefix + QLatin1Char('.');
31 }
32 result.append(type.qmlTypeName());
33 return result;
34}
35
36QQmlTypeData::QQmlTypeData(const QUrl &url, QQmlTypeLoader *manager)
37 : QQmlNotifyingBlob(url, QmlFile, manager),
38 m_typesResolved(false), m_implicitImportLoaded(false)
39{
40
41}
42
43QQmlTypeData::~QQmlTypeData()
44{
45 m_scripts.clear();
46 m_compositeSingletons.clear();
47 m_resolvedTypes.clear();
48}
49
50QV4::CompiledData::CompilationUnit *QQmlTypeData::compilationUnit() const
51{
52 return m_compiledData.data();
53}
54
55QQmlType QQmlTypeData::qmlType(const QString &inlineComponentName) const
56{
57 if (inlineComponentName.isEmpty())
58 return m_qmlType;
59 return m_inlineComponentData[inlineComponentName].qmlType;
60}
61
62bool QQmlTypeData::tryLoadFromDiskCache()
63{
64 assertTypeLoaderThread();
65
66 if (!m_backupSourceCode.isCacheable())
67 return false;
68
69 if (!m_typeLoader->readCacheFile())
70 return false;
71
72 auto unit = QQml::makeRefPointer<QV4::CompiledData::CompilationUnit>();
73 {
74 QString error;
75 if (!unit->loadFromDisk(url(), m_backupSourceCode.sourceTimeStamp(), &error)) {
76 qCDebug(DBG_DISK_CACHE) << "Error loading" << urlString() << "from disk cache:" << error;
77 return false;
78 }
79 }
80
81 if (unit->unitData()->flags & QV4::CompiledData::Unit::PendingTypeCompilation) {
82 restoreIR(unit);
83 return true;
84 }
85
86 return loadFromDiskCache(unit);
87}
88
89bool QQmlTypeData::loadFromDiskCache(const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit)
90{
91 assertTypeLoaderThread();
92
93 m_compiledData = unit;
94
95 QList<QV4::CompiledData::InlineComponent> ics;
96 for (int i = 0, count = m_compiledData->objectCount(); i < count; ++i) {
97 auto object = m_compiledData->objectAt(i);
98 m_typeReferences.collectFromObject(object);
99 const auto inlineComponentTable = object->inlineComponentTable();
100 for (auto i = 0; i != object->nInlineComponents; ++i) {
101 ics.push_back(inlineComponentTable[i]);
102 }
103 }
104
105 m_importCache->setBaseUrl(finalUrl(), finalUrlString());
106
107 // For remote URLs, we don't delay the loading of the implicit import
108 // because the loading probably requires an asynchronous fetch of the
109 // qmldir (so we can't load it just in time).
110 if (!finalUrl().scheme().isEmpty()) {
111 QUrl qmldirUrl = finalUrl().resolved(QUrl(QLatin1String("qmldir")));
112 if (!QQmlImports::isLocal(qmldirUrl)) {
113 if (!loadImplicitImport())
114 return false;
115
116 // find the implicit import
117 for (quint32 i = 0, count = m_compiledData->importCount(); i < count; ++i) {
118 const QV4::CompiledData::Import *import = m_compiledData->importAt(i);
119 if (m_compiledData->stringAt(import->uriIndex) == QLatin1String(".")
120 && import->qualifierIndex == 0
121 && !import->version.hasMajorVersion()
122 && !import->version.hasMinorVersion()) {
123 QList<QQmlError> errors;
124 auto pendingImport = std::make_shared<PendingImport>(
125 this, import, QQmlImports::ImportNoFlag);
126 pendingImport->precedence = QQmlImportInstance::Implicit;
127 if (!fetchQmldir(qmldirUrl, std::move(pendingImport), 1, &errors)) {
128 setError(errors);
129 return false;
130 }
131 break;
132 }
133 }
134 }
135 }
136
137 for (int i = 0, count = m_compiledData->importCount(); i < count; ++i) {
138 const QV4::CompiledData::Import *import = m_compiledData->importAt(i);
139 QList<QQmlError> errors;
140 if (!addImport(import, {}, &errors)) {
141 Q_ASSERT(errors.size());
142 QQmlError error(errors.takeFirst());
143 error.setUrl(m_importCache->baseUrl());
144 error.setLine(qmlConvertSourceCoordinate<quint32, int>(import->location.line()));
145 error.setColumn(qmlConvertSourceCoordinate<quint32, int>(import->location.column()));
146 errors.prepend(error); // put it back on the list after filling out information.
147 setError(errors);
148 return false;
149 }
150 }
151
152 for (auto&& ic: ics) {
153 QString const nameString = m_compiledData->stringAt(ic.nameIndex);
154 auto importUrl = finalUrl();
155 importUrl.setFragment(nameString);
156 auto import = new QQmlImportInstance();
157 m_importCache->addInlineComponentImport(import, nameString, importUrl);
158 }
159
160 return true;
161}
162
163template<>
164void QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::allocateNamedObjects(
165 const QV4::CompiledData::Object *object) const
166{
167 Q_UNUSED(object);
168}
169
170template<>
171bool QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::markAsComponent(int index) const
172{
173 return m_compiler->objectAt(index)->hasFlag(QV4::CompiledData::Object::IsComponent);
174}
175
176template<>
177void QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::setObjectId(int index) const
178{
179 Q_UNUSED(index)
180 // we cannot sanity-check the index here because bindings are sorted in a different order
181 // in the CU vs the IR.
182}
183
184template<>
185void QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::resolveGeneralizedGroupProperty(
186 const CompiledObject &component, CompiledBinding *binding)
187{
188 // We cannot make it fail here. It might be a custom-parsed property
189 for (int i = 0, count = component.namedObjectsInComponentCount(); i < count; ++i) {
190 const int candidateIndex = component.namedObjectsInComponentTable()[i];
191 if (m_compiler->objectAt(candidateIndex)->idNameIndex == binding->propertyNameIndex) {
192 m_propertyCaches->set(binding->value.objectIndex, m_propertyCaches->at(candidateIndex));
193 return;
194 }
195 }
196}
197
199
200template<>
201typename QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::AliasResolutionResult
202QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::resolveAliasesInObject(
203 const CompiledObject &component, int objectIndex,
204 QQmlPropertyCacheAliasCreator<QV4::CompiledData::CompilationUnit> *aliasCacheCreator,
205 QQmlError *error)
206{
207 const CompiledObject *obj = m_compiler->objectAt(objectIndex);
208 int aliasIndex = 0;
209 const auto doAppendAlias = [&](const QV4::CompiledData::Alias *alias, int encodedIndex,
210 int resolvedTargetObjectId) {
211 return appendAliasToPropertyCache(
212 &component, alias, objectIndex, aliasIndex++, encodedIndex,
213 resolvedTargetObjectId, aliasCacheCreator, error);
214 };
215
216 const auto handleDeepAlias = [&](
217 const QV4::CompiledData::Alias *alias, const QQmlPropertyCache::ConstPtr &propertyCache,
218 int coreIndex, QStringView subProperty, int resolvedTargetObjectId)
219 {
220 const QQmlPropertyResolver resolver = QQmlPropertyResolver(propertyCache);
221 const QQmlPropertyData *actualProperty = resolver.property(subProperty.toString());
222 if (!actualProperty)
223 return DeepAliasResult::NoProperty;
224
225 if (doAppendAlias(
226 alias, QQmlPropertyIndex(coreIndex, actualProperty->coreIndex()).toEncoded(),
227 resolvedTargetObjectId)) {
228 return DeepAliasResult::Success;
229 }
230
231 return DeepAliasResult::CannotAppend;
232 };
233
234 for (auto alias = obj->aliasesBegin(), end = obj->aliasesEnd(); alias != end; ++alias) {
235 if (resolvedAliases.contains(alias)) {
236 ++aliasIndex;
237 continue;
238 }
239
240 int targetObjectIndex = -1;
241 for (int i = 0, end = component.namedObjectsInComponentCount(); i < end; ++i) {
242 const int candidateIndex = component.namedObjectsInComponentTable()[i];
243 if (m_compiler->objectAt(candidateIndex)->idNameIndex == alias->idIndex()) {
244 targetObjectIndex = candidateIndex;
245 break;
246 }
247 }
248 if (targetObjectIndex == -1) {
249 *error = qQmlCompileError(
250 alias->referenceLocation(),
251 tr("Invalid alias reference. Unable to find id \"%1\"")
252 .arg(stringAt(alias->idIndex())));
253 break;
254 }
255 const QV4::CompiledData::Object *targetObject = m_compiler->objectAt(targetObjectIndex);
256 const int resolvedTargetObjectId = targetObject->objectId();
257
258 QStringView property;
259 QStringView subProperty;
260
261 const QString aliasPropertyValue = stringAt(alias->propertyNameIndex());
262 const int propertySeparator = aliasPropertyValue.indexOf(QLatin1Char('.'));
263 if (propertySeparator != -1) {
264 property = QStringView{aliasPropertyValue}.left(propertySeparator);
265 subProperty = QStringView{aliasPropertyValue}.mid(propertySeparator + 1);
266 } else {
267 property = QStringView(aliasPropertyValue);
268 }
269
270 if (property.isEmpty()) {
271 if (doAppendAlias(alias, -1, resolvedTargetObjectId))
272 continue;
273 return SomeAliasesResolved;
274 }
275
276 Q_ASSERT(!property.isEmpty());
277 QQmlPropertyCache::ConstPtr targetCache = m_propertyCaches->at(targetObjectIndex);
278 Q_ASSERT(targetCache);
279
280 const QQmlPropertyResolver resolver(targetCache);
281 const QQmlPropertyData *targetProperty = resolver.property(property.toString());
282 if (!targetProperty)
283 return SomeAliasesResolved;
284
285 const int coreIndex = targetProperty->coreIndex();
286 if (subProperty.isEmpty()) {
287 if (doAppendAlias(
288 alias, QQmlPropertyIndex(coreIndex).toEncoded(), resolvedTargetObjectId)) {
289 continue;
290 }
291 return SomeAliasesResolved;
292 }
293
294 if (const QMetaObject *valueTypeMetaObject
295 = QQmlMetaType::metaObjectForValueType(targetProperty->propType())) {
296 const int valueTypeIndex = valueTypeMetaObject->indexOfProperty(
297 subProperty.toString().toUtf8().constData());
298 if (valueTypeIndex == -1)
299 return SomeAliasesResolved;
300
301 if (doAppendAlias(
302 alias, QQmlPropertyIndex(coreIndex, valueTypeIndex).toEncoded(),
303 resolvedTargetObjectId)) {
304 continue;
305 }
306
307 return SomeAliasesResolved;
308 }
309
310 Q_ASSERT(subProperty.at(0).isLower());
311
312 bool foundDeepAliasInBindings = false;
313 for (auto it = targetObject->bindingsBegin(); it != targetObject->bindingsEnd(); ++it) {
314 if (m_compiler->stringAt(it->propertyNameIndex) != property)
315 continue;
316
317 switch (handleDeepAlias(
318 alias, m_propertyCaches->at(it->value.objectIndex), coreIndex, subProperty,
319 resolvedTargetObjectId)) {
320 case DeepAliasResult::NoProperty:
321 continue;
322 case DeepAliasResult::CannotAppend:
323 return SomeAliasesResolved;
324 case DeepAliasResult::Success:
325 foundDeepAliasInBindings = true;
326 break;
327 }
328
329 break;
330 }
331
332 if (foundDeepAliasInBindings)
333 continue;
334
335 const QQmlPropertyCache::ConstPtr typeCache
336 = QQmlMetaType::propertyCacheForType(targetProperty->propType());
337 if (!typeCache)
338 return SomeAliasesResolved;
339
340 switch (handleDeepAlias(alias, typeCache, coreIndex, subProperty, resolvedTargetObjectId)) {
341 case DeepAliasResult::NoProperty:
342 case DeepAliasResult::CannotAppend:
343 return SomeAliasesResolved;
344 case DeepAliasResult::Success:
345 break;
346 }
347 }
348
349 return AllAliasesResolved;
350}
351
352template<>
353bool QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::wrapImplicitComponent(
354 const QV4::CompiledData::Binding *binding)
355{
356 // This should have been done when creating the CU.
357 Q_UNUSED(binding);
358 return false;
359}
360
361QQmlError QQmlTypeData::createTypeAndPropertyCaches(
362 const QQmlRefPointer<QQmlTypeNameCache> &typeNameCache,
363 const QV4::CompiledData::ResolvedTypeReferenceMap &resolvedTypeCache)
364{
365 assertTypeLoaderThread();
366
367 Q_ASSERT(m_compiledData);
368 m_compiledData->typeNameCache = typeNameCache;
369 m_compiledData->resolvedTypes = resolvedTypeCache;
370 m_compiledData->inlineComponentData = m_inlineComponentData;
371 m_compiledData->qmlType = m_qmlType;
372
373 QQmlPendingGroupPropertyBindings pendingGroupPropertyBindings;
374
375 {
376 QQmlPropertyCacheCreator<QV4::CompiledData::CompilationUnit> propertyCacheCreator(
377 &m_compiledData->propertyCaches, &pendingGroupPropertyBindings, m_typeLoader,
378 m_compiledData.data(), m_importCache.data(), typeClassName());
379
380 QQmlError error = propertyCacheCreator.verifyNoICCycle();
381 if (error.isValid())
382 return error;
383
384 QQmlPropertyCacheCreatorBase::IncrementalResult result;
385 do {
386 result = propertyCacheCreator.buildMetaObjectsIncrementally();
387 if (result.error.isValid()) {
388 return result.error;
389 } else {
390 QQmlComponentAndAliasResolver resolver(
391 m_compiledData.data(), &m_compiledData->propertyCaches);
392 if (const QQmlError error = resolver.resolve(result.processedRoot);
393 error.isValid()) {
394 return error;
395 }
396 pendingGroupPropertyBindings.resolveMissingPropertyCaches(
397 &m_compiledData->propertyCaches);
398 pendingGroupPropertyBindings.clear(); // anything that can be processed is now processed
399 }
400
401 } while (result.canResume);
402 }
403
404 pendingGroupPropertyBindings.resolveMissingPropertyCaches(&m_compiledData->propertyCaches);
405 return QQmlError();
406}
407
408// local helper function for inline components
409namespace {
410using InlineComponentData = QV4::CompiledData::InlineComponentData;
411
412template<typename ObjectContainer>
413void setupICs(
414 const ObjectContainer &container, QHash<QString, InlineComponentData> *icData,
415 const QUrl &baseUrl,
416 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit) {
417 Q_ASSERT(icData->empty());
418 for (int i = 0; i != container->objectCount(); ++i) {
419 auto root = container->objectAt(i);
420 for (auto it = root->inlineComponentsBegin(); it != root->inlineComponentsEnd(); ++it) {
421 // We cannot re-use a previously finalized inline component type here. We need our own.
422 // We can and should re-use speculative type references, though.
423 InlineComponentData icDatum(
424 QQmlMetaType::findOrCreateFactualInlineComponentType(
425 baseUrl, container->stringAt(it->nameIndex), compilationUnit),
426 int(it->objectIndex), int(it->nameIndex));
427
428 icData->insert(container->stringAt(it->nameIndex), icDatum);
429 }
430 }
431};
432}
433
434bool QQmlTypeData::checkScripts()
435{
436 // Check all script dependencies for errors
437 for (int ii = 0; ii < m_scripts.size(); ++ii) {
438 const ScriptReference &script = m_scripts.at(ii);
439 Q_ASSERT(script.script->isCompleteOrError());
440 if (script.script->isError()) {
441 createError(
442 script,
443 QQmlTypeLoader::tr("Script %1 unavailable").arg(script.script->urlString()));
444 return false;
445 }
446 }
447 return true;
448}
449
450void QQmlTypeData::createError(const TypeReference &type, const QString &message)
451{
452 createError(type, message, type.typeData ? type.typeData->errors() : QList<QQmlError>());
453}
454
455void QQmlTypeData::createError(const ScriptReference &script, const QString &message)
456{
457 createError(script, message, script.script ? script.script->errors() : QList<QQmlError>());
458}
459
460bool QQmlTypeData::checkDependencies()
461{
462 // Check all type dependencies for errors
463 for (auto it = std::as_const(m_resolvedTypes).begin(), end = std::as_const(m_resolvedTypes).end();
464 it != end; ++it) {
465 const TypeReference &type = *it;
466 Q_ASSERT(!type.typeData
467 || type.typeData->isCompleteOrError()
468 || type.type.isInlineComponentType());
469
470 if (type.typeData && type.typeData->isError()) {
471 const QString &typeName = stringAt(it.key());
472 createError(type, QQmlTypeLoader::tr("Type %1 unavailable").arg(typeName));
473 return false;
474 }
475
476 if (!type.selfReference && type.type.isInlineComponentType()) {
477 const QString icName = type.type.elementName();
478 Q_ASSERT(!icName.isEmpty());
479
480 // We have a CU here. Check if the inline component exists.
481 if (type.typeData && type.typeData->compilationUnit()->inlineComponentId(icName) >= 0)
482 return true;
483
484 const QString typeName = stringAt(it.key());
485 const qsizetype lastDot = typeName.lastIndexOf(u'.');
486 createError(
487 type,
488 QQmlTypeLoader::tr("Type %1 has no inline component type called %2")
489 .arg(QStringView{typeName}.left(lastDot), icName));
490 return false;
491 }
492 }
493
494 return true;
495}
496
497bool QQmlTypeData::checkCompositeSingletons()
498{
499 // Check all composite singleton type dependencies for errors
500 for (int ii = 0; ii < m_compositeSingletons.size(); ++ii) {
501 const TypeReference &type = m_compositeSingletons.at(ii);
502 Q_ASSERT(!type.typeData || type.typeData->isCompleteOrError());
503 if (type.typeData && type.typeData->isError()) {
504 QString typeName = type.type.qmlTypeName();
505 createError(type, QQmlTypeLoader::tr("Type %1 unavailable").arg(typeName));
506 return false;
507 }
508 }
509
510 return true;
511}
512
513void QQmlTypeData::createQQmlType()
514{
515 if (QQmlPropertyCacheCreatorBase::canCreateClassNameTypeByUrl(finalUrl())) {
516 const bool isSingleton = m_document
517 ? m_document.data()->isSingleton()
518 : (m_compiledData->unitData()->flags & QV4::CompiledData::Unit::IsSingleton);
519 m_qmlType = QQmlMetaType::findCompositeType(
520 url(), m_compiledData, isSingleton
521 ? QQmlMetaType::Singleton
522 : QQmlMetaType::NonSingleton);
523 m_typeClassName = QByteArray(m_qmlType.typeId().name()).chopped(1);
524 }
525}
526
527bool QQmlTypeData::rebuildFromSource()
528{
529 // Clear and re-build everything.
530
531 m_typeReferences.clear();
532 m_scripts.clear();
533 m_namespaces.clear();
534 m_compositeSingletons.clear();
535
536 m_resolvedTypes.clear();
537 m_typesResolved = false;
538
539 m_qmlType = QQmlType();
540 m_typeClassName.clear();
541
542 m_inlineComponentData.clear();
543 m_compiledData.reset();
544
545 m_implicitImportLoaded = false;
546
547 m_importCache.adopt(new QQmlImports);
548 m_unresolvedImports.clear();
549
550 if (!loadFromSource())
551 return false;
552
553 continueLoadFromIR();
554
555 if (!resolveTypes())
556 return false;
557
558 if (!checkScripts())
559 return false;
560
561 if (!checkDependencies())
562 return false;
563
564 if (!checkCompositeSingletons())
565 return false;
566
567 createQQmlType();
568
569 setupICs(m_document, &m_inlineComponentData, finalUrl(), m_compiledData);
570 return true;
571}
572
573void QQmlTypeData::done()
574{
575 assertTypeLoaderThread();
576
577 auto cleanup = qScopeGuard([this]{
578 m_backupSourceCode = SourceCodeData();
579 m_document.reset();
580 m_typeReferences.clear();
581 if (isError()) {
582 const auto encounteredErrors = errors();
583 for (const QQmlError &e : encounteredErrors)
584 qCDebug(DBG_DISK_CACHE) << e.toString();
585 m_compiledData.reset();
586 // Clear resolved types, scripts, and composite singletons to break
587 // potential circular references (e.g., A depends on B, B depends on A)
588 m_resolvedTypes.clear();
589 m_compositeSingletons.clear();
590 m_scripts.clear();
591 }
592 });
593
594 if (isError())
595 return;
596
597 if (!checkScripts())
598 return;
599
600 if (!checkDependencies())
601 return;
602
603 if (!checkCompositeSingletons())
604 return;
605
606 createQQmlType();
607
608 if (m_document)
609 setupICs(m_document, &m_inlineComponentData, finalUrl(), m_compiledData);
610 else
611 setupICs(m_compiledData, &m_inlineComponentData, finalUrl(), m_compiledData);
612
613 QV4::CompiledData::ResolvedTypeReferenceMap resolvedTypeCache;
614 QQmlRefPointer<QQmlTypeNameCache> typeNameCache;
615
616 // If we've pulled the CU from the memory cache, we don't need to do any verification.
617 const bool verifyCaches = !m_compiledData
618 || (m_compiledData->resolvedTypes.isEmpty() && !m_compiledData->typeNameCache);
619
620 if (verifyCaches) {
621 QQmlError error = buildTypeResolutionCaches(&typeNameCache, &resolvedTypeCache);
622 if (error.isValid()) {
623 setError(error);
624 qDeleteAll(resolvedTypeCache);
625 return;
626 }
627 }
628
629 const auto dependencyHasher = [&resolvedTypeCache, this]() {
630 return typeLoader()->hashDependencies(&resolvedTypeCache, m_compositeSingletons);
631 };
632
633 // verify if any dependencies changed if we're using a cache
634 if (m_document.isNull() && verifyCaches) {
635 const QQmlError error = createTypeAndPropertyCaches(typeNameCache, resolvedTypeCache);
636 if (error.isValid() || !m_compiledData->verifyChecksum(dependencyHasher)) {
637
638 if (error.isValid()) {
639 qCDebug(DBG_DISK_CACHE)
640 << "Failed to create property caches for"
641 << m_compiledData->fileName()
642 << "because" << error.description();
643 } else {
644 qCDebug(DBG_DISK_CACHE)
645 << "Checksum mismatch for cached version of"
646 << m_compiledData->fileName();
647 }
648
649 resolvedTypeCache.clear();
650 typeNameCache.reset();
651
652 if (!rebuildFromSource())
653 return;
654
655 const QQmlError error = buildTypeResolutionCaches(&typeNameCache, &resolvedTypeCache);
656 if (error.isValid()) {
657 setError(error);
658 qDeleteAll(resolvedTypeCache);
659 return;
660 }
661 }
662 }
663
664 if (!m_document.isNull()) {
665 Q_ASSERT(verifyCaches);
666 // Compile component
667 compile(typeNameCache, &resolvedTypeCache, dependencyHasher);
668 if (isError())
669 return;
670 }
671
672 {
673 m_compiledData->inlineComponentData = m_inlineComponentData;
674 {
675 // Sanity check property bindings
676 QQmlPropertyValidator validator(typeLoader(), m_importCache.data(), m_compiledData);
677 QList<QQmlError> errors = validator.validate();
678 if (!errors.isEmpty()) {
679 setError(errors);
680 return;
681 }
682 }
683
684 m_compiledData->finalizeCompositeType(qmlType());
685 }
686
687 {
688 QQmlType type = QQmlMetaType::qmlType(finalUrl());
689 if (m_compiledData && m_compiledData->unitData()->flags & QV4::CompiledData::Unit::IsSingleton) {
690 if (!type.isValid()) {
691 QQmlError error;
692 error.setDescription(QQmlTypeLoader::tr("No matching type found, pragma Singleton files cannot be used by QQmlComponent."));
693 setError(error);
694 return;
695 } else if (!type.isCompositeSingleton()) {
696 QQmlError error;
697 error.setDescription(QQmlTypeLoader::tr("pragma Singleton used with a non composite singleton type %1").arg(type.qmlTypeName()));
698 setError(error);
699 return;
700 }
701 } else {
702 // If the type is CompositeSingleton but there was no pragma Singleton in the
703 // QML file, lets report an error.
704 if (type.isValid() && type.isCompositeSingleton()) {
705 QString typeName = type.qmlTypeName();
706 setError(QQmlTypeLoader::tr("qmldir defines type as singleton, but no pragma Singleton found in type %1.").arg(typeName));
707 return;
708 }
709 }
710 }
711
712 {
713 // Collect imported scripts
714 m_compiledData->dependentScripts.reserve(m_scripts.size());
715 for (int scriptIndex = 0; scriptIndex < m_scripts.size(); ++scriptIndex) {
716 const QQmlTypeData::ScriptReference &script = m_scripts.at(scriptIndex);
717
718 QStringView qualifier(script.qualifier);
719 QString enclosingNamespace;
720
721 const int lastDotIndex = qualifier.lastIndexOf(QLatin1Char('.'));
722 if (lastDotIndex != -1) {
723 enclosingNamespace = qualifier.left(lastDotIndex).toString();
724 qualifier = qualifier.mid(lastDotIndex+1);
725 }
726
727 m_compiledData->typeNameCache->add(
728 qualifier.toString(), scriptIndex, enclosingNamespace);
729 QQmlRefPointer<QQmlScriptData> scriptData = script.script->scriptData();
730 m_compiledData->dependentScripts << scriptData;
731 }
732 }
733}
734
735bool QQmlTypeData::loadImplicitImport()
736{
737 assertTypeLoaderThread();
738
739 m_implicitImportLoaded = true; // Even if we hit an error, count as loaded (we'd just keep hitting the error)
740
741 m_importCache->setBaseUrl(finalUrl(), finalUrlString());
742
743 // For local urls, add an implicit import "." as most overridden lookup.
744 // This will also trigger the loading of the qmldir and the import of any native
745 // types from available plugins.
746 QList<QQmlError> implicitImportErrors;
747 QString localQmldir;
748 m_importCache->addImplicitImport(typeLoader(), &localQmldir, &implicitImportErrors);
749
750 // When loading with QQmlImports::ImportImplicit, the imports are _appended_ to the namespace
751 // in the order they are loaded. Therefore, the addImplicitImport above gets the highest
752 // precedence. This is in contrast to normal priority imports. Those are _prepended_ in the
753 // order they are loaded.
754 if (!localQmldir.isEmpty()) {
755 const QQmlTypeLoaderQmldirContent qmldir = typeLoader()->qmldirContent(localQmldir);
756 const QList<QQmlDirParser::Import> moduleImports
757 = QQmlMetaType::moduleImports(qmldir.typeNamespace(), QTypeRevision())
758 + qmldir.imports();
759 loadDependentImports(moduleImports, QString(), QTypeRevision(),
760 QQmlImportInstance::Implicit + 1, QQmlImports::ImportNoFlag,
761 &implicitImportErrors);
762 }
763
764 if (!implicitImportErrors.isEmpty()) {
765 setError(implicitImportErrors);
766 return false;
767 }
768
769 return true;
770}
771
772void QQmlTypeData::dataReceived(const SourceCodeData &data)
773{
774 assertTypeLoaderThread();
775
776 m_backupSourceCode = data;
777
778 if (tryLoadFromDiskCache())
779 return;
780
781 if (isError())
782 return;
783
784 if (!m_backupSourceCode.exists() || m_backupSourceCode.isEmpty()) {
785 if (m_cachedUnitStatus == QQmlMetaType::CachedUnitLookupError::VersionMismatch)
786 setError(QQmlTypeLoader::tr("File was compiled ahead of time with an incompatible version of Qt and the original file cannot be found. Please recompile"));
787 else if (!m_backupSourceCode.exists())
788 setError(QQmlTypeLoader::tr("No such file or directory"));
789 else
790 setError(QQmlTypeLoader::tr("File is empty"));
791 return;
792 }
793
794 if (!loadFromSource())
795 return;
796
797 continueLoadFromIR();
798}
799
800void QQmlTypeData::initializeFromCachedUnit(const QQmlPrivate::CachedQmlUnit *unit)
801{
802 assertTypeLoaderThread();
803
804 if (unit->qmlData->qmlUnit()->nObjects == 0) {
805 setError(QQmlTypeLoader::tr("Cached QML Unit has no objects"));
806 return;
807 }
808
809 m_document.reset(
810 new QmlIR::Document(urlString(), finalUrlString(), m_typeLoader->isDebugging()));
811 QQmlIRLoader loader(unit->qmlData, m_document.data());
812 loader.load();
813 m_document->javaScriptCompilationUnit
814 = QQmlRefPointer<QV4::CompiledData::CompilationUnit>(
815 new QV4::CompiledData::CompilationUnit(unit->qmlData, unit->aotCompiledFunctions),
816 QQmlRefPointer<QV4::CompiledData::CompilationUnit>::Adopt);
817 continueLoadFromIR();
818}
819
820bool QQmlTypeData::loadFromSource()
821{
822 assertTypeLoaderThread();
823
824 m_document.reset(
825 new QmlIR::Document(urlString(), finalUrlString(), m_typeLoader->isDebugging()));
826 m_document->jsModule.sourceTimeStamp = m_backupSourceCode.sourceTimeStamp();
827 QmlIR::IRBuilder compiler;
828
829 QString sourceError;
830 const QString source = m_backupSourceCode.readAll(&sourceError);
831 if (!sourceError.isEmpty()) {
832 setError(sourceError);
833 return false;
834 }
835
836 if (!compiler.generateFromQml(source, finalUrlString(), m_document.data())) {
837 QList<QQmlError> errors;
838 errors.reserve(compiler.errors.size());
839 for (const QQmlJS::DiagnosticMessage &msg : std::as_const(compiler.errors)) {
840 QQmlError e;
841 e.setUrl(url());
842 e.setLine(qmlConvertSourceCoordinate<quint32, int>(msg.loc.startLine));
843 e.setColumn(qmlConvertSourceCoordinate<quint32, int>(msg.loc.startColumn));
844 e.setDescription(msg.message);
845 errors << e;
846 }
847 setError(errors);
848 return false;
849 }
850 return true;
851}
852
853void QQmlTypeData::restoreIR(const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit)
854{
855 assertTypeLoaderThread();
856
857 m_document.reset(
858 new QmlIR::Document(urlString(), finalUrlString(), m_typeLoader->isDebugging()));
859 QQmlIRLoader loader(unit->unitData(), m_document.data());
860 loader.load();
861 m_document->javaScriptCompilationUnit = unit;
862 continueLoadFromIR();
863}
864
865void QQmlTypeData::continueLoadFromIR()
866{
867 assertTypeLoaderThread();
868
869 for (auto const& object: std::as_const(m_document->objects)) {
870 for (auto it = object->inlineComponentsBegin(); it != object->inlineComponentsEnd(); ++it) {
871 QString const nameString = m_document->stringAt(it->nameIndex);
872 auto importUrl = finalUrl();
873 importUrl.setFragment(nameString);
874 auto import = new QQmlImportInstance(); // Note: The cache takes ownership of the QQmlImportInstance
875 m_importCache->addInlineComponentImport(import, nameString, importUrl);
876 }
877 }
878
879 m_typeReferences.collectFromObjects(m_document->objects.constBegin(), m_document->objects.constEnd());
880 m_importCache->setBaseUrl(finalUrl(), finalUrlString());
881
882 // For remote URLs, we don't delay the loading of the implicit import
883 // because the loading probably requires an asynchronous fetch of the
884 // qmldir (so we can't load it just in time).
885 if (!finalUrl().scheme().isEmpty()) {
886 QUrl qmldirUrl = finalUrl().resolved(QUrl(QLatin1String("qmldir")));
887 if (!QQmlImports::isLocal(qmldirUrl)) {
888 if (!loadImplicitImport())
889 return;
890 // This qmldir is for the implicit import
891 auto implicitImport = std::make_shared<PendingImport>();
892 implicitImport->uri = QLatin1String(".");
893 implicitImport->version = QTypeRevision();
894 QList<QQmlError> errors;
895
896 if (!fetchQmldir(qmldirUrl, implicitImport, 1, &errors)) {
897 setError(errors);
898 return;
899 }
900 }
901 }
902
903 QList<QQmlError> errors;
904
905 for (const QV4::CompiledData::Import *import : std::as_const(m_document->imports)) {
906 if (!addImport(import, {}, &errors)) {
907 Q_ASSERT(errors.size());
908
909 // We're only interested in the chronoligically last error. The previous
910 // errors might be from unsuccessfully trying to load a module from the
911 // resource file system.
912 QQmlError error = errors.first();
913 error.setUrl(m_importCache->baseUrl());
914 error.setLine(qmlConvertSourceCoordinate<quint32, int>(import->location.line()));
915 error.setColumn(qmlConvertSourceCoordinate<quint32, int>(import->location.column()));
916 setError(error);
917 return;
918 }
919 }
920}
921
922void QQmlTypeData::allDependenciesDone()
923{
924 assertTypeLoaderThread();
925
926 QQmlTypeLoader::Blob::allDependenciesDone();
927
928 if (!m_typesResolved)
929 resolveTypes();
930}
931
932QString QQmlTypeData::stringAt(int index) const
933{
934 if (m_compiledData)
935 return m_compiledData->stringAt(index);
936 return m_document->jsGenerator.stringTable.stringForIndex(index);
937}
938
939void QQmlTypeData::compile(const QQmlRefPointer<QQmlTypeNameCache> &typeNameCache,
940 QV4::CompiledData::ResolvedTypeReferenceMap *resolvedTypeCache,
941 const QV4::CompiledData::DependentTypesHasher &dependencyHasher)
942{
943 assertTypeLoaderThread();
944
945 Q_ASSERT(m_compiledData.isNull());
946
947 const bool typeRecompilation = m_document
948 && m_document->javaScriptCompilationUnit
949 && m_document->javaScriptCompilationUnit->unitData()
950 && (m_document->javaScriptCompilationUnit->unitData()->flags
951 & QV4::CompiledData::Unit::PendingTypeCompilation);
952
953 QQmlTypeCompiler compiler(
954 typeLoader(), this, m_document.data(), resolvedTypeCache, dependencyHasher);
955 auto compilationUnit = compiler.compile();
956 if (!compilationUnit) {
957 qDeleteAll(*resolvedTypeCache);
958 resolvedTypeCache->clear();
959 setError(compiler.compilationErrors());
960 return;
961 }
962
963 const bool trySaveToDisk = m_typeLoader->writeCacheFile() && !typeRecompilation;
964 if (trySaveToDisk) {
965 QString errorString;
966 if (compilationUnit->saveToDisk(url(), &errorString)) {
967 QString error;
968 if (!compilationUnit->loadFromDisk(url(), m_backupSourceCode.sourceTimeStamp(), &error)) {
969 // ignore error, keep using the in-memory compilation unit.
970 }
971 } else {
972 qCDebug(DBG_DISK_CACHE) << "Error saving cached version of"
973 << compilationUnit->fileName() << "to disk:" << errorString;
974 }
975 }
976
977 m_compiledData = std::move(compilationUnit);
978 m_compiledData->typeNameCache = typeNameCache;
979 m_compiledData->resolvedTypes = *resolvedTypeCache;
980 m_compiledData->propertyCaches = std::move(*compiler.propertyCaches());
981 Q_ASSERT(m_compiledData->propertyCaches.count()
982 == static_cast<int>(m_compiledData->objectCount()));
983}
984
985bool QQmlTypeData::resolveTypes()
986{
987 assertTypeLoaderThread();
988
989 Q_ASSERT(!m_typesResolved);
990
991 // Check that all imports were resolved
992 QList<QQmlError> errors;
993 auto it = m_unresolvedImports.constBegin(), end = m_unresolvedImports.constEnd();
994 for ( ; it != end; ++it) {
995 const PendingImportPtr &import = *it;
996 if (import->priority != 0)
997 continue;
998
999 // If the import was potentially remote and all the network requests have failed,
1000 // we now know that there is no qmldir. We can register its types.
1001 if (registerPendingTypes(import))
1002 continue;
1003
1004 // This import was not resolved
1005 QQmlError error;
1006 error.setDescription(QQmlTypeLoader::tr("module \"%1\" is not installed").arg(import->uri));
1007 error.setUrl(m_importCache->baseUrl());
1008 error.setLine(qmlConvertSourceCoordinate<quint32, int>(
1009 import->location.line()));
1010 error.setColumn(qmlConvertSourceCoordinate<quint32, int>(
1011 import->location.column()));
1012 errors.prepend(error);
1013 }
1014
1015 if (errors.size()) {
1016 setError(errors);
1017 return false;
1018 }
1019
1020 // Load the implicit import since it may have additional scripts.
1021 if (!m_implicitImportLoaded && !loadImplicitImport())
1022 return false;
1023
1024 // Add any imported scripts to our resolved set
1025 const auto resolvedScripts = m_importCache->resolvedScripts();
1026 for (const QQmlImports::ScriptReference &script : resolvedScripts) {
1027 QQmlRefPointer<QQmlScriptBlob> blob
1028 = typeLoader()->getScript(script.location, script.fileName);
1029 addDependency(blob.data());
1030
1031 ScriptReference ref;
1032 //ref.location = ...
1033 if (!script.qualifier.isEmpty())
1034 {
1035 ref.qualifier = script.qualifier + QLatin1Char('.') + script.nameSpace;
1036 // Add a reference to the enclosing namespace
1037 m_namespaces.insert(script.qualifier);
1038 } else {
1039 ref.qualifier = script.nameSpace;
1040 }
1041
1042 ref.script = blob;
1043 m_scripts << ref;
1044 }
1045
1046 // Lets handle resolved composite singleton types
1047 const auto resolvedCompositeSingletons = m_importCache->resolvedCompositeSingletons();
1048 for (const QQmlImports::CompositeSingletonReference &csRef : resolvedCompositeSingletons) {
1049 TypeReference ref;
1050 QString typeName;
1051 if (!csRef.prefix.isEmpty()) {
1052 typeName = csRef.prefix + QLatin1Char('.') + csRef.typeName;
1053 // Add a reference to the enclosing namespace
1054 m_namespaces.insert(csRef.prefix);
1055 } else {
1056 typeName = csRef.typeName;
1057 }
1058
1059 QTypeRevision version = csRef.version;
1060 if (!resolveType(typeName, version, ref, -1, -1, true, QQmlType::CompositeSingletonType))
1061 return false;
1062
1063 if (ref.type.isCompositeSingleton()) {
1064 ref.typeData = typeLoader()->getType(ref.type.sourceUrl());
1065 if (ref.typeData->isWaiting() || m_waitingOnMe.contains(ref.typeData.data())) {
1066 qCDebug(lcCycle) << "Possible cyclic dependency detected between"
1067 << ref.typeData->urlString() << "and" << urlString();
1068 continue;
1069 }
1070 addDependency(ref.typeData.data());
1071 ref.prefix = csRef.prefix;
1072
1073 m_compositeSingletons << ref;
1074 }
1075 }
1076
1077 for (auto unresolvedRef = m_typeReferences.constBegin(), end = m_typeReferences.constEnd();
1078 unresolvedRef != end; ++unresolvedRef) {
1079
1080 TypeReference ref; // resolved reference
1081
1082 const bool reportErrors = unresolvedRef->errorWhenNotFound;
1083
1084 QTypeRevision version;
1085
1086 const QString name = stringAt(unresolvedRef.key());
1087
1088 bool *selfReferenceDetection = unresolvedRef->needsCreation ? nullptr : &ref.selfReference;
1089
1090 if (!resolveType(name, version, ref, unresolvedRef->location.line(),
1091 unresolvedRef->location.column(), reportErrors,
1092 QQmlType::AnyRegistrationType, selfReferenceDetection) && reportErrors)
1093 return false;
1094
1095 if (ref.selfReference) {
1096 // nothing to do
1097 } else if (ref.type.isComposite()) {
1098 ref.typeData = typeLoader()->getType(ref.type.sourceUrl());
1099 addDependency(ref.typeData.data());
1100 } else if (ref.type.isInlineComponentType()) {
1101 QUrl containingTypeUrl = ref.type.sourceUrl();
1102 Q_ASSERT(!containingTypeUrl.isEmpty());
1103 if (QQmlMetaType::equalBaseUrls(finalUrl(), containingTypeUrl)) {
1104 ref.selfReference = true;
1105 } else {
1106 containingTypeUrl.setFragment(QString());
1107 auto typeData = typeLoader()->getType(containingTypeUrl);
1108 Q_ASSERT(typeData.data() != this);
1109 ref.typeData = typeData;
1110 addDependency(typeData.data());
1111 }
1112 }
1113
1114 ref.version = version;
1115 ref.location = unresolvedRef->location;
1116 ref.needsCreation = unresolvedRef->needsCreation;
1117 m_resolvedTypes.insert(unresolvedRef.key(), ref);
1118 }
1119
1120 m_typesResolved = true;
1121 return true;
1122}
1123
1124QQmlError QQmlTypeData::buildTypeResolutionCaches(
1125 QQmlRefPointer<QQmlTypeNameCache> *typeNameCache,
1126 QV4::CompiledData::ResolvedTypeReferenceMap *resolvedTypeCache) const
1127{
1128 assertTypeLoaderThread();
1129
1130 typeNameCache->adopt(new QQmlTypeNameCache(m_importCache));
1131
1132 for (const QString &ns: m_namespaces)
1133 (*typeNameCache)->add(ns);
1134
1135 // Add any Composite Singletons that were used to the import cache
1136 for (const QQmlTypeData::TypeReference &singleton: m_compositeSingletons)
1137 (*typeNameCache)->add(singleton.type.qmlTypeName(), singleton.type.sourceUrl(), singleton.prefix);
1138
1139 m_importCache->populateCache(typeNameCache->data());
1140
1141 for (auto resolvedType = m_resolvedTypes.constBegin(), end = m_resolvedTypes.constEnd(); resolvedType != end; ++resolvedType) {
1142 auto ref = std::make_unique<QV4::ResolvedTypeReference>();
1143 QQmlType qmlType = resolvedType->type;
1144 ref->setType(qmlType);
1145 ref->setIsSelfReference(resolvedType->selfReference);
1146 if (resolvedType->typeData) {
1147 if (resolvedType->needsCreation && qmlType.isCompositeSingleton()) {
1148 return qQmlCompileError(resolvedType->location, tr("Composite Singleton Type %1 is not creatable.").arg(qmlType.qmlTypeName()));
1149 }
1150 const auto compilationUnit = resolvedType->typeData->compilationUnit();
1151 if (qmlType.isInlineComponentType()) {
1152 // Inline component which is part of an already resolved type
1153 QString icName = qmlType.elementName();
1154 Q_ASSERT(!icName.isEmpty());
1155
1156 ref->setTypePropertyCache(compilationUnit->propertyCaches.at(
1157 compilationUnit->inlineComponentId(icName)));
1158 Q_ASSERT(ref->type().isInlineComponentType());
1159 } else {
1160 ref->setTypePropertyCache(compilationUnit->rootPropertyCache());
1161 }
1162 if (!resolvedType->selfReference)
1163 ref->setCompilationUnit(compilationUnit);
1164 } else if (qmlType.isInlineComponentType()) {
1165 // Inline component.
1166 // If it's from a different file we have a typeData and can't get here.
1167 // If it's defined in the same file we're currently compiling, we don't want to use it.
1168 // We're going to fill in the property caches later after all.
1169 Q_ASSERT(resolvedType->selfReference);
1170 Q_ASSERT(ref->isSelfReference());
1171 } else if (qmlType.isValid() && !resolvedType->selfReference) {
1172 Q_ASSERT(ref->type().isValid());
1173
1174 if (resolvedType->needsCreation && !qmlType.isCreatable()) {
1175 QString reason = qmlType.noCreationReason();
1176 if (reason.isEmpty())
1177 reason = tr("Element is not creatable.");
1178 return qQmlCompileError(resolvedType->location, reason);
1179 }
1180
1181 if (qmlType.containsRevisionedAttributes()) {
1182 // It can only have (revisioned) properties or methods if it has a metaobject
1183 Q_ASSERT(qmlType.metaObject());
1184 ref->setTypePropertyCache(
1185 QQmlMetaType::propertyCache(qmlType, resolvedType->version));
1186 }
1187 }
1188 ref->setVersion(resolvedType->version);
1189 ref->doDynamicTypeCheck();
1190 resolvedTypeCache->insert(resolvedType.key(), ref.release());
1191 }
1192 QQmlError noError;
1193 return noError;
1194}
1195
1196bool QQmlTypeData::resolveType(const QString &typeName, QTypeRevision &version,
1197 TypeReference &ref, int lineNumber, int columnNumber,
1198 bool reportErrors, QQmlType::RegistrationType registrationType,
1199 bool *typeRecursionDetected)
1200{
1201 assertTypeLoaderThread();
1202
1203 QQmlImportNamespace *typeNamespace = nullptr;
1204 QList<QQmlError> errors;
1205
1206 bool typeFound = m_importCache->resolveType(
1207 typeLoader(), typeName, &ref.type, &version, &typeNamespace, &errors, registrationType,
1208 typeRecursionDetected);
1209 if (!typeNamespace && !typeFound && !m_implicitImportLoaded) {
1210 // Lazy loading of implicit import
1211 if (loadImplicitImport()) {
1212 // Try again to find the type
1213 errors.clear();
1214 typeFound = m_importCache->resolveType(
1215 typeLoader(), typeName, &ref.type, &version, &typeNamespace, &errors,
1216 registrationType, typeRecursionDetected);
1217 } else {
1218 return false; //loadImplicitImport() hit an error, and called setError already
1219 }
1220 }
1221
1222 if ((!typeFound || typeNamespace) && reportErrors) {
1223 // Known to not be a type:
1224 // - known to be a namespace (Namespace {})
1225 // - type with unknown namespace (UnknownNamespace.SomeType {})
1226 QQmlError error;
1227 if (typeNamespace) {
1228 error.setDescription(QQmlTypeLoader::tr("Namespace %1 cannot be used as a type").arg(typeName));
1229 } else {
1230 if (errors.size()) {
1231 error = errors.takeFirst();
1232 } else {
1233 // this should not be possible!
1234 // Description should come from error provided by addImport() function.
1235 error.setDescription(QQmlTypeLoader::tr("Unreported error adding script import to import database"));
1236 }
1237 error.setUrl(m_importCache->baseUrl());
1238 error.setDescription(QQmlTypeLoader::tr("%1 %2").arg(typeName, error.description()));
1239 }
1240
1241 if (lineNumber != -1)
1242 error.setLine(lineNumber);
1243 if (columnNumber != -1)
1244 error.setColumn(columnNumber);
1245
1246 errors.prepend(error);
1247 setError(errors);
1248 return false;
1249 }
1250
1251 return true;
1252}
1253
1254void QQmlTypeData::scriptImported(
1255 const QQmlRefPointer<QQmlScriptBlob> &blob, const QV4::CompiledData::Location &location,
1256 const QString &nameSpace, const QString &qualifier)
1257{
1258 assertTypeLoaderThread();
1259
1260 ScriptReference ref;
1261 ref.script = blob;
1262 ref.location = location;
1263 ref.qualifier = qualifier.isEmpty() ? nameSpace : qualifier + QLatin1Char('.') + nameSpace;
1264
1265 m_scripts << ref;
1266}
1267
1268QT_END_NAMESPACE
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
DeepAliasResult