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>
17#include <QtCore/qloggingcategory.h>
18#include <QtCore/qcryptographichash.h>
26QString QQmlTypeData::TypeReference::qualifiedName()
const
29 if (!prefix.isEmpty()) {
30 result = prefix + QLatin1Char(
'.');
32 result.append(type.qmlTypeName());
36QQmlTypeData::QQmlTypeData(
const QUrl &url, QQmlTypeLoader *manager)
37 : QQmlNotifyingBlob(url, QmlFile, manager),
38 m_typesResolved(
false), m_implicitImportLoaded(
false)
43QQmlTypeData::~QQmlTypeData()
46 m_compositeSingletons.clear();
47 m_resolvedTypes.clear();
50QV4::CompiledData::CompilationUnit *QQmlTypeData::compilationUnit()
const
52 return m_compiledData.data();
55QQmlType QQmlTypeData::qmlType(
const QString &inlineComponentName)
const
57 if (inlineComponentName.isEmpty())
59 return m_inlineComponentData[inlineComponentName].qmlType;
62bool QQmlTypeData::tryLoadFromDiskCache()
64 assertTypeLoaderThread();
66 if (!m_backupSourceCode.isCacheable())
69 if (!m_typeLoader->readCacheFile())
72 auto unit = QQml::makeRefPointer<QV4::CompiledData::CompilationUnit>();
75 if (!unit->loadFromDisk(url(), m_backupSourceCode.sourceTimeStamp(), &error)) {
76 qCDebug(DBG_DISK_CACHE) <<
"Error loading" << urlString() <<
"from disk cache:" << error;
81 if (unit->unitData()->flags & QV4::CompiledData::Unit::PendingTypeCompilation) {
86 return loadFromDiskCache(unit);
89bool QQmlTypeData::loadFromDiskCache(
const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit)
91 assertTypeLoaderThread();
93 m_compiledData = unit;
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]);
105 m_importCache->setBaseUrl(finalUrl(), finalUrlString());
110 if (!finalUrl().scheme().isEmpty()) {
111 QUrl qmldirUrl = finalUrl().resolved(QUrl(QLatin1String(
"qmldir")));
112 if (!QQmlImports::isLocal(qmldirUrl)) {
113 if (!loadImplicitImport())
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)) {
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);
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);
164void QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::allocateNamedObjects(
165 const QV4::CompiledData::Object *object)
const
171bool QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::markAsComponent(
int index)
const
173 return m_compiler->objectAt(index)->hasFlag(QV4::CompiledData::Object::IsComponent);
177void QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::setObjectId(
int index)
const
185void QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::resolveGeneralizedGroupProperty(
186 const CompiledObject &component, CompiledBinding *binding)
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));
201typename QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::AliasResolutionResult
202QQmlComponentAndAliasResolver<QV4::CompiledData::CompilationUnit>::resolveAliasesInObject(
203 const CompiledObject &component,
int objectIndex,
204 QQmlPropertyCacheAliasCreator<QV4::CompiledData::CompilationUnit> *aliasCacheCreator,
207 const CompiledObject *obj = m_compiler->objectAt(objectIndex);
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);
216 const auto handleDeepAlias = [&](
217 const QV4::CompiledData::Alias *alias,
const QQmlPropertyCache::ConstPtr &propertyCache,
218 int coreIndex, QStringView subProperty,
int resolvedTargetObjectId)
220 const QQmlPropertyResolver resolver = QQmlPropertyResolver(propertyCache);
221 const QQmlPropertyData *actualProperty = resolver.property(subProperty.toString());
223 return DeepAliasResult::NoProperty;
226 alias, QQmlPropertyIndex(coreIndex, actualProperty->coreIndex()).toEncoded(),
227 resolvedTargetObjectId)) {
228 return DeepAliasResult::Success;
231 return DeepAliasResult::CannotAppend;
234 for (
auto alias = obj->aliasesBegin(), end = obj->aliasesEnd(); alias != end; ++alias) {
235 if (resolvedAliases.contains(alias)) {
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;
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())));
255 const QV4::CompiledData::Object *targetObject = m_compiler->objectAt(targetObjectIndex);
256 const int resolvedTargetObjectId = targetObject->objectId();
258 QStringView property;
259 QStringView subProperty;
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);
267 property = QStringView(aliasPropertyValue);
270 if (property.isEmpty()) {
271 if (doAppendAlias(alias, -1, resolvedTargetObjectId))
273 return SomeAliasesResolved;
276 Q_ASSERT(!property.isEmpty());
277 QQmlPropertyCache::ConstPtr targetCache = m_propertyCaches->at(targetObjectIndex);
278 Q_ASSERT(targetCache);
280 const QQmlPropertyResolver resolver(targetCache);
281 const QQmlPropertyData *targetProperty = resolver.property(property.toString());
283 return SomeAliasesResolved;
285 const int coreIndex = targetProperty->coreIndex();
286 if (subProperty.isEmpty()) {
288 alias, QQmlPropertyIndex(coreIndex).toEncoded(), resolvedTargetObjectId)) {
291 return SomeAliasesResolved;
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;
302 alias, QQmlPropertyIndex(coreIndex, valueTypeIndex).toEncoded(),
303 resolvedTargetObjectId)) {
307 return SomeAliasesResolved;
310 Q_ASSERT(subProperty.at(0).isLower());
312 bool foundDeepAliasInBindings =
false;
313 for (
auto it = targetObject->bindingsBegin(); it != targetObject->bindingsEnd(); ++it) {
314 if (m_compiler->stringAt(it->propertyNameIndex) != property)
317 const QQmlPropertyCache::ConstPtr bindingCache
318 = m_propertyCaches->at(it->value.objectIndex);
322 switch (handleDeepAlias(
323 alias, bindingCache, coreIndex, subProperty, resolvedTargetObjectId)) {
324 case DeepAliasResult::NoProperty:
326 case DeepAliasResult::CannotAppend:
327 return SomeAliasesResolved;
328 case DeepAliasResult::Success:
329 foundDeepAliasInBindings =
true;
336 if (foundDeepAliasInBindings)
339 const QQmlPropertyCache::ConstPtr typeCache
340 = QQmlMetaType::propertyCacheForType(targetProperty->propType());
342 return SomeAliasesResolved;
344 switch (handleDeepAlias(alias, typeCache, coreIndex, subProperty, resolvedTargetObjectId)) {
345 case DeepAliasResult::NoProperty:
346 case DeepAliasResult::CannotAppend:
347 return SomeAliasesResolved;
348 case DeepAliasResult::Success:
353 return AllAliasesResolved;
356QQmlError QQmlTypeData::createTypeAndPropertyCaches(
357 const QQmlRefPointer<QQmlTypeNameCache> &typeNameCache,
358 const QV4::CompiledData::ResolvedTypeReferenceMap &resolvedTypeCache)
360 assertTypeLoaderThread();
362 Q_ASSERT(m_compiledData);
363 m_compiledData->typeNameCache = typeNameCache;
364 m_compiledData->resolvedTypes = resolvedTypeCache;
365 m_compiledData->inlineComponentData = m_inlineComponentData;
366 m_compiledData->qmlType = m_qmlType;
368 QQmlPendingGroupPropertyBindings pendingGroupPropertyBindings;
371 QQmlPropertyCacheCreator<QV4::CompiledData::CompilationUnit> propertyCacheCreator(
372 &m_compiledData->propertyCaches, &pendingGroupPropertyBindings, m_typeLoader,
373 m_compiledData.data(), m_importCache.data(), typeClassName());
375 QQmlError error = propertyCacheCreator.verifyNoICCycle();
379 QQmlPropertyCacheCreatorBase::IncrementalResult result;
381 result = propertyCacheCreator.buildMetaObjectsIncrementally();
382 if (result.error.isValid()) {
385 QQmlComponentAndAliasResolver resolver(
386 m_compiledData.data(), &m_compiledData->propertyCaches);
387 if (
const QQmlError error = resolver.resolve(result.processedRoot);
391 pendingGroupPropertyBindings.resolveMissingPropertyCaches(
392 &m_compiledData->propertyCaches);
393 pendingGroupPropertyBindings.clear();
396 }
while (result.canResume);
399 pendingGroupPropertyBindings.resolveMissingPropertyCaches(&m_compiledData->propertyCaches);
405using InlineComponentData = QV4::CompiledData::InlineComponentData;
407template<
typename ObjectContainer>
409 const ObjectContainer &container, QHash<QString, InlineComponentData> *icData,
411 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit) {
412 Q_ASSERT(icData->empty());
413 for (
int i = 0; i != container->objectCount(); ++i) {
414 auto root = container->objectAt(i);
415 for (
auto it = root->inlineComponentsBegin(); it != root->inlineComponentsEnd(); ++it) {
418 InlineComponentData icDatum(
419 QQmlMetaType::findOrCreateFactualInlineComponentType(
420 baseUrl, container->stringAt(it->nameIndex), compilationUnit),
421 int(it->objectIndex),
int(it->nameIndex));
423 icData->insert(container->stringAt(it->nameIndex), icDatum);
429bool QQmlTypeData::checkScripts()
432 for (
int ii = 0; ii < m_scripts.size(); ++ii) {
433 const ScriptReference &script = m_scripts.at(ii);
434 Q_ASSERT(script.script->isCompleteOrError());
435 if (script.script->isError()) {
438 QQmlTypeLoader::tr(
"Script %1 unavailable").arg(script.script->urlString()));
445void QQmlTypeData::createError(
const TypeReference &type,
const QString &message)
447 createError(type, message, type.typeData ? type.typeData->errors() : QList<QQmlError>());
450void QQmlTypeData::createError(
const ScriptReference &script,
const QString &message)
452 createError(script, message, script.script ? script.script->errors() : QList<QQmlError>());
455bool QQmlTypeData::checkDependencies()
458 for (
auto it = std::as_const(m_resolvedTypes).begin(), end = std::as_const(m_resolvedTypes).end();
460 const TypeReference &type = *it;
461 Q_ASSERT(!type.typeData
462 || type.typeData->isCompleteOrError()
463 || type.type.isInlineComponent());
465 if (type.typeData && type.typeData->isError()) {
466 const QString &typeName = stringAt(it.key());
467 createError(type, QQmlTypeLoader::tr(
"Type %1 unavailable").arg(typeName));
471 if (!type.selfReference && type.type.isInlineComponent()) {
472 const QString icName = type.type.elementName();
473 Q_ASSERT(!icName.isEmpty());
476 if (type.typeData && type.typeData->compilationUnit()->inlineComponentId(icName) >= 0)
479 const QString typeName = stringAt(it.key());
480 const qsizetype lastDot = typeName.lastIndexOf(u'.');
483 QQmlTypeLoader::tr(
"Type %1 has no inline component type called %2")
484 .arg(QStringView{typeName}.left(lastDot), icName));
492bool QQmlTypeData::checkCompositeSingletons()
495 for (
int ii = 0; ii < m_compositeSingletons.size(); ++ii) {
496 const TypeReference &type = m_compositeSingletons.at(ii);
497 Q_ASSERT(!type.typeData || type.typeData->isCompleteOrError());
498 if (type.typeData && type.typeData->isError()) {
499 QString typeName = type.type.qmlTypeName();
500 createError(type, QQmlTypeLoader::tr(
"Type %1 unavailable").arg(typeName));
508void QQmlTypeData::createQQmlType()
510 if (QQmlPropertyCacheCreatorBase::canCreateClassNameTypeByUrl(finalUrl())) {
511 const bool isSingleton = m_document
512 ? m_document.data()->isSingleton()
513 : (m_compiledData->unitData()->flags & QV4::CompiledData::Unit::IsSingleton);
514 m_qmlType = QQmlMetaType::findCompositeType(
515 url(), m_compiledData, isSingleton
516 ? QQmlMetaType::Singleton
517 : QQmlMetaType::NonSingleton);
518 m_typeClassName = QByteArray(m_qmlType.typeId().name()).chopped(1);
522bool QQmlTypeData::rebuildFromSource()
526 m_typeReferences.clear();
528 m_namespaces.clear();
529 m_compositeSingletons.clear();
531 m_resolvedTypes.clear();
532 m_typesResolved =
false;
534 m_qmlType = QQmlType();
535 m_typeClassName.clear();
537 m_inlineComponentData.clear();
538 m_compiledData.reset();
540 m_implicitImportLoaded =
false;
542 m_importCache.adopt(
new QQmlImports);
543 m_unresolvedImports.clear();
545 if (!loadFromSource())
548 continueLoadFromIR();
556 if (!checkDependencies())
559 if (!checkCompositeSingletons())
564 setupICs(m_document, &m_inlineComponentData, finalUrl(), m_compiledData);
568void QQmlTypeData::done()
570 assertTypeLoaderThread();
572 auto cleanup = qScopeGuard([
this]{
573 m_backupSourceCode = SourceCodeData();
575 m_typeReferences.clear();
577 const auto encounteredErrors = errors();
578 for (
const QQmlError &e : encounteredErrors)
579 qCDebug(DBG_DISK_CACHE) << e.toString();
580 m_compiledData.reset();
583 m_resolvedTypes.clear();
584 m_compositeSingletons.clear();
595 if (!checkDependencies())
598 if (!checkCompositeSingletons())
604 setupICs(m_document, &m_inlineComponentData, finalUrl(), m_compiledData);
606 setupICs(m_compiledData, &m_inlineComponentData, finalUrl(), m_compiledData);
608 QV4::CompiledData::ResolvedTypeReferenceMap resolvedTypeCache;
609 QQmlRefPointer<QQmlTypeNameCache> typeNameCache;
612 const bool verifyCaches = !m_compiledData
613 || (m_compiledData->resolvedTypes.isEmpty() && !m_compiledData->typeNameCache);
616 QQmlError error = buildTypeResolutionCaches(&typeNameCache, &resolvedTypeCache);
617 if (error.isValid()) {
619 qDeleteAll(resolvedTypeCache);
624 const auto dependencyHasher = [&resolvedTypeCache,
this]() {
625 return typeLoader()->hashDependencies(&resolvedTypeCache, m_compositeSingletons);
629 if (m_document.isNull() && verifyCaches) {
630 const QQmlError error = createTypeAndPropertyCaches(typeNameCache, resolvedTypeCache);
631 if (error.isValid() || !m_compiledData->verifyChecksum(dependencyHasher)) {
633 if (error.isValid()) {
634 qCDebug(DBG_DISK_CACHE)
635 <<
"Failed to create property caches for"
636 << m_compiledData->fileName()
637 <<
"because" << error.description();
639 qCDebug(DBG_DISK_CACHE)
640 <<
"Checksum mismatch for cached version of"
641 << m_compiledData->fileName();
644 resolvedTypeCache.clear();
645 typeNameCache.reset();
647 if (!rebuildFromSource())
650 const QQmlError error = buildTypeResolutionCaches(&typeNameCache, &resolvedTypeCache);
651 if (error.isValid()) {
653 qDeleteAll(resolvedTypeCache);
659 if (!m_document.isNull()) {
660 Q_ASSERT(verifyCaches);
662 compile(typeNameCache, &resolvedTypeCache, dependencyHasher);
668 m_compiledData->inlineComponentData = m_inlineComponentData;
671 QQmlPropertyValidator validator(typeLoader(), m_importCache.data(), m_compiledData);
672 QList<QQmlError> errors = validator.validate();
673 if (!errors.isEmpty()) {
679 m_compiledData->finalizeCompositeType(qmlType());
683 QQmlType type = QQmlMetaType::qmlType(finalUrl());
684 if (m_compiledData && m_compiledData->unitData()->flags & QV4::CompiledData::Unit::IsSingleton) {
685 if (!type.isValid()) {
687 error.setDescription(QQmlTypeLoader::tr(
"No matching type found, pragma Singleton files cannot be used by QQmlComponent."));
690 }
else if (!type.isCompositeSingleton()) {
692 error.setDescription(QQmlTypeLoader::tr(
"pragma Singleton used with a non composite singleton type %1").arg(type.qmlTypeName()));
699 if (type.isValid() && type.isCompositeSingleton()) {
700 QString typeName = type.qmlTypeName();
701 setError(QQmlTypeLoader::tr(
"qmldir defines type as singleton, but no pragma Singleton found in type %1.").arg(typeName));
709 m_compiledData->dependentScripts.reserve(m_scripts.size());
710 for (
int scriptIndex = 0; scriptIndex < m_scripts.size(); ++scriptIndex) {
711 const QQmlTypeData::ScriptReference &script = m_scripts.at(scriptIndex);
713 QStringView qualifier(script.qualifier);
714 QString enclosingNamespace;
716 const int lastDotIndex = qualifier.lastIndexOf(QLatin1Char(
'.'));
717 if (lastDotIndex != -1) {
718 enclosingNamespace = qualifier.left(lastDotIndex).toString();
719 qualifier = qualifier.mid(lastDotIndex+1);
722 m_compiledData->typeNameCache->add(
723 qualifier.toString(), scriptIndex, enclosingNamespace);
724 QQmlRefPointer<QQmlScriptData> scriptData = script.script->scriptData();
725 m_compiledData->dependentScripts << scriptData;
730bool QQmlTypeData::loadImplicitImport()
732 assertTypeLoaderThread();
734 m_implicitImportLoaded =
true;
736 m_importCache->setBaseUrl(finalUrl(), finalUrlString());
741 QList<QQmlError> implicitImportErrors;
743 m_importCache->addImplicitImport(typeLoader(), &localQmldir, &implicitImportErrors);
749 if (!localQmldir.isEmpty()) {
750 const QQmlTypeLoaderQmldirContent qmldir = typeLoader()->qmldirContent(localQmldir);
751 const QList<QQmlDirParser::Import> moduleImports
752 = QQmlMetaType::moduleImports(qmldir.typeNamespace(), QTypeRevision())
754 loadDependentImports(moduleImports, QString(), QTypeRevision(),
755 QQmlImportInstance::Implicit + 1, QQmlImports::ImportNoFlag,
756 &implicitImportErrors);
759 if (!implicitImportErrors.isEmpty()) {
760 setError(implicitImportErrors);
767void QQmlTypeData::dataReceived(
const SourceCodeData &data)
769 assertTypeLoaderThread();
771 m_backupSourceCode = data;
773 if (tryLoadFromDiskCache())
779 if (!m_backupSourceCode.exists() || m_backupSourceCode.isEmpty()) {
780 if (m_cachedUnitStatus == QQmlMetaType::CachedUnitLookupError::VersionMismatch)
781 setError(QQmlTypeLoader::tr(
"File was compiled ahead of time with an incompatible version of Qt and the original file cannot be found. Please recompile"));
782 else if (!m_backupSourceCode.exists())
783 setError(QQmlTypeLoader::tr(
"No such file or directory"));
785 setError(QQmlTypeLoader::tr(
"File is empty"));
789 if (!loadFromSource())
792 continueLoadFromIR();
795void QQmlTypeData::initializeFromCachedUnit(
const QQmlPrivate::CachedQmlUnit *unit)
797 assertTypeLoaderThread();
799 if (unit->qmlData->qmlUnit()->nObjects == 0) {
800 setError(QQmlTypeLoader::tr(
"Cached QML Unit has no objects"));
805 new QmlIR::Document(urlString(), finalUrlString(), m_typeLoader->isDebugging()));
806 QQmlIRLoader loader(unit->qmlData, m_document.data());
808 m_document->javaScriptCompilationUnit
809 = QQmlRefPointer<QV4::CompiledData::CompilationUnit>(
810 new QV4::CompiledData::CompilationUnit(unit->qmlData, unit->aotCompiledFunctions, unit->validateLookupSignatures),
811 QQmlRefPointer<QV4::CompiledData::CompilationUnit>::Adopt);
812 continueLoadFromIR();
815bool QQmlTypeData::loadFromSource()
817 assertTypeLoaderThread();
820 new QmlIR::Document(urlString(), finalUrlString(), m_typeLoader->isDebugging()));
821 m_document->jsModule.sourceTimeStamp = m_backupSourceCode.sourceTimeStamp();
822 QmlIR::IRBuilder compiler;
825 const QString source = m_backupSourceCode.readAll(&sourceError);
826 if (!sourceError.isEmpty()) {
827 setError(sourceError);
831 if (!compiler.generateFromQml(source, finalUrlString(), m_document.data())) {
832 QList<QQmlError> errors;
833 errors.reserve(compiler.errors.size());
834 for (
const QQmlJS::DiagnosticMessage &msg : std::as_const(compiler.errors)) {
837 e.setLine(qmlConvertSourceCoordinate<quint32,
int>(msg.loc.startLine));
838 e.setColumn(qmlConvertSourceCoordinate<quint32,
int>(msg.loc.startColumn));
839 e.setDescription(msg.message);
848void QQmlTypeData::restoreIR(
const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &unit)
850 assertTypeLoaderThread();
853 new QmlIR::Document(urlString(), finalUrlString(), m_typeLoader->isDebugging()));
854 QQmlIRLoader loader(unit->unitData(), m_document.data());
856 m_document->javaScriptCompilationUnit = unit;
857 continueLoadFromIR();
860void QQmlTypeData::continueLoadFromIR()
862 assertTypeLoaderThread();
864 for (
auto const& object: std::as_const(m_document->objects)) {
865 for (
auto it = object->inlineComponentsBegin(); it != object->inlineComponentsEnd(); ++it) {
866 QString
const nameString = m_document->stringAt(it->nameIndex);
867 auto importUrl = finalUrl();
868 importUrl.setFragment(nameString);
869 auto import =
new QQmlImportInstance();
870 m_importCache->addInlineComponentImport(import, nameString, importUrl);
874 m_typeReferences.collectFromObjects(m_document->objects.constBegin(), m_document->objects.constEnd());
875 m_importCache->setBaseUrl(finalUrl(), finalUrlString());
880 if (!finalUrl().scheme().isEmpty()) {
881 QUrl qmldirUrl = finalUrl().resolved(QUrl(QLatin1String(
"qmldir")));
882 if (!QQmlImports::isLocal(qmldirUrl)) {
883 if (!loadImplicitImport())
886 auto implicitImport = std::make_shared<PendingImport>();
887 implicitImport->uri = QLatin1String(
".");
888 implicitImport->version = QTypeRevision();
889 QList<QQmlError> errors;
891 if (!fetchQmldir(qmldirUrl, implicitImport, 1, &errors)) {
898 QList<QQmlError> errors;
900 for (
const QV4::CompiledData::Import *import : std::as_const(m_document->imports)) {
901 if (!addImport(import, {}, &errors)) {
902 Q_ASSERT(errors.size());
907 QQmlError error = errors.first();
908 error.setUrl(m_importCache->baseUrl());
909 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(import->location.line()));
910 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(import->location.column()));
917void QQmlTypeData::allDependenciesDone()
919 assertTypeLoaderThread();
921 QQmlTypeLoader::Blob::allDependenciesDone();
923 if (!m_typesResolved)
927QString QQmlTypeData::stringAt(
int index)
const
930 return m_compiledData->stringAt(index);
931 return m_document->jsGenerator.stringTable.stringForIndex(index);
934void QQmlTypeData::compile(
const QQmlRefPointer<QQmlTypeNameCache> &typeNameCache,
935 QV4::CompiledData::ResolvedTypeReferenceMap *resolvedTypeCache,
936 const QV4::CompiledData::DependentTypesHasher &dependencyHasher)
938 assertTypeLoaderThread();
940 Q_ASSERT(m_compiledData.isNull());
942 const bool typeRecompilation = m_document
943 && m_document->javaScriptCompilationUnit
944 && m_document->javaScriptCompilationUnit->unitData()
945 && (m_document->javaScriptCompilationUnit->unitData()->flags
946 & QV4::CompiledData::Unit::PendingTypeCompilation);
948 QQmlTypeCompiler compiler(
949 typeLoader(),
this, m_document.data(), resolvedTypeCache, dependencyHasher);
950 auto compilationUnit = compiler.compile();
951 if (!compilationUnit) {
952 qDeleteAll(*resolvedTypeCache);
953 resolvedTypeCache->clear();
954 setError(compiler.compilationErrors());
958 const bool trySaveToDisk = m_typeLoader->writeCacheFile() && !typeRecompilation;
961 if (compilationUnit->saveToDisk(url(), &errorString)) {
963 if (!compilationUnit->loadFromDisk(url(), m_backupSourceCode.sourceTimeStamp(), &error)) {
967 qCDebug(DBG_DISK_CACHE) <<
"Error saving cached version of"
968 << compilationUnit->fileName() <<
"to disk:" << errorString;
972 m_compiledData = std::move(compilationUnit);
973 m_compiledData->typeNameCache = typeNameCache;
974 m_compiledData->resolvedTypes = *resolvedTypeCache;
975 m_compiledData->propertyCaches = std::move(*compiler.propertyCaches());
976 Q_ASSERT(m_compiledData->propertyCaches.count()
977 >=
static_cast<
int>(m_compiledData->objectCount()));
980bool QQmlTypeData::resolveTypes()
982 assertTypeLoaderThread();
984 Q_ASSERT(!m_typesResolved);
987 QList<QQmlError> errors;
988 auto it = m_unresolvedImports.constBegin(), end = m_unresolvedImports.constEnd();
989 for ( ; it != end; ++it) {
990 const PendingImportPtr &import = *it;
991 if (import->priority != 0)
996 if (registerPendingTypes(import))
1001 error.setDescription(QQmlTypeLoader::tr(
"module \"%1\" is not installed").arg(import->uri));
1002 error.setUrl(m_importCache->baseUrl());
1003 error.setLine(qmlConvertSourceCoordinate<quint32,
int>(
1004 import->location.line()));
1005 error.setColumn(qmlConvertSourceCoordinate<quint32,
int>(
1006 import->location.column()));
1007 errors.prepend(error);
1010 if (errors.size()) {
1016 if (!m_implicitImportLoaded && !loadImplicitImport())
1020 const auto resolvedScripts = m_importCache->resolvedScripts();
1021 for (
const QQmlImports::ScriptReference &script : resolvedScripts) {
1022 QQmlRefPointer<QQmlScriptBlob> blob
1023 = typeLoader()->getScript(script.location, script.fileName);
1024 addDependency(blob.data());
1026 ScriptReference ref;
1028 if (!script.qualifier.isEmpty())
1030 ref.qualifier = script.qualifier + QLatin1Char(
'.') + script.nameSpace;
1032 m_namespaces.insert(script.qualifier);
1034 ref.qualifier = script.nameSpace;
1042 const auto resolvedCompositeSingletons = m_importCache->resolvedCompositeSingletons();
1043 for (
const QQmlImports::CompositeSingletonReference &csRef : resolvedCompositeSingletons) {
1046 if (!csRef.prefix.isEmpty()) {
1047 typeName = csRef.prefix + QLatin1Char(
'.') + csRef.typeName;
1049 m_namespaces.insert(csRef.prefix);
1051 typeName = csRef.typeName;
1054 QTypeRevision version = csRef.version;
1055 if (!resolveType(typeName, version, ref, -1, -1,
true, QQmlType::CompositeSingletonType))
1058 if (ref.type.isCompositeSingleton()) {
1059 ref.typeData = typeLoader()->getType(ref.type.sourceUrl());
1060 if (ref.typeData->isWaiting() || m_waitingOnMe.contains(ref.typeData.data())) {
1061 qCDebug(lcCycle) <<
"Possible cyclic dependency detected between"
1062 << ref.typeData->urlString() <<
"and" << urlString();
1065 addDependency(ref.typeData.data());
1066 ref.prefix = csRef.prefix;
1068 m_compositeSingletons << ref;
1072 for (
auto unresolvedRef = m_typeReferences.constBegin(), end = m_typeReferences.constEnd();
1073 unresolvedRef != end; ++unresolvedRef) {
1077 const bool reportErrors = unresolvedRef->errorWhenNotFound;
1079 QTypeRevision version;
1081 const QString name = stringAt(unresolvedRef.key());
1083 bool *selfReferenceDetection = unresolvedRef->needsCreation ?
nullptr : &ref.selfReference;
1085 if (!resolveType(name, version, ref, unresolvedRef->location.line(),
1086 unresolvedRef->location.column(), reportErrors,
1087 QQmlType::AnyRegistrationType, selfReferenceDetection) && reportErrors)
1090 if (ref.selfReference) {
1092 }
else if (ref.type.isInlineComponent()) {
1093 QUrl containingTypeUrl = ref.type.sourceUrl();
1094 Q_ASSERT(!containingTypeUrl.isEmpty());
1095 if (QQmlMetaType::equalBaseUrls(finalUrl(), containingTypeUrl)) {
1096 ref.selfReference =
true;
1098 containingTypeUrl.setFragment(QString());
1099 auto typeData = typeLoader()->getType(containingTypeUrl);
1100 Q_ASSERT(typeData.data() !=
this);
1101 ref.typeData = typeData;
1102 addDependency(typeData.data());
1104 }
else if (ref.type.isComposite()) {
1105 ref.typeData = typeLoader()->getType(ref.type.sourceUrl());
1106 addDependency(ref.typeData.data());
1109 ref.version = version;
1110 ref.location = unresolvedRef->location;
1111 ref.needsCreation = unresolvedRef->needsCreation;
1112 m_resolvedTypes.insert(unresolvedRef.key(), ref);
1115 m_typesResolved =
true;
1119QQmlError QQmlTypeData::buildTypeResolutionCaches(
1120 QQmlRefPointer<QQmlTypeNameCache> *typeNameCache,
1121 QV4::CompiledData::ResolvedTypeReferenceMap *resolvedTypeCache)
const
1123 assertTypeLoaderThread();
1125 typeNameCache->adopt(
new QQmlTypeNameCache(m_importCache));
1127 for (
const QString &ns: m_namespaces)
1128 (*typeNameCache)->add(ns);
1131 for (
const QQmlTypeData::TypeReference &singleton: m_compositeSingletons)
1132 (*typeNameCache)->add(singleton.type.qmlTypeName(), singleton.type.sourceUrl(), singleton.prefix);
1134 m_importCache->populateCache(typeNameCache->data());
1136 for (
auto resolvedType = m_resolvedTypes.constBegin(), end = m_resolvedTypes.constEnd(); resolvedType != end; ++resolvedType) {
1137 auto ref = std::make_unique<QV4::ResolvedTypeReference>();
1138 QQmlType qmlType = resolvedType->type;
1139 ref->setType(qmlType);
1140 ref->setIsSelfReference(resolvedType->selfReference);
1141 if (resolvedType->typeData) {
1142 if (resolvedType->needsCreation && qmlType.isCompositeSingleton()) {
1143 return qQmlCompileError(resolvedType->location, tr(
"Composite Singleton Type %1 is not creatable.").arg(qmlType.qmlTypeName()));
1145 const auto compilationUnit = resolvedType->typeData->compilationUnit();
1146 if (qmlType.isInlineComponent()) {
1148 QString icName = qmlType.elementName();
1149 Q_ASSERT(!icName.isEmpty());
1151 ref->setTypePropertyCache(compilationUnit->propertyCaches.at(
1152 compilationUnit->inlineComponentId(icName)));
1153 Q_ASSERT(ref->type().isInlineComponent());
1155 ref->setTypePropertyCache(compilationUnit->rootPropertyCache());
1157 if (!resolvedType->selfReference)
1158 ref->setCompilationUnit(compilationUnit);
1159 }
else if (qmlType.isInlineComponent()) {
1164 Q_ASSERT(resolvedType->selfReference);
1165 Q_ASSERT(ref->isSelfReference());
1166 }
else if (qmlType.isValid() && !resolvedType->selfReference) {
1167 Q_ASSERT(ref->type().isValid());
1169 if (resolvedType->needsCreation && !qmlType.isCreatable()) {
1170 QString reason = qmlType.noCreationReason();
1171 if (reason.isEmpty())
1172 reason = tr(
"Element is not creatable.");
1173 return qQmlCompileError(resolvedType->location, reason);
1176 if (qmlType.containsRevisionedAttributes()) {
1178 Q_ASSERT(qmlType.metaObject());
1179 ref->setTypePropertyCache(
1180 QQmlMetaType::propertyCache(qmlType, resolvedType->version));
1183 ref->setVersion(resolvedType->version);
1184 ref->doDynamicTypeCheck();
1185 resolvedTypeCache->insert(resolvedType.key(), ref.release());
1191bool QQmlTypeData::resolveType(
const QString &typeName, QTypeRevision &version,
1192 TypeReference &ref,
int lineNumber,
int columnNumber,
1193 bool reportErrors, QQmlType::RegistrationType registrationType,
1194 bool *typeRecursionDetected)
1196 assertTypeLoaderThread();
1198 QQmlImportNamespace *typeNamespace =
nullptr;
1199 QList<QQmlError> errors;
1201 bool typeFound = m_importCache->resolveType(
1202 typeLoader(), typeName, &ref.type, &version, &typeNamespace, &errors, registrationType,
1203 typeRecursionDetected);
1204 if (!typeNamespace && !typeFound && !m_implicitImportLoaded) {
1206 if (loadImplicitImport()) {
1209 typeFound = m_importCache->resolveType(
1210 typeLoader(), typeName, &ref.type, &version, &typeNamespace, &errors,
1211 registrationType, typeRecursionDetected);
1217 if ((!typeFound || typeNamespace) && reportErrors) {
1222 if (typeNamespace) {
1223 error.setDescription(QQmlTypeLoader::tr(
"Namespace %1 cannot be used as a type").arg(typeName));
1225 if (errors.size()) {
1226 error = errors.takeFirst();
1230 error.setDescription(QQmlTypeLoader::tr(
"Unreported error adding script import to import database"));
1232 error.setUrl(m_importCache->baseUrl());
1233 error.setDescription(QQmlTypeLoader::tr(
"%1 %2").arg(typeName, error.description()));
1236 if (lineNumber != -1)
1237 error.setLine(lineNumber);
1238 if (columnNumber != -1)
1239 error.setColumn(columnNumber);
1241 errors.prepend(error);
1249void QQmlTypeData::scriptImported(
1250 const QQmlRefPointer<QQmlScriptBlob> &blob,
const QV4::CompiledData::Location &location,
1251 const QString &nameSpace,
const QString &qualifier)
1253 assertTypeLoaderThread();
1255 ScriptReference ref;
1257 ref.location = location;
1258 ref.qualifier = qualifier.isEmpty() ? nameSpace : qualifier + QLatin1Char(
'.') + nameSpace;
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")