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
qqmlcomponentandaliasresolver_p.h
Go to the documentation of this file.
1// Copyright (C) 2023 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#ifndef QQMLCOMPONENTANDALIASRESOLVER_P_H
6#define QQMLCOMPONENTANDALIASRESOLVER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <QtQml/qqmlcomponent.h>
20#include <QtQml/qqmlerror.h>
21
22#include <QtCore/qglobal.h>
23#include <QtCore/qhash.h>
24
25#include <private/qqmltypeloader_p.h>
26#include <private/qqmlpropertyresolver_p.h>
27#include <private/qqmlpropertycachecreator_p.h>
28
30
32
33// This class primarily resolves component boundaries in a document.
34// With the information about boundaries, it then goes on to resolve aliases and generalized
35// grouped properties. Both rely on IDs as first part of their expressions and the IDs have
36// to be located in surrounding components. That's why we have to do this with the component
37// boundaries in mind.
38
43
44template<typename ObjectContainer>
46{
47public:
48 using CompiledObject = typename ObjectContainer::CompiledObject;
49 using CompiledBinding = typename ObjectContainer::CompiledBinding;
50
52 ObjectContainer *compiler,
53 QQmlPropertyCacheVector *propertyCaches);
54
55 [[nodiscard]] QQmlError resolve(int root = 0);
56
57private:
58 enum AliasResolutionResult {
59 NoAliasResolved,
60 SomeAliasesResolved,
61 AllAliasesResolved
62 };
63
64 enum AliasTargetResult {
65 TargetResolved,
66 TargetInvalid,
67 TargetInOtherObject,
68 TargetIsResolvedAlias,
69 TargetIsPendingAlias
70 };
71
72 static bool findSubProperty(
73 const QQmlPropertyCache::ConstPtr &cache, QStringView subProperty,
74 QQmlPropertyIndex *propIdx);
75 [[nodiscard]] bool resolveDeepAlias(
76 const CompiledObject *targetObject, QStringView property, QStringView subProperty,
77 QMetaType targetPropertyType, QQmlPropertyIndex *propIdx) const;
78 [[nodiscard]] QQmlError resolveSubProperty(
79 const QV4::CompiledData::Alias *alias, const CompiledObject *targetObject,
80 QStringView property, QStringView subProperty, QMetaType targetPropertyType,
81 QQmlPropertyIndex *propIdx) const;
82 [[nodiscard]] AliasTargetResult resolveAliasTarget(
83 const QV4::CompiledData::Alias *alias, int objectIndex, int targetObjectIndex,
84 QQmlPropertyIndex *propIdx, QQmlError *error) const;
85 [[nodiscard]] AliasResolutionResult resolveAliasesInObject(
86 int objectIndex, QQmlPropertyCacheAliasCreator<ObjectContainer> *aliasCacheCreator,
87 QQmlError *error);
88
89 // To be specialized for each container
90 void allocateNamedObjects(int componentRoot) const;
91 void setObjectId(int index) const;
92 [[nodiscard]] bool markAsComponent(int index) const;
93 [[nodiscard]] bool isComponentBoundary(int index) const;
94 [[nodiscard]] bool appendAliasToPropertyCache(
95 const QV4::CompiledData::Alias *alias, int objectIndex, int aliasIndex,
96 int encodedPropertyIndex, int resolvedTargetObjectId, int targetObjectIndex,
97 QQmlPropertyCacheAliasCreator<ObjectContainer> *aliasCacheCreator, QQmlError *error)
98 {
99 *error = aliasCacheCreator->appendAliasToPropertyCache(
100 *alias, objectIndex, aliasIndex, encodedPropertyIndex, resolvedTargetObjectId,
101 targetObjectIndex);
102 resolvedAliases.insert(alias);
103 return !error->isValid();
104 }
105
106
107 void resolveGeneralizedGroupedProperty(CompiledBinding *binding);
108 [[nodiscard]] bool wrapImplicitComponent(CompiledBinding *binding)
109 {
110 const int childObjectIndex = binding->value.objectIndex;
111
112 // Create an implicit component wrapper as an additional property cache entry.
113 // The CU binary is read-only so we cannot patch the binding.
114 QQmlPropertyCache::ConstPtr baseComponentCache
115 = QQmlMetaType::propertyCache(&QQmlComponent::staticMetaObject);
116 QQmlPropertyCache::Ptr wrapperCache = baseComponentCache->copyAndReserve(1, 0, 0, 0);
117 wrapperCache->appendComponentWrapper(
118 wrapperCache->propertyCount(), childObjectIndex);
119
120 const uint wrapperIndex = m_propertyCaches->count();
121 m_propertyCaches->append(std::move(wrapperCache));
122
123 m_componentRoots.append(wrapperIndex);
124 return true;
125 }
126
127 [[nodiscard]] QQmlError findAndRegisterImplicitComponents(
128 const CompiledObject *obj, const QQmlPropertyCache::ConstPtr &propertyCache);
129 [[nodiscard]] QQmlError collectIdsAndAliases(int objectIndex);
130 [[nodiscard]] QQmlError resolveAliases();
131 void resolveGeneralizedGroupedProperties();
132 [[nodiscard]] QQmlError resolveComponentsInInlineComponentRoot(int root);
133
134 QString stringAt(int idx) const { return m_compiler->stringAt(idx); }
135 QV4::ResolvedTypeReference *resolvedType(int id) const { return m_compiler->resolvedType(id); }
136
137 [[nodiscard]] QQmlError error(
138 const QV4::CompiledData::Location &location,
139 const QString &description)
140 {
141 QQmlError error;
142 error.setLine(qmlConvertSourceCoordinate<quint32, int>(location.line()));
143 error.setColumn(qmlConvertSourceCoordinate<quint32, int>(location.column()));
144 error.setDescription(description);
145 error.setUrl(m_compiler->url());
146 return error;
147 }
148
149 template<typename Token>
150 [[nodiscard]] QQmlError error(Token token, const QString &description)
151 {
152 return error(token->location, description);
153 }
154
155 static bool isUsableComponent(const QMetaObject *metaObject)
156 {
157 // The metaObject is a component we're interested in if it either is a QQmlComponent itself
158 // or if any of its parents is a QQmlAbstractDelegateComponent. We don't want to include
159 // qqmldelegatecomponent_p.h because it belongs to QtQmlModels.
160
161 if (metaObject == &QQmlComponent::staticMetaObject)
162 return true;
163
164 for (; metaObject; metaObject = metaObject->superClass()) {
165 if (qstrcmp(metaObject->className(), "QQmlAbstractDelegateComponent") == 0)
166 return true;
167 }
168
169 return false;
170 }
171
172 ObjectContainer *m_compiler = nullptr;
173
174 // Implicit component insertion may have added objects and thus we also need
175 // to extend the symmetric propertyCaches. Therefore, non-const propertyCaches.
176 QQmlPropertyCacheVector *m_propertyCaches = nullptr;
177
178 // indices of the objects that are actually Component {}
179 QList<quint32> m_componentRoots;
180 QList<int> m_objectsWithAliases;
181 QList<CompiledBinding *> m_generalizedGroupedProperties;
182 QSet<const QV4::CompiledData::Alias *> resolvedAliases;
183 typename ObjectContainer::IdToObjectMap m_idToObjectIndex;
184};
185
186template<typename ObjectContainer>
188 ObjectContainer *compiler,
189 QQmlPropertyCacheVector *propertyCaches)
190 : m_compiler(compiler)
192{
193}
194
195template<typename ObjectContainer>
196QQmlError QQmlComponentAndAliasResolver<ObjectContainer>::findAndRegisterImplicitComponents(
197 const CompiledObject *obj, const QQmlPropertyCache::ConstPtr &propertyCache)
198{
199 QQmlPropertyResolver propertyResolver(propertyCache);
200
201 const QQmlPropertyData *defaultProperty = obj->indexOfDefaultPropertyOrAlias != -1
202 ? propertyCache->parent()->defaultProperty()
203 : propertyCache->defaultProperty();
204
205 for (auto binding = obj->bindingsBegin(), end = obj->bindingsEnd(); binding != end; ++binding) {
206 if (binding->type() != QV4::CompiledData::Binding::Type_Object)
207 continue;
208
209 auto targetObject = m_compiler->objectAt(binding->value.objectIndex);
210 auto typeReference = resolvedType(targetObject->inheritedTypeNameIndex);
211 Q_ASSERT(typeReference);
212
213 const QMetaObject *firstMetaObject = nullptr;
214 if (const auto propCache = typeReference->typePropertyCache())
215 firstMetaObject = propCache->firstCppMetaObject();
216 if (isUsableComponent(firstMetaObject))
217 continue;
218
219 // if here, not a QQmlComponent, so needs wrapping
220 const QQmlPropertyData *pd = nullptr;
221 if (binding->propertyNameIndex != quint32(0)) {
222 bool notInRevision = false;
223 pd = propertyResolver.property(stringAt(binding->propertyNameIndex), &notInRevision);
224 } else {
225 pd = defaultProperty;
226 }
227 if (!pd || !pd->isQObject())
228 continue;
229
230 // If the version is given, use it and look up by QQmlType.
231 // Otherwise, make sure we look up by metaobject.
232 // TODO: Is this correct?
233 QQmlPropertyCache::ConstPtr pc = pd->typeVersion().hasMinorVersion()
234 ? QQmlMetaType::rawPropertyCacheForType(pd->propType(), pd->typeVersion())
235 : QQmlMetaType::rawPropertyCacheForType(pd->propType());
236 const QMetaObject *mo = pc ? pc->firstCppMetaObject() : nullptr;
237 while (mo) {
238 if (mo == &QQmlComponent::staticMetaObject)
239 break;
240 mo = mo->superClass();
241 }
242
243 if (!mo)
244 continue;
245
246 if (!wrapImplicitComponent(binding))
247 return error(binding, QQmlComponentAndAliasResolverBase::tr("Cannot wrap implicit component"));
248 }
249
250 return QQmlError();
251}
252
253template<typename ObjectContainer>
254QQmlError QQmlComponentAndAliasResolver<ObjectContainer>::resolveComponentsInInlineComponentRoot(
255 int root)
256{
257 // Find implicit components in the inline component itself. Also warn about inline
258 // components being explicit components.
259
260 const auto rootObj = m_compiler->objectAt(root);
261 Q_ASSERT(rootObj->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot));
262
263 if (const int typeName = rootObj->inheritedTypeNameIndex) {
264 const auto *tref = resolvedType(typeName);
265 Q_ASSERT(tref);
266 if (tref->type().metaObject() == &QQmlComponent::staticMetaObject) {
267 qCWarning(lcQmlTypeCompiler).nospace().noquote()
268 << m_compiler->url().toString() << ":" << rootObj->location.line() << ":"
269 << rootObj->location.column()
270 << ": Using a Component as the root of an inline component is deprecated: "
271 "inline components are "
272 "automatically wrapped into Components when needed.";
273 return QQmlError();
274 }
275 }
276
277 const QQmlPropertyCache::ConstPtr rootCache = m_propertyCaches->at(root);
278 Q_ASSERT(rootCache);
279
280 return findAndRegisterImplicitComponents(rootObj, rootCache);
281}
282
283// Resolve ignores everything relating to inline components, except for implicit components.
284template<typename ObjectContainer>
286{
287 // Detect real Component {} objects as well as implicitly defined components, such as
288 // someItemDelegate: Item {}
289 // In the implicit case Item is surrounded by a synthetic Component {} because the property
290 // on the left hand side is of QQmlComponent type.
291 const int objCountWithoutSynthesizedComponents = m_compiler->objectCount();
292
293 if (root != 0) {
294 const QQmlError error = resolveComponentsInInlineComponentRoot(root);
295 if (error.isValid())
296 return error;
297 }
298
299 // root+1, as ic root is handled at the end
300 const int startObjectIndex = root == 0 ? root : root+1;
301
302 for (int i = startObjectIndex; i < objCountWithoutSynthesizedComponents; ++i) {
303 auto obj = m_compiler->objectAt(i);
304 const bool isInlineComponentRoot
305 = obj->hasFlag(QV4::CompiledData::Object::IsInlineComponentRoot);
306 const bool isPartOfInlineComponent
307 = obj->hasFlag(QV4::CompiledData::Object::IsPartOfInlineComponent);
308 QQmlPropertyCache::ConstPtr cache = m_propertyCaches->at(i);
309
310 if (root == 0) {
311 // normal component root, skip over anything inline component related
312 if (isInlineComponentRoot || isPartOfInlineComponent)
313 continue;
314 } else if (!isPartOfInlineComponent || isInlineComponentRoot) {
315 // When handling an inline component, stop where the inline component ends
316 // Note: We do not support nested inline components. Therefore, isInlineComponentRoot
317 // tells us that the element after the current inline component is again an
318 // inline component
319 break;
320 }
321
322 if (obj->inheritedTypeNameIndex == 0 && !cache)
323 continue;
324
325 bool isExplicitComponent = false;
326 if (obj->inheritedTypeNameIndex) {
327 auto *tref = resolvedType(obj->inheritedTypeNameIndex);
328 Q_ASSERT(tref);
329 if (tref->type().metaObject() == &QQmlComponent::staticMetaObject)
330 isExplicitComponent = true;
331 }
332
333 if (!isExplicitComponent) {
334 if (cache) {
335 const QQmlError error = findAndRegisterImplicitComponents(obj, cache);
336 if (error.isValid())
337 return error;
338 }
339 continue;
340 }
341
342 if (!markAsComponent(i))
343 return error(obj, QQmlComponentAndAliasResolverBase::tr("Cannot mark object as component"));
344
345 // check if this object is the root
346 if (i == 0) {
347 if (isExplicitComponent)
348 qCWarning(lcQmlTypeCompiler).nospace().noquote()
349 << m_compiler->url().toString() << ":" << obj->location.line() << ":"
350 << obj->location.column()
351 << ": Using a Component as the root of a QML document is deprecated: types "
352 "defined in qml documents are "
353 "automatically wrapped into Components when needed.";
354 }
355
356 if (obj->functionCount() > 0)
357 return error(obj, QQmlComponentAndAliasResolverBase::tr("Component objects cannot declare new functions."));
358 if (obj->propertyCount() > 0 || obj->aliasCount() > 0)
359 return error(obj, QQmlComponentAndAliasResolverBase::tr("Component objects cannot declare new properties."));
360 if (obj->signalCount() > 0)
361 return error(obj, QQmlComponentAndAliasResolverBase::tr("Component objects cannot declare new signals."));
362
363 if (obj->bindingCount() == 0)
364 return error(obj, QQmlComponentAndAliasResolverBase::tr("Cannot create empty component specification"));
365
366 const auto rootBinding = obj->bindingsBegin();
367 const auto bindingsEnd = obj->bindingsEnd();
368
369 // Produce the more specific "no properties" error rather than the "invalid body" error
370 // where possible.
371 for (auto b = rootBinding; b != bindingsEnd; ++b) {
372 if (b->propertyNameIndex == 0)
373 continue;
374
375 return error(b, QQmlComponentAndAliasResolverBase::tr("Component elements may not contain properties other than id"));
376 }
377
378 if (auto b = rootBinding;
379 b->type() != QV4::CompiledData::Binding::Type_Object || ++b != bindingsEnd) {
380 return error(obj, QQmlComponentAndAliasResolverBase::tr("Invalid component body specification"));
381 }
382
383 // For the root object, we are going to collect ids/aliases and resolve them for as a
384 // separate last pass.
385 if (i != 0)
386 m_componentRoots.append(i);
387 }
388
389 for (int i = 0; i < m_componentRoots.size(); ++i) {
390 const int componentRoot = m_componentRoots.at(i);
391 const int childObjectIndex = componentRoot < m_compiler->objectCount()
392 ? m_compiler->objectAt(componentRoot)->bindingsBegin()->value.objectIndex
393 : m_compiler->resolvedIndex(componentRoot);
394
395 m_idToObjectIndex.clear();
396 m_objectsWithAliases.clear();
397 m_generalizedGroupedProperties.clear();
398
399 if (const QQmlError error = collectIdsAndAliases(childObjectIndex);
400 error.isValid()) {
401 return error;
402 }
403
404 allocateNamedObjects(componentRoot);
405
406 if (const QQmlError error = resolveAliases(); error.isValid())
407 return error;
408
409 resolveGeneralizedGroupedProperties();
410 }
411
412 // Collect ids and aliases for root
413 m_idToObjectIndex.clear();
414 m_objectsWithAliases.clear();
415 m_generalizedGroupedProperties.clear();
416
417 if (const QQmlError error = collectIdsAndAliases(root); error.isValid())
418 return error;
419
420 allocateNamedObjects(root);
421 if (const QQmlError error = resolveAliases(); error.isValid())
422 return error;
423
424 resolveGeneralizedGroupedProperties();
425 return QQmlError();
426}
427
428template<typename ObjectContainer>
429QQmlError QQmlComponentAndAliasResolver<ObjectContainer>::collectIdsAndAliases(int objectIndex)
430{
431 auto obj = m_compiler->objectAt(objectIndex);
432
433 if (obj->idNameIndex != 0) {
434 if (m_idToObjectIndex.contains(obj->idNameIndex))
435 return error(obj->locationOfIdProperty, QQmlComponentAndAliasResolverBase::tr("id is not unique"));
436 setObjectId(objectIndex);
437 m_idToObjectIndex.insert(obj->idNameIndex, objectIndex);
438 }
439
440 if (obj->aliasCount() > 0)
441 m_objectsWithAliases.append(objectIndex);
442
443 // Stop at Component boundary
444 if (isComponentBoundary(objectIndex) && objectIndex != /*root object*/0)
445 return QQmlError();
446
447 for (auto binding = obj->bindingsBegin(), end = obj->bindingsEnd();
448 binding != end; ++binding) {
449 switch (binding->type()) {
450 case QV4::CompiledData::Binding::Type_GroupedProperty: {
451 const auto *inner = m_compiler->objectAt(binding->value.objectIndex);
452 if (m_compiler->stringAt(inner->inheritedTypeNameIndex).isEmpty()) {
453 const auto cache = m_propertyCaches->at(objectIndex);
454 if (!cache || !cache->property(
455 m_compiler->stringAt(binding->propertyNameIndex), nullptr, nullptr)) {
456 m_generalizedGroupedProperties.append(binding);
457 }
458 }
459 }
460 Q_FALLTHROUGH();
461 case QV4::CompiledData::Binding::Type_Object:
462 case QV4::CompiledData::Binding::Type_AttachedProperty:
463 // Implicit component wrappers are component boundaries.
464 // Their children are collected separately via the componentRoots loop.
465 if (m_compiler->implicitComponentForObject(binding->value.objectIndex) != -1)
466 break;
467 if (const QQmlError error = collectIdsAndAliases(binding->value.objectIndex);
468 error.isValid()) {
469 return error;
470 }
471 break;
472 default:
473 break;
474 }
475 }
476
477 return QQmlError();
478}
479
480template<typename ObjectContainer>
481QQmlError QQmlComponentAndAliasResolver<ObjectContainer>::resolveAliases()
482{
483 if (m_objectsWithAliases.isEmpty())
484 return QQmlError();
485
486 QQmlPropertyCacheAliasCreator<ObjectContainer> aliasCacheCreator(m_propertyCaches, m_compiler);
487
488 bool atLeastOneAliasResolved;
489 do {
490 atLeastOneAliasResolved = false;
491 QList<int> pendingObjects;
492
493 for (int objectIndex: std::as_const(m_objectsWithAliases)) {
494
495 QQmlError error;
496 const auto result = resolveAliasesInObject(objectIndex, &aliasCacheCreator, &error);
497 if (error.isValid())
498 return error;
499
500 if (result == AllAliasesResolved) {
501 atLeastOneAliasResolved = true;
502 } else if (result == SomeAliasesResolved) {
503 atLeastOneAliasResolved = true;
504 pendingObjects.append(objectIndex);
505 } else {
506 pendingObjects.append(objectIndex);
507 }
508 }
509 qSwap(m_objectsWithAliases, pendingObjects);
510 } while (!m_objectsWithAliases.isEmpty() && atLeastOneAliasResolved);
511
512 if (!atLeastOneAliasResolved && !m_objectsWithAliases.isEmpty()) {
513 const CompiledObject *obj = m_compiler->objectAt(m_objectsWithAliases.first());
514 for (auto alias = obj->aliasesBegin(), end = obj->aliasesEnd(); alias != end; ++alias) {
515 if (!resolvedAliases.contains(alias)) {
516 return error(
517 alias->location(), QQmlComponentAndAliasResolverBase::tr("Cyclic alias"));
518 }
519 }
520 }
521
522 return QQmlError();
523}
524
525template<typename ObjectContainer>
526bool QQmlComponentAndAliasResolver<ObjectContainer>::findSubProperty(
527 const QQmlPropertyCache::ConstPtr &cache, QStringView subProperty,
528 QQmlPropertyIndex *propIdx)
529{
530 if (!cache)
531 return false;
532
533 const QQmlPropertyResolver resolver(cache);
534 const QQmlPropertyData *pd = resolver.property(subProperty.toString());
535 if (!pd)
536 return false;
537
538 *propIdx = QQmlPropertyIndex(propIdx->coreIndex(), pd->coreIndex());
539 return true;
540}
541
542// Attempts to resolve a "deep alias" - an alias whose sub-property path
543// goes through a QObject property, an inline component binding, or another
544// alias. For example: \c{alias foo: target.groupedProp.innerProp}
545//
546// First searches the target object's bindings and aliases for \a property,
547// then looks up \a subProperty on the bound/aliased object's property cache.
548// If no binding or alias matches, falls back to looking up \a subProperty
549// on the declared type's property cache.
550//
551// On success, updates \a propIdx with the resolved sub-property index and
552// returns \c true.
553template<typename ObjectContainer>
554bool QQmlComponentAndAliasResolver<ObjectContainer>::resolveDeepAlias(
555 const CompiledObject *targetObject, QStringView property, QStringView subProperty,
556 QMetaType targetPropertyType, QQmlPropertyIndex *propIdx) const
557{
558 for (auto it = targetObject->bindingsBegin(), end = targetObject->bindingsEnd();
559 it != end; ++it) {
560 if (stringAt(it->propertyNameIndex) != property)
561 continue;
562 if (findSubProperty(m_propertyCaches->at(it->value.objectIndex), subProperty, propIdx))
563 return true;
564 }
565
566 for (auto it = targetObject->aliasesBegin(), end = targetObject->aliasesEnd();
567 it != end; ++it) {
568 if (stringAt(it->nameIndex()) != property)
569 continue;
570 const int innerObjectIndex = m_idToObjectIndex.value(it->idIndex(), -1);
571 if (innerObjectIndex == -1)
572 continue;
573 if (findSubProperty(m_propertyCaches->at(innerObjectIndex), subProperty, propIdx))
574 return true;
575 }
576
577 return findSubProperty(
578 QQmlMetaType::propertyCacheForType(targetPropertyType), subProperty, propIdx);
579}
580
581template<typename ObjectContainer>
582QQmlError QQmlComponentAndAliasResolver<ObjectContainer>::resolveSubProperty(
583 const QV4::CompiledData::Alias *alias, const CompiledObject *targetObject,
584 QStringView property, QStringView subProperty, QMetaType targetPropertyType,
585 QQmlPropertyIndex *propIdx) const
586{
587 const auto invalidTarget = [&]() {
588 return qQmlCompileError(
589 alias->referenceLocation(),
590 QQmlComponentAndAliasResolverBase::tr("Invalid alias target location: %1")
591 .arg(subProperty.toString()));
592 };
593
594 const QMetaObject *valueTypeMetaObject
595 = QQmlMetaType::metaObjectForValueType(targetPropertyType);
596 if (!valueTypeMetaObject) {
597 // Could be a deep alias.
598 if (subProperty.at(0).isLower()
599 && resolveDeepAlias(targetObject, property, subProperty, targetPropertyType,
600 propIdx)) {
601 return QQmlError();
602 }
603 return invalidTarget();
604 }
605
606 const int valueTypeIndex = valueTypeMetaObject->indexOfProperty(
607 subProperty.toString().toUtf8().constData());
608 if (valueTypeIndex == -1)
609 return invalidTarget();
610
611 Q_ASSERT(valueTypeIndex <= 0x0000FFFF);
612 *propIdx = QQmlPropertyIndex(propIdx->coreIndex(), valueTypeIndex);
613 return QQmlError();
614}
615
616template<typename ObjectContainer>
617typename QQmlComponentAndAliasResolver<ObjectContainer>::AliasTargetResult
618QQmlComponentAndAliasResolver<ObjectContainer>::resolveAliasTarget(
619 const QV4::CompiledData::Alias *alias, int objectIndex, int targetObjectIndex,
620 QQmlPropertyIndex *propIdx, QQmlError *error) const
621{
622 const QString aliasPropertyValue = stringAt(alias->propertyNameIndex());
623 const QStringView aliasProperty(aliasPropertyValue);
624
625 QStringView property = aliasProperty;
626 QStringView subProperty;
627 if (const int propertySeparator = aliasProperty.indexOf(QLatin1Char('.'));
628 propertySeparator != -1) {
629 property = aliasProperty.left(propertySeparator);
630 subProperty = aliasProperty.mid(propertySeparator + 1);
631 }
632
633 // Alias points to object. No need to resolve properties.
634 if (property.isEmpty())
635 return TargetResolved;
636
637 const auto invalidTarget = [&]() {
638 *error = qQmlCompileError(
639 alias->referenceLocation(),
640 QQmlComponentAndAliasResolverBase::tr("Invalid alias target location: %1")
641 .arg(property.toString()));
642 return TargetInvalid;
643 };
644
645 const QQmlPropertyCache::ConstPtr targetCache = m_propertyCaches->at(targetObjectIndex);
646 if (!targetCache)
647 return invalidTarget();
648
649 const QQmlPropertyResolver resolver(targetCache);
650 const QQmlPropertyData *targetProperty = resolver.property(
651 property.toString(), nullptr, QQmlPropertyResolver::IgnoreRevision);
652
653 const CompiledObject *targetObject = m_compiler->objectAt(targetObjectIndex);
654
655 // The target may be an alias on the target object that has not been resolved yet.
656 // Resolve that one first and try this one again later.
657 if (!targetProperty) {
658 const auto targetAlias = std::find_if(
659 targetObject->aliasesBegin(), targetObject->aliasesEnd(),
660 [&](const auto &t) { return stringAt(t.nameIndex()) == property; });
661 if (targetAlias != targetObject->aliasesEnd()) {
662 // Don't continue yet; respect the order of objects.
663 if (targetObjectIndex != objectIndex)
664 return TargetInOtherObject;
665 return resolvedAliases.contains(targetAlias) ? TargetIsResolvedAlias
666 : TargetIsPendingAlias;
667 }
668 }
669
670 if (!targetProperty || targetProperty->coreIndex() > 0x0000FFFF)
671 return invalidTarget();
672
673 *propIdx = QQmlPropertyIndex(targetProperty->coreIndex());
674
675 if (!subProperty.isEmpty()) {
676 *error = resolveSubProperty(
677 alias, targetObject, property, subProperty, targetProperty->propType(), propIdx);
678 if (error->isValid())
679 return TargetInvalid;
680 }
681
682 return TargetResolved;
683}
684
685template<typename ObjectContainer>
686typename QQmlComponentAndAliasResolver<ObjectContainer>::AliasResolutionResult
687QQmlComponentAndAliasResolver<ObjectContainer>::resolveAliasesInObject(
688 int objectIndex, QQmlPropertyCacheAliasCreator<ObjectContainer> *aliasCacheCreator,
689 QQmlError *error)
690{
691 const CompiledObject *obj = m_compiler->objectAt(objectIndex);
692 if (!obj->aliasCount())
693 return AllAliasesResolved;
694
695 int aliasIndex = 0;
696 int numSkippedAliases = 0;
697 bool hasUnresolvedLocalAliases = false;
698
699 for (auto alias = obj->aliasesBegin(), end = obj->aliasesEnd(); alias != end;
700 ++alias, ++aliasIndex) {
701 if (resolvedAliases.contains(alias)) {
702 ++numSkippedAliases;
703 continue;
704 }
705
706 const int targetObjectIndex = m_idToObjectIndex.value(alias->idIndex(), -1);
707 if (targetObjectIndex == -1) {
708 *error = qQmlCompileError(
709 alias->referenceLocation(),
710 QQmlComponentAndAliasResolverBase::tr(
711 "Invalid alias reference. Unable to find id \"%1\"")
712 .arg(stringAt(alias->idIndex())));
713 break;
714 }
715
716 QQmlPropertyIndex propIdx;
717 const AliasTargetResult target
718 = resolveAliasTarget(alias, objectIndex, targetObjectIndex, &propIdx, error);
719 if (target == TargetInvalid)
720 break;
721
722 if (target == TargetInOtherObject)
723 return aliasIndex == numSkippedAliases ? NoAliasResolved : SomeAliasesResolved;
724
725 if (target == TargetIsPendingAlias) {
726 // Target is in the same object but not resolved yet. Try again later.
727 ++numSkippedAliases;
728 hasUnresolvedLocalAliases = true;
729 continue;
730 }
731
732 // If the target is an already resolved alias, we can set this alias right away.
733 const int encodedPropertyIndex
734 = target == TargetIsResolvedAlias ? -1 : propIdx.toEncoded();
735 if (!appendAliasToPropertyCache(
736 alias, objectIndex, aliasIndex, encodedPropertyIndex,
737 m_compiler->objectId(targetObjectIndex), targetObjectIndex, aliasCacheCreator,
738 error)) {
739 break;
740 }
741 }
742
743 if (numSkippedAliases == aliasIndex)
744 return NoAliasResolved;
745
746 if (aliasIndex == obj->aliasCount() && !hasUnresolvedLocalAliases)
747 return AllAliasesResolved;
748
749 return SomeAliasesResolved;
750}
751
752template<typename ObjectContainer>
753void QQmlComponentAndAliasResolver<ObjectContainer>::resolveGeneralizedGroupedProperties()
754{
755 for (CompiledBinding *binding : std::as_const(m_generalizedGroupedProperties))
756 resolveGeneralizedGroupedProperty(binding);
757}
758
759QT_END_NAMESPACE
760
761#endif // QQMLCOMPONENTANDALIASRESOLVER_P_H
QQmlComponentAndAliasResolver(ObjectContainer *compiler, QQmlPropertyCacheVector *propertyCaches)
typename ObjectContainer::CompiledObject CompiledObject
typename ObjectContainer::CompiledBinding CompiledBinding
QT_BEGIN_NAMESPACE Q_DECLARE_LOGGING_CATEGORY(lcQIORing)