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
qqmljstyperesolver.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
10#include "qqmljsutils_p.h"
11#include <private/qv4codegen_p.h>
12#include <private/qv4value_p.h>
13
14#include <private/qduplicatetracker_p.h>
15
16#include <QtCore/qloggingcategory.h>
17#include <QtCore/qtyperevision.h>
18
20
21using namespace Qt::StringLiterals;
22
23Q_STATIC_LOGGING_CATEGORY(lcTypeResolver, "qt.qml.compiler.typeresolver", QtInfoMsg);
24
25static inline void assertExtension(const QQmlJSScope::ConstPtr &type, QLatin1String extension)
26{
27 Q_ASSERT(type);
28 Q_ASSERT(type->extensionType().scope->internalName() == extension);
29 Q_ASSERT(type->extensionIsJavaScript());
30}
31
32QQmlJSTypeResolver::QQmlJSTypeResolver(QQmlJSImporter *importer)
33 : m_pool(std::make_unique<QQmlJSRegisterContentPool>())
34 , m_imports(importer->builtinInternalNames())
35 , m_importer(importer)
36{
37 const QQmlJSImporter::ImportedTypes &builtinTypes = m_imports;
38
39 m_voidType = builtinTypes.type(u"void"_s).scope;
40 assertExtension(m_voidType, "undefined"_L1);
41
42 m_nullType = builtinTypes.type(u"std::nullptr_t"_s).scope;
43 Q_ASSERT(m_nullType);
44
45 m_realType = builtinTypes.type(u"double"_s).scope;
46 assertExtension(m_realType, "Number"_L1);
47
48 m_floatType = builtinTypes.type(u"float"_s).scope;
49 assertExtension(m_floatType, "Number"_L1);
50
51 m_int8Type = builtinTypes.type(u"qint8"_s).scope;
52 assertExtension(m_int8Type, "Number"_L1);
53
54 m_uint8Type = builtinTypes.type(u"quint8"_s).scope;
55 assertExtension(m_uint8Type, "Number"_L1);
56
57 m_int16Type = builtinTypes.type(u"short"_s).scope;
58 assertExtension(m_int16Type, "Number"_L1);
59
60 m_uint16Type = builtinTypes.type(u"ushort"_s).scope;
61 assertExtension(m_uint16Type, "Number"_L1);
62
63 m_int32Type = builtinTypes.type(u"int"_s).scope;
64 assertExtension(m_int32Type, "Number"_L1);
65
66 m_uint32Type = builtinTypes.type(u"uint"_s).scope;
67 assertExtension(m_uint32Type, "Number"_L1);
68
69 m_int64Type = builtinTypes.type(u"qlonglong"_s).scope;
70 Q_ASSERT(m_int64Type);
71
72 m_uint64Type = builtinTypes.type(u"qulonglong"_s).scope;
73 Q_ASSERT(m_uint64Type);
74
75 m_sizeType = builtinTypes.type(u"qsizetype"_s).scope;
76 assertExtension(m_sizeType, "Number"_L1);
77
78 // qsizetype is either a 32bit or a 64bit signed integer. We don't want to special-case it.
79 Q_ASSERT(m_sizeType == m_int32Type || m_sizeType == m_int64Type);
80
81 m_boolType = builtinTypes.type(u"bool"_s).scope;
82 assertExtension(m_boolType, "Boolean"_L1);
83
84 m_stringType = builtinTypes.type(u"QString"_s).scope;
85 assertExtension(m_stringType, "String"_L1);
86
87 m_stringListType = builtinTypes.type(u"QStringList"_s).scope;
88 assertExtension(m_stringListType, "Array"_L1);
89
90 m_byteArrayType = builtinTypes.type(u"QByteArray"_s).scope;
91 assertExtension(m_byteArrayType, "ArrayBuffer"_L1);
92
93 m_urlType = builtinTypes.type(u"QUrl"_s).scope;
94 assertExtension(m_urlType, "URL"_L1);
95
96 m_dateTimeType = builtinTypes.type(u"QDateTime"_s).scope;
97 assertExtension(m_dateTimeType, "Date"_L1);
98
99 m_dateType = builtinTypes.type(u"QDate"_s).scope;
100 Q_ASSERT(m_dateType);
101
102 m_timeType = builtinTypes.type(u"QTime"_s).scope;
103 Q_ASSERT(m_timeType);
104
105 m_regexpType = builtinTypes.type(u"QRegularExpression"_s).scope;
106 Q_ASSERT(m_regexpType);
107
108 m_variantListType = builtinTypes.type(u"QVariantList"_s).scope;
109 assertExtension(m_variantListType, "Array"_L1);
110
111 m_variantMapType = builtinTypes.type(u"QVariantMap"_s).scope;
112 Q_ASSERT(m_variantMapType);
113 m_varType = builtinTypes.type(u"QVariant"_s).scope;
114 Q_ASSERT(m_varType);
115
116 m_qmlPropertyMapType = builtinTypes.type(u"QQmlPropertyMap"_s).scope;
117 Q_ASSERT(m_qmlPropertyMapType);
118
119 m_jsValueType = builtinTypes.type(u"QJSValue"_s).scope;
120 Q_ASSERT(m_jsValueType);
121
122 m_qObjectType = builtinTypes.type(u"QObject"_s).scope;
123 assertExtension(m_qObjectType, "Object"_L1);
124
125 m_qObjectListType = builtinTypes.type(u"QObjectList"_s).scope;
126 assertExtension(m_qObjectListType, "Array"_L1);
127
128 m_qQmlScriptStringType = builtinTypes.type(u"QQmlScriptString"_s).scope;
129 Q_ASSERT(m_qQmlScriptStringType);
130
131 m_functionType = builtinTypes.type(u"function"_s).scope;
132 Q_ASSERT(m_functionType);
133
134 m_numberPrototype = builtinTypes.type(u"NumberPrototype"_s).scope;
135 Q_ASSERT(m_numberPrototype);
136
137 m_arrayPrototype = builtinTypes.type(u"ArrayPrototype"_s).scope;
138 Q_ASSERT(m_arrayPrototype);
139
140 m_listPropertyType = m_qObjectType->listType();
141 Q_ASSERT(m_listPropertyType->internalName() == u"QQmlListProperty<QObject>"_s);
142 Q_ASSERT(m_listPropertyType->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence);
143 Q_ASSERT(m_listPropertyType->elementTypeName() == u"QObject"_s);
144 assertExtension(m_listPropertyType, "Array"_L1);
145
146 QQmlJSScope::Ptr emptyType = QQmlJSScope::create();
147 emptyType->setAccessSemantics(QQmlJSScope::AccessSemantics::None);
148 m_emptyType = emptyType;
149
150 QQmlJSScope::Ptr jsPrimitiveType = QQmlJSScope::create();
151 jsPrimitiveType->setInternalName(u"QJSPrimitiveValue"_s);
152 jsPrimitiveType->setFilePath(u"qjsprimitivevalue.h"_s);
153 jsPrimitiveType->setAccessSemantics(QQmlJSScope::AccessSemantics::Value);
154 m_jsPrimitiveType = jsPrimitiveType;
155
156 QQmlJSScope::Ptr metaObjectType = QQmlJSScope::create();
157 metaObjectType->setInternalName(u"const QMetaObject"_s);
158 metaObjectType->setFilePath(u"qmetaobject.h"_s);
159 metaObjectType->setAccessSemantics(QQmlJSScope::AccessSemantics::Reference);
160 m_metaObjectType = metaObjectType;
161
162 m_jsGlobalObject = importer->jsGlobalObject();
163
164 QQmlJSScope::Ptr forInIteratorPtr = QQmlJSScope::create();
165 forInIteratorPtr->setAccessSemantics(QQmlJSScope::AccessSemantics::Value);
166 forInIteratorPtr->setFilePath(u"qjslist.h"_s);
167 forInIteratorPtr->setInternalName(u"QJSListForInIterator::Ptr"_s);
168 m_forInIteratorPtr = forInIteratorPtr;
169
170 QQmlJSScope::Ptr forOfIteratorPtr = QQmlJSScope::create();
171 forOfIteratorPtr->setAccessSemantics(QQmlJSScope::AccessSemantics::Value);
172 forOfIteratorPtr->setFilePath(u"qjslist.h"_s);
173 forOfIteratorPtr->setInternalName(u"QJSListForOfIterator::Ptr"_s);
174 m_forOfIteratorPtr = forOfIteratorPtr;
175
176 // We use this as scope type quite often, and it should always be the same scope type.
177 m_jsGlobalObjectContent = m_pool->createType(
178 m_jsGlobalObject, QQmlJSRegisterContent::InvalidLookupIndex,
179 QQmlJSRegisterContent::GlobalObject);
180}
181
182/*!
183 \internal
184
185 Initializes the type resolver. As part of that initialization, makes \a
186 visitor traverse the program when given.
187*/
188void QQmlJSTypeResolver::init(QQmlJSImportVisitor *visitor, QQmlJS::AST::Node *program)
189{
190 m_logger = visitor->logger();
191
192 m_objectsById.clear();
193 m_objectsByLocation.clear();
194 m_imports.clear();
195 m_signalHandlers.clear();
196 m_mergeCache.clear();
197
198 if (program)
199 program->accept(visitor);
200
201 m_objectsById = visitor->addressableScopes();
202 m_objectsByLocation = visitor->scopesBylocation();
203 m_signalHandlers = visitor->signalHandlers();
204 m_imports = visitor->imports();
205 m_seenModuleQualifiers = visitor->seenModuleQualifiers();
206}
207
208QQmlJSScope::ConstPtr QQmlJSTypeResolver::mathObject() const
209{
210 return jsGlobalObject()->property(u"Math"_s).type();
211}
212
213QQmlJSScope::ConstPtr QQmlJSTypeResolver::consoleObject() const
214{
215 return jsGlobalObject()->property(u"console"_s).type();
216}
217
218QQmlJSScope::ConstPtr
219QQmlJSTypeResolver::scopeForLocation(const QV4::CompiledData::Location &location) const
220{
221 // #if required for standalone DOM compilation against Qt 6.2
222 qCDebug(lcTypeResolver()).nospace()
223 << "looking for object at " << location.line() << ':' << location.column();
224 return m_objectsByLocation[location];
225}
226
227QQmlJSScope::ConstPtr QQmlJSTypeResolver::typeFromAST(QQmlJS::AST::Type *type) const
228{
229 const QString typeId = QmlIR::IRBuilder::asString(type->typeId);
230 if (!type->typeArgument)
231 return m_imports.type(typeId).scope;
232 if (typeId == u"list"_s) {
233 if (const QQmlJSScope::ConstPtr typeArgument = typeForName(type->typeArgument->toString()))
234 return typeArgument->listType();
235 }
236 return QQmlJSScope::ConstPtr();
237}
238
239QQmlJSScope::ConstPtr QQmlJSTypeResolver::typeForConst(QV4::ReturnedValue rv) const
240{
241 QV4::Value value = QV4::Value::fromReturnedValue(rv);
242 if (value.isUndefined())
243 return voidType();
244
245 if (value.isInt32())
246 return int32Type();
247
248 if (value.isBoolean())
249 return boolType();
250
251 if (value.isDouble())
252 return realType();
253
254 if (value.isNull())
255 return nullType();
256
257 if (value.isEmpty())
258 return emptyType();
259
260 return {};
261}
262
263QQmlJSRegisterContent
264QQmlJSTypeResolver::typeForBinaryOperation(QSOperator::Op oper, QQmlJSRegisterContent left,
265 QQmlJSRegisterContent right) const
266{
267 Q_ASSERT(left.isValid());
268 Q_ASSERT(right.isValid());
269
270 switch (oper) {
271 case QSOperator::Op::Equal:
272 case QSOperator::Op::NotEqual:
273 case QSOperator::Op::StrictEqual:
274 case QSOperator::Op::StrictNotEqual:
275 case QSOperator::Op::Lt:
276 case QSOperator::Op::Gt:
277 case QSOperator::Op::Ge:
278 case QSOperator::Op::In:
279 case QSOperator::Op::Le:
280 return operationType(boolType());
281 case QSOperator::Op::BitAnd:
282 case QSOperator::Op::BitOr:
283 case QSOperator::Op::BitXor:
284 case QSOperator::Op::LShift:
285 case QSOperator::Op::RShift:
286 return operationType(int32Type());
287 case QSOperator::Op::URShift:
288 return operationType(uint32Type());
289 case QSOperator::Op::Add: {
290 const auto leftContents = left.containedType();
291 const auto rightContents = right.containedType();
292 if (leftContents == stringType() || rightContents == stringType())
293 return operationType(stringType());
294
295 const QQmlJSScope::ConstPtr result = merge(leftContents, rightContents);
296 if (result == boolType())
297 return operationType(int32Type());
298 if (isNumeric(result))
299 return operationType(realType());
300
301 return operationType(jsPrimitiveType());
302 }
303 case QSOperator::Op::Sub:
304 case QSOperator::Op::Mul:
305 case QSOperator::Op::Exp: {
306 const QQmlJSScope::ConstPtr result = merge(left.containedType(), right.containedType());
307 return operationType(result == boolType() ? int32Type() : realType());
308 }
309 case QSOperator::Op::Div:
310 case QSOperator::Op::Mod:
311 return operationType(realType());
312 case QSOperator::Op::As:
313 return operationType(right.containedType());
314 default:
315 break;
316 }
317
318 return operationType(merge(left.containedType(), right.containedType()));
319}
320
321QQmlJSRegisterContent QQmlJSTypeResolver::typeForArithmeticUnaryOperation(
322 UnaryOperator op, QQmlJSRegisterContent operand) const
323{
324 switch (op) {
325 case UnaryOperator::Not:
326 return operationType(boolType());
327 case UnaryOperator::Complement:
328 return operationType(int32Type());
329 case UnaryOperator::Plus:
330 if (isIntegral(operand))
331 return operationType(operand.containedType());
332 Q_FALLTHROUGH();
333 default:
334 if (operand.containedType() == boolType())
335 return operationType(int32Type());
336 break;
337 }
338
339 return operationType(realType());
340}
341
342bool QQmlJSTypeResolver::isPrimitive(QQmlJSRegisterContent type) const
343{
344 return isPrimitive(type.containedType());
345}
346
347bool QQmlJSTypeResolver::isNumeric(QQmlJSRegisterContent type) const
348{
349 return isNumeric(type.containedType());
350}
351
352bool QQmlJSTypeResolver::isIntegral(QQmlJSRegisterContent type) const
353{
354 return isIntegral(type.containedType());
355}
356
357bool QQmlJSTypeResolver::isIntegral(const QQmlJSScope::ConstPtr &type) const
358{
359 return isSignedInteger(type) || isUnsignedInteger(type);
360}
361
362bool QQmlJSTypeResolver::isPrimitive(const QQmlJSScope::ConstPtr &type) const
363{
364 return (isNumeric(type) && type != m_int64Type && type != m_uint64Type)
365 || type == m_boolType || type == m_voidType || type == m_nullType
366 || type == m_stringType || type == m_jsPrimitiveType;
367}
368
369bool QQmlJSTypeResolver::isNumeric(const QQmlJSScope::ConstPtr &type) const
370{
371 if (!type) // should this be a precondition instead?
372 return false;
373
374 if (type->scopeType() == QQmlJSScope::ScopeType::EnumScope)
375 return true;
376
377 if (type == m_realType)
378 return true;
379 if (type == m_floatType)
380 return true;
381 if (type == m_int8Type)
382 return true;
383 if (type == m_uint8Type)
384 return true;
385 if (type == m_int16Type)
386 return true;
387 if (type == m_uint16Type)
388 return true;
389 if (type == m_int32Type)
390 return true;
391 if (type == m_uint32Type)
392 return true;
393 if (type == m_int64Type)
394 return true;
395 if (type == m_uint64Type)
396 return true;
397 // sizetype is covered by one of the above cases (int32 or int64)
398 // booleans are not numeric
399
400 // the number prototype is numeric
401 if (type == m_numberPrototype)
402 return true;
403
404 // and types directly inheriting from it, notably number, are also
405 // numeric
406 if (type->baseType() == m_numberPrototype)
407 return true;
408
409 // it isn't possible (for users) to derive from m_numberPrototpye,
410 // so we know the list above is exhaustive / we don't need to go up
411 // further in the inheritance chain
412
413 return false;
414}
415
416bool QQmlJSTypeResolver::isSignedInteger(const QQmlJSScope::ConstPtr &type) const
417{
418 return type == m_int8Type || type == m_int16Type
419 || type == m_int32Type || type == m_int64Type;
420}
421
422bool QQmlJSTypeResolver::isUnsignedInteger(const QQmlJSScope::ConstPtr &type) const
423{
424 return type == m_uint8Type || type == m_uint16Type
425 || type == m_uint32Type || type == m_uint64Type;
426}
427
428bool QQmlJSTypeResolver::isNativeArrayIndex(const QQmlJSScope::ConstPtr &type) const
429{
430 return type == m_uint8Type || type == m_int8Type || type == m_uint16Type || type == m_int16Type
431 || type == m_uint32Type || type == m_int32Type;
432}
433
434QQmlJSScope::ConstPtr QQmlJSTypeResolver::containedTypeForName(const QString &name) const
435{
436 QQmlJSScope::ConstPtr type = typeForName(name);
437
438 if (!type || type->isSingleton() || type->isScript())
439 return type;
440
441 switch (type->accessSemantics()) {
442 case QQmlJSScope::AccessSemantics::Reference:
443 if (const auto attached = type->attachedType())
444 return genericType(attached) ? attached : QQmlJSScope::ConstPtr();
445 return metaObjectType();
446 case QQmlJSScope::AccessSemantics::None:
447 return metaObjectType();
448 case QQmlJSScope::AccessSemantics::Sequence:
449 case QQmlJSScope::AccessSemantics::Value:
450 return canAddressValueTypes() ? metaObjectType() : QQmlJSScope::ConstPtr();
451 }
452
453 Q_UNREACHABLE_RETURN(QQmlJSScope::ConstPtr());
454}
455
456QQmlJSRegisterContent QQmlJSTypeResolver::registerContentForName(
457 const QString &name, QQmlJSRegisterContent scopeType) const
458{
459 QQmlJSScope::ConstPtr type = typeForName(name);
460 if (!type)
461 return QQmlJSRegisterContent();
462
463 if (type->isSingleton()) {
464 return m_pool->createType(
465 type, QQmlJSRegisterContent::InvalidLookupIndex,
466 QQmlJSRegisterContent::Singleton, scopeType);
467 }
468
469 if (type->isScript()) {
470 return m_pool->createType(
471 type, QQmlJSRegisterContent::InvalidLookupIndex,
472 QQmlJSRegisterContent::Script, scopeType);
473 }
474
475 const QQmlJSRegisterContent namedType = m_pool->createType(
476 type, QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::TypeByName,
477 scopeType);
478
479 if (const auto attached = type->attachedType()) {
480 if (!genericType(attached)) {
481 m_logger->log(u"Cannot resolve generic base of attached %1"_s.arg(
482 attached->internalName()),
483 qmlCompiler, attached->sourceLocation());
484 return {};
485 } else if (type->accessSemantics() != QQmlJSScope::AccessSemantics::Reference) {
486 m_logger->log(u"Cannot retrieve attached object for non-reference type %1"_s.arg(
487 type->internalName()),
488 qmlCompiler, type->sourceLocation());
489 return {};
490 } else {
491 // We don't know yet whether we need the attached or the plain object. In direct
492 // mode, we will figure this out using the scope type and access any enums of the
493 // plain type directly. In indirect mode, we can use enum lookups.
494 return m_pool->createType(
495 attached, QQmlJSRegisterContent::InvalidLookupIndex,
496 QQmlJSRegisterContent::Attachment, namedType);
497 }
498 }
499
500 switch (type->accessSemantics()) {
501 case QQmlJSScope::AccessSemantics::None:
502 case QQmlJSScope::AccessSemantics::Reference:
503 // A plain reference to a non-singleton, non-attached type.
504 // We may still need the plain type reference for enum lookups,
505 // Store it as QMetaObject.
506 // This only works with namespaces and object types.
507 return m_pool->createType(
508 metaObjectType(), QQmlJSRegisterContent::InvalidLookupIndex,
509 QQmlJSRegisterContent::MetaType, namedType);
510 case QQmlJSScope::AccessSemantics::Sequence:
511 case QQmlJSScope::AccessSemantics::Value:
512 if (scopeType.isImportNamespace() || canAddressValueTypes()) {
513 return m_pool->createType(
514 metaObjectType(), QQmlJSRegisterContent::InvalidLookupIndex,
515 QQmlJSRegisterContent::MetaType, namedType);
516 }
517 // Else this is not actually a type reference. You cannot get the metaobject
518 // of a value type in QML and sequences don't even have metaobjects.
519 break;
520 }
521
522 return QQmlJSRegisterContent();
523}
524
525QQmlJSRegisterContent QQmlJSTypeResolver::original(QQmlJSRegisterContent type) const
526{
527 QQmlJSRegisterContent result = type.original();
528 return result.isNull() ? type : result;
529}
530
531QQmlJSScope::ConstPtr QQmlJSTypeResolver::originalContainedType(
532 QQmlJSRegisterContent container) const
533{
534 return original(container).containedType();
535}
536
537QQmlJSRegisterContent QQmlJSTypeResolver::shadowed(QQmlJSRegisterContent type) const
538{
539 QQmlJSRegisterContent result = type.shadowed();
540 return result.isNull() ? type : result;
541}
542
543bool QQmlJSTypeResolver::adjustTrackedType(
544 QQmlJSRegisterContent tracked, const QQmlJSScope::ConstPtr &conversion) const
545{
546 if (m_cloneMode == QQmlJSTypeResolver::DoNotCloneTypes)
547 return true;
548
549 QQmlJSScope::ConstPtr contained = tracked.containedType();
550 QQmlJSScope::ConstPtr result = conversion;
551
552 // Do not adjust to the JavaScript extension of the original type. Rather keep the original
553 // type in that case.
554 QQmlJSUtils::searchBaseAndExtensionTypes(
555 contained, [&](const QQmlJSScope::ConstPtr &scope, QQmlJSScope::ExtensionKind kind) {
556 if (kind != QQmlJSScope::ExtensionJavaScript || scope != result)
557 return false;
558 result = contained;
559 return true;
560 });
561
562
563 // If we cannot convert to the new type without the help of e.g. lookupResultMetaType(),
564 // we better not change the type.
565 if (!canPrimitivelyConvertFromTo(contained, result)
566 && !canPopulate(result, contained, nullptr)
567 && !selectConstructor(result, contained, nullptr).isValid()) {
568 return false;
569 }
570
571 m_pool->adjustType(tracked, result);
572 return true;
573}
574
575bool QQmlJSTypeResolver::adjustTrackedType(
576 QQmlJSRegisterContent tracked, QQmlJSRegisterContent conversion) const
577{
578 return adjustTrackedType(tracked, conversion.containedType());
579}
580
581bool QQmlJSTypeResolver::adjustTrackedType(
582 QQmlJSRegisterContent tracked, const QList<QQmlJSRegisterContent> &conversions) const
583{
584 if (m_cloneMode == QQmlJSTypeResolver::DoNotCloneTypes)
585 return true;
586
587 QQmlJSScope::ConstPtr result;
588 for (QQmlJSRegisterContent type : conversions)
589 result = merge(type.containedType(), result);
590
591 return adjustTrackedType(tracked, result);
592}
593
594void QQmlJSTypeResolver::adjustOriginalType(
595 QQmlJSRegisterContent tracked, const QQmlJSScope::ConstPtr &conversion) const
596{
597 if (m_cloneMode == QQmlJSTypeResolver::DoNotCloneTypes)
598 return;
599
600 m_pool->generalizeType(tracked, conversion);
601}
602
603void QQmlJSTypeResolver::generalizeType(QQmlJSRegisterContent type) const
604{
605 if (m_cloneMode == QQmlJSTypeResolver::DoNotCloneTypes)
606 return;
607
608 for (QQmlJSRegisterContent orig = type; !orig.isNull(); orig = orig.original()) {
609 if (!orig.shadowed().isValid())
610 m_pool->generalizeType(orig, genericType(orig.containedType()));
611 }
612}
613
614bool QQmlJSTypeResolver::canConvertFromTo(const QQmlJSScope::ConstPtr &from,
615 const QQmlJSScope::ConstPtr &to) const
616{
617 if (canPrimitivelyConvertFromTo(from, to)
618 || canPopulate(to, from, nullptr)
619 || selectConstructor(to, from, nullptr).isValid()) {
620 return true;
621 }
622
623 // ### need a generic solution for custom cpp types:
624 // if (from->m_hasBoolOverload && equals(to, boolType))
625 // return true;
626
627 // All of these types have QString conversions that require a certain format
628 // TODO: Actually verify these strings or deprecate them.
629 // Some of those type are builtins or should be builtins. We should add code for them
630 // in QQmlJSCodeGenerator::conversion().
631 if (from == m_stringType && !to.isNull()) {
632 const QString toTypeName = to->internalName();
633 if (toTypeName == u"QPoint"_s || toTypeName == u"QPointF"_s
634 || toTypeName == u"QSize"_s || toTypeName == u"QSizeF"_s
635 || toTypeName == u"QRect"_s || toTypeName == u"QRectF"_s) {
636 return true;
637 }
638 }
639
640 return false;
641}
642
643bool QQmlJSTypeResolver::canConvertFromTo(QQmlJSRegisterContent from,
644 QQmlJSRegisterContent to) const
645{
646 return canConvertFromTo(from.containedType(), to.containedType());
647}
648
649static QQmlJSRegisterContent::ContentVariant mergeVariants(QQmlJSRegisterContent::ContentVariant a,
650 QQmlJSRegisterContent::ContentVariant b)
651{
652 return (a == b) ? a : QQmlJSRegisterContent::Unknown;
653}
654
655QQmlJSRegisterContent QQmlJSTypeResolver::merge(
656 QQmlJSRegisterContent a, QQmlJSRegisterContent b) const
657{
658 Q_ASSERT(a != b);
659
660 // Both mergeScopes() calls below recurse into this function. See m_mergeCache.
661 const RegisterPair key(a, b);
662 if (const auto it = m_mergeCache.constFind(key); it != m_mergeCache.constEnd())
663 return m_pool->clone(*it);
664
665 // We cannot easily provide an operator< for QQmlJSRegisterContent.
666 // Therefore we use qHash and operator== here to deduplicate. That's somewhat inefficient.
667 QSet<QQmlJSRegisterContent> origins;
668
669 QQmlJSRegisterContent aResultScope;
670 if (a.isConversion()) {
671 const auto &aOrigins = a.conversionOrigins();
672 for (const auto &aOrigin : aOrigins)
673 origins.insert(aOrigin);
674 aResultScope = a.conversionResultScope();
675 } else {
676 origins.insert(a);
677 aResultScope = a.scope();
678 }
679
680 QQmlJSRegisterContent bResultScope;
681 if (b.isConversion()) {
682 const auto &bOrigins = b.conversionOrigins();
683 for (const auto &bOrigin : bOrigins)
684 origins.insert(bOrigin);
685 bResultScope = b.conversionResultScope();
686 } else {
687 origins.insert(b);
688 bResultScope = b.scope();
689 }
690
691 const auto mergeScopes = [&](QQmlJSRegisterContent a, QQmlJSRegisterContent b) {
692 // If they are the same, we don't want to re-track them.
693 // We want the repetition on type mismatches to converge.
694 return (a == b) ? a : merge(a, b);
695 };
696
697 const QQmlJSRegisterContent merged = m_pool->createConversion(
698 origins.values(),
699 merge(a.containedType(), b.containedType()),
700 mergeScopes(aResultScope, bResultScope),
701 mergeVariants(a.variant(), b.variant()),
702 mergeScopes(a.scope(), b.scope()));
703
704 m_mergeCache.insert(key, m_pool->clone(merged));
705 return merged;
706}
707
708QQmlJSScope::ConstPtr QQmlJSTypeResolver::merge(const QQmlJSScope::ConstPtr &a,
709 const QQmlJSScope::ConstPtr &b) const
710{
711 if (a.isNull())
712 return b;
713
714 if (b.isNull())
715 return a;
716
717 const auto baseOrExtension
718 = [](const QQmlJSScope::ConstPtr &a, const QQmlJSScope::ConstPtr &b) {
719 QQmlJSScope::ConstPtr found;
720 QQmlJSUtils::searchBaseAndExtensionTypes(
721 a, [&](const QQmlJSScope::ConstPtr &scope, QQmlJSScope::ExtensionKind kind) {
722 switch (kind) {
723 case QQmlJSScope::NotExtension:
724 // If b inherits scope, then scope is a common base type of a and b
725 if (b->inherits(scope)) {
726 found = scope;
727 return true;
728 }
729 break;
730 case QQmlJSScope::ExtensionJavaScript:
731 // Merging a type with its JavaScript extension produces the type.
732 // Giving the JavaScript extension as type to be read means we expect any type
733 // that fulfills the given JavaScript interface
734 if (scope == b) {
735 found = a;
736 return true;
737 }
738 break;
739 case QQmlJSScope::ExtensionType:
740 case QQmlJSScope::ExtensionNamespace:
741 break;
742 }
743 return false;
744 });
745 return found;
746 };
747
748 if (a == b)
749 return a;
750
751 if (a == jsValueType() || a == varType())
752 return a;
753 if (b == jsValueType() || b == varType())
754 return b;
755
756 const auto isInt32Compatible = [&](const QQmlJSScope::ConstPtr &type) {
757 return (isIntegral(type)
758 && type != uint32Type()
759 && type != int64Type()
760 && type != uint64Type())
761 || type == boolType();
762 };
763
764 if (isInt32Compatible(a) && isInt32Compatible(b))
765 return int32Type();
766
767 const auto isUInt32Compatible = [&](const QQmlJSScope::ConstPtr &type) {
768 return (isUnsignedInteger(type) && type != uint64Type()) || type == boolType();
769 };
770
771 if (isUInt32Compatible(a) && isUInt32Compatible(b))
772 return uint32Type();
773
774 if (isNumeric(a) && isNumeric(b))
775 return realType();
776
777 if (isPrimitive(a) && isPrimitive(b))
778 return jsPrimitiveType();
779
780 if (const auto base = baseOrExtension(a, b))
781 return base;
782
783 if (const auto base = baseOrExtension(b, a))
784 return base;
785
786 if ((a == nullType() || a == boolType()) && b->isReferenceType())
787 return b;
788
789 if ((b == nullType() || b == boolType()) && a->isReferenceType())
790 return a;
791
792 return varType();
793}
794
795bool QQmlJSTypeResolver::canHold(
796 const QQmlJSScope::ConstPtr &container, const QQmlJSScope::ConstPtr &contained) const
797{
798 if (container == contained || container == m_varType || container == m_jsValueType)
799 return true;
800
801 if (container == m_jsPrimitiveType)
802 return isPrimitive(contained);
803
804 if (container == m_variantListType)
805 return contained->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence;
806
807 if (container == m_qObjectListType || container == m_listPropertyType) {
808 if (contained->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence)
809 return false;
810 if (QQmlJSScope::ConstPtr element = contained->elementType())
811 return element->isReferenceType();
812 return false;
813 }
814
815 if (QQmlJSUtils::searchBaseAndExtensionTypes(
816 container, [&](const QQmlJSScope::ConstPtr &base) {
817 return base == contained;
818 })) {
819 return true;
820 }
821
822 if (container->isReferenceType()) {
823 if (QQmlJSUtils::searchBaseAndExtensionTypes(
824 contained, [&](const QQmlJSScope::ConstPtr &base) {
825 return base == container;
826 })) {
827 return true;
828 }
829 }
830
831 return false;
832}
833
834
835bool QQmlJSTypeResolver::canHoldUndefined(QQmlJSRegisterContent content) const
836{
837 const auto canBeUndefined = [this](const QQmlJSScope::ConstPtr &type) {
838 return type == m_voidType || type == m_varType
839 || type == m_jsValueType || type == m_jsPrimitiveType;
840 };
841
842 if (!canBeUndefined(content.containedType()))
843 return false;
844
845 if (!content.isConversion())
846 return true;
847
848 const auto &origins = content.conversionOrigins();
849 for (const auto &origin : origins) {
850 if (canBeUndefined(originalContainedType(origin)))
851 return true;
852 }
853
854 return false;
855}
856
857bool QQmlJSTypeResolver::isOptionalType(QQmlJSRegisterContent content) const
858{
859 if (!content.isConversion())
860 return false;
861
862 const auto &origins = content.conversionOrigins();
863 if (origins.length() != 2)
864 return false;
865
866 // Conversion origins are always adjusted to the conversion result. None of them will be void.
867 // Therefore, retrieve the originals first.
868
869 return original(origins[0]).contains(m_voidType) || original(origins[1]).contains(m_voidType);
870}
871
872QQmlJSRegisterContent QQmlJSTypeResolver::extractNonVoidFromOptionalType(
873 QQmlJSRegisterContent content) const
874{
875 if (!isOptionalType(content))
876 return QQmlJSRegisterContent();
877
878 // Conversion origins are always adjusted to the conversion result. None of them will be void.
879 // Therefore, retrieve the originals first.
880
881 QList<QQmlJSRegisterContent> origins(content.conversionOrigins());
882 std::transform(origins.cbegin(), origins.cend(), origins.begin(),
883 [this](QQmlJSRegisterContent content) {return original(content);});
884 const auto &constOrigins = origins;
885 const QQmlJSRegisterContent result = constOrigins[0].contains(m_voidType)
886 ? constOrigins[1]
887 : constOrigins[0];
888
889 // The result may still be undefined. You can write "undefined ?? undefined ?? 1"
890 return result;
891}
892
893QQmlJSScope::ConstPtr QQmlJSTypeResolver::genericType(
894 const QQmlJSScope::ConstPtr &type,
895 ComponentIsGeneric allowComponent) const
896{
897 if (type->isScript())
898 return m_jsValueType;
899
900 if (type == m_metaObjectType)
901 return m_metaObjectType;
902
903 if (type->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
904 QString unresolvedBaseTypeName;
905 for (auto base = type; base;) {
906 // QObject and QQmlComponent are the two required base types.
907 // Any QML type system has to define those, or use the ones from builtins.
908 // As QQmlComponent is derived from QObject, we can restrict ourselves to the latter.
909 // This results in less if'ery when retrieving a QObject* from somewhere and deciding
910 // what it is.
911 if (base->internalName() == u"QObject"_s) {
912 return base;
913 } else if (allowComponent == ComponentIsGeneric::Yes
914 && base->internalName() == u"QQmlComponent"_s) {
915 return base;
916 }
917
918 if (auto baseBase = base->baseType()) {
919 base = baseBase;
920 } else {
921 unresolvedBaseTypeName = base->baseTypeName();
922 break;
923 }
924 }
925
926 // Reference types that are not QObject or QQmlComponent are likely JavaScript objects.
927 // We don't want to deal with those, but m_jsValueType is the best generic option.
928 if (type->filePath().isEmpty())
929 return m_jsValueType;
930
931 m_logger->log(u"Object type %1 is not derived from QObject or QQmlComponent. "
932 "You may need to fully qualify all names in C++ so that moc can see them. "
933 "You may also need to add qt_extract_metatypes(<target containing %2>)."_s
934 .arg(type->internalName(), unresolvedBaseTypeName),
935 qmlCompiler, type->sourceLocation());
936
937 // If it does have a filePath, it's some C++ type which we haven't fully resolved.
938 return m_jsValueType;
939 }
940
941 if (type->isListProperty())
942 return m_listPropertyType;
943
944 if (type->scopeType() == QQmlSA::ScopeType::EnumScope)
945 return type->baseType();
946
947 if (isPrimitive(type)) {
948 // If the filePath is set, the type is storable and we can just return it.
949 if (!type->filePath().isEmpty())
950 return type;
951
952 // If the type is JavaScript's 'number' type, it's not directly storable, but still
953 // primitive. We use C++ 'double' then.
954 if (isNumeric(type))
955 return m_realType;
956
957 // Otherwise we use QJSPrimitiveValue.
958 // TODO: JavaScript's 'string' and 'boolean' could be special-cased here.
959 return m_jsPrimitiveType;
960 }
961
962 for (const QQmlJSScope::ConstPtr &builtin : {
963 m_realType, m_floatType, m_int8Type, m_uint8Type, m_int16Type, m_uint16Type,
964 m_int32Type, m_uint32Type, m_int64Type, m_uint64Type, m_boolType, m_stringType,
965 m_stringListType, m_byteArrayType, m_urlType, m_dateTimeType, m_dateType,
966 m_timeType, m_variantListType, m_variantMapType, m_varType, m_jsValueType,
967 m_jsPrimitiveType, m_listPropertyType, m_qObjectType, m_qObjectListType,
968 m_metaObjectType, m_forInIteratorPtr, m_forOfIteratorPtr }) {
969 if (type == builtin || type == builtin->listType())
970 return type;
971 }
972
973 return m_varType;
974}
975
976/*!
977 * \internal
978 * The type of a JavaScript literal value appearing in script code
979 */
980QQmlJSRegisterContent QQmlJSTypeResolver::literalType(const QQmlJSScope::ConstPtr &type) const
981{
982 return m_pool->createType(
983 type, QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::Literal);
984}
985
986/*!
987 * \internal
988 * The type of the result of a JavaScript operation
989 */
990QQmlJSRegisterContent QQmlJSTypeResolver::operationType(const QQmlJSScope::ConstPtr &type) const
991{
992 return m_pool->createType(
993 type, QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::Operation);
994}
995
996/*!
997 * \internal
998 * A type named explicitly, for example in "as"-casts or as function annotation.
999 */
1000QQmlJSRegisterContent QQmlJSTypeResolver::namedType(const QQmlJSScope::ConstPtr &type) const
1001{
1002 return m_pool->createType(
1003 type, QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::TypeByName);
1004}
1005
1006QQmlJSRegisterContent QQmlJSTypeResolver::syntheticType(const QQmlJSScope::ConstPtr &type) const
1007{
1008 return m_pool->createType(
1009 type, QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::Unknown);
1010}
1011
1012static QQmlJSRegisterContent::ContentVariant scopeContentVariant(QQmlJSScope::ExtensionKind mode,
1013 bool isMethod)
1014{
1015 switch (mode) {
1016 case QQmlJSScope::NotExtension:
1017 case QQmlJSScope::ExtensionType:
1018 case QQmlJSScope::ExtensionJavaScript:
1019 return isMethod
1020 ? QQmlJSRegisterContent::Method
1021 : QQmlJSRegisterContent::Property;
1022 case QQmlJSScope::ExtensionNamespace:
1023 break;
1024 }
1025 Q_UNREACHABLE_RETURN(QQmlJSRegisterContent::Unknown);
1026}
1027
1028static bool isRevisionAllowed(int memberRevision, const QQmlJSScope::ConstPtr &scope)
1029{
1030 Q_ASSERT(scope->isComposite());
1031 const QTypeRevision revision = QTypeRevision::fromEncodedVersion(memberRevision);
1032
1033 // If the memberRevision is either invalid or 0.0, then everything is allowed.
1034 if (!revision.isValid() || revision == QTypeRevision::zero())
1035 return true;
1036
1037 const QTypeRevision typeRevision = QQmlJSScope::nonCompositeBaseRevision(
1038 {scope->baseType(), scope->baseTypeRevision()});
1039
1040 // If the revision is not valid, we haven't found a non-composite base type.
1041 // There is nothing we can say about the property then.
1042 return typeRevision.isValid() && typeRevision >= revision;
1043}
1044
1045QQmlJSScope::ConstPtr QQmlJSTypeResolver::resolveParentProperty(
1046 const QString &name, const QQmlJSScope::ConstPtr &base,
1047 const QQmlJSScope::ConstPtr &propType) const
1048{
1049 if (m_parentMode != UseDocumentParent || name != base->parentPropertyName())
1050 return propType;
1051
1052 const QQmlJSScope::ConstPtr baseParent = base->parentScope();
1053 if (!baseParent || !baseParent->inherits(propType))
1054 return propType;
1055
1056 const QString defaultPropertyName = baseParent->defaultPropertyName();
1057 if (defaultPropertyName.isEmpty()) // no reason to search for bindings
1058 return propType;
1059
1060 const QList<QQmlJSMetaPropertyBinding> defaultPropBindings
1061 = baseParent->propertyBindings(defaultPropertyName);
1062 for (const QQmlJSMetaPropertyBinding &binding : defaultPropBindings) {
1063 if (binding.bindingType() == QQmlSA::BindingType::Object && binding.objectType() == base)
1064 return baseParent;
1065 }
1066
1067 return propType;
1068}
1069
1070using ComponentBehavior = QmlIR::Pragma::ComponentBehaviorValue;
1071
1073{
1074 return objectsById.componentsAreBound() ? QmlIR::Pragma::Bound : QmlIR::Pragma::Unbound;
1075}
1076
1077/*!
1078 * \internal
1079 *
1080 * Returns the context object type of the context \a scope belongs to, if it has a property or
1081 * method called \a name. The context object is the root object of the surrounding component.
1082 * If \a behavior is Bound, the context objects of the lexically enclosing contexts are searched as
1083 * well, up to the root of the document or of an inline component. Returns nullptr if no root has
1084 * the name, or we cannot tell where the component starts.
1085 */
1087 const QQmlJSScope::ConstPtr &scope, const QString &name, ComponentBehavior behavior)
1088{
1089 // The keywords come before everything else, including the context object.
1090 if (QV4::Compiler::Codegen::isNameKeyword(name))
1091 return {};
1092
1093 const auto hasName = [&](const QQmlJSScope::ConstPtr &root) {
1094 return QQmlJSUtils::searchBaseAndExtensionTypes(
1095 root, [&](const QQmlJSScope::ConstPtr &found, QQmlJSScope::ExtensionKind mode) {
1096 return mode != QQmlJSScope::ExtensionNamespace
1097 && (found->hasOwnProperty(name) || found->hasOwnMethod(name));
1098 });
1099 };
1100
1101 for (QQmlJSScope::ConstPtr current = scope; current; current = current->parentScope()) {
1102 if (current->scopeType() != QQmlSA::ScopeType::QMLScope)
1103 continue;
1104
1105 const QQmlJSScope::IsComponentRoot status = current->componentRootStatus();
1106 if (status == QQmlJSScope::IsComponentRoot::No)
1107 continue;
1108
1109 // If the status is Maybe, we may return a context object from an outer context even
1110 // though the search would have had to stop at an inner one and hand off to the next
1111 // stage. That can only make the code generator reject code it could have compiled, so
1112 // it is safe.
1113 if (hasName(current))
1114 return current;
1115
1116 if (status == QQmlJSScope::IsComponentRoot::Yes
1117 && (behavior == QmlIR::Pragma::Unbound || current->isFileRootComponent()
1118 || current->isInlineComponent())) {
1119 return {};
1120 }
1121 }
1122
1123 return {};
1124}
1125
1126/*!
1127 * \internal
1128 *
1129 * Retrieves the type of whatever \a name signifies in the given \a scope.
1130 * \a name can be an ID, a property of the scope, a singleton, an attachment,
1131 * a plain type reference or a JavaScript global.
1132 *
1133 * The lookup follows the rules in the internal "Resolving Unqualified Names in QML"
1134 * documentation (qml-name-resolution.qdoc), as far as the compiler can know the
1135 * contexts and scope objects involved. User-created contexts are not visible
1136 * ahead of time. They are only searched after the global object, and script
1137 * strings, the only code that can run in one as immediate context, are not
1138 * compiled. The global object, including the JavaScript globals, can therefore
1139 * be resolved here.
1140 *
1141 * \a options tells us whether to consider components as bound. If components
1142 * are bound we can retrieve objects identified by ID in outer contexts.
1143 *
1144 * Names found on the context object (rule 6) are resolved, but the code
1145 * generator does not generate code for them. The linter warns about them as
1146 * unqualified access.
1147 *
1148 * Rule 7: If components are bound, the context objects of the lexically
1149 * enclosing contexts come after the context object and before the global
1150 * object. They are searched, too, and the code generator does not generate
1151 * code for names they provide either.
1152 */
1153QQmlJSScope::ConstPtr QQmlJSTypeResolver::scopedType(
1154 const QQmlJSScope::ConstPtr &scope, const QString &name,
1155 QQmlJSScopesByIdOptions options) const
1156{
1157 // Type names come before everything but the keywords. Lower-case type names are only found if
1158 // value types are addressable. Otherwise containedTypeForName() doesn't find them.
1159 if (QQmlJSScope::ConstPtr result = containedTypeForName(name))
1160 return result;
1161
1162 QQmlJSScopesById::CertainCallback<QQmlJSScope::ConstPtr> identified;
1163 if (m_objectsById.possibleScopes(name, scope, options, identified)
1164 != QQmlJSScopesById::Success::Yes) {
1165 // Could not determine component boundaries
1166 return {};
1167 }
1168
1169 if (identified.result) {
1170 // Found a definite match
1171 return identified.result;
1172 }
1173
1174 if (QQmlJSScope::ConstPtr base = QQmlJSScope::findCurrentQMLScope(scope)) {
1175 QQmlJSScope::ConstPtr result;
1176 if (QQmlJSUtils::searchBaseAndExtensionTypes(
1177 base, [&](const QQmlJSScope::ConstPtr &found, QQmlJSScope::ExtensionKind mode) {
1178 if (mode == QQmlJSScope::ExtensionNamespace) // no use for it here
1179 return false;
1180
1181 if (found->hasOwnProperty(name)) {
1182 const QQmlJSMetaProperty prop = found->ownProperty(name);
1183 if (!isRevisionAllowed(prop.revision(), scope))
1184 return false;
1185
1186 result = resolveParentProperty(name, base, prop.type());
1187 return true;
1188 }
1189
1190 if (found->hasOwnMethod(name)) {
1191 const auto methods = found->ownMethods(name);
1192 for (const auto &method : methods) {
1193 if (isRevisionAllowed(method.revision(), scope)) {
1194 result = jsValueType();
1195 return true;
1196 }
1197 }
1198 }
1199
1200 return false;
1201 })) {
1202 return result;
1203 }
1204 }
1205
1206 // Rule 6: The context object comes before the global object.
1207 if (const QQmlJSScope::ConstPtr root = contextObjectWithName(
1208 scope, name, componentBehavior(m_objectsById))) {
1209 QQmlJSScope::ConstPtr result;
1210 QQmlJSUtils::searchBaseAndExtensionTypes(
1211 root, [&](const QQmlJSScope::ConstPtr &found, QQmlJSScope::ExtensionKind mode) {
1212 if (mode == QQmlJSScope::ExtensionNamespace)
1213 return false;
1214 if (found->hasOwnProperty(name)) {
1215 result = resolveParentProperty(name, root, found->ownProperty(name).type());
1216 return true;
1217 }
1218 if (found->hasOwnMethod(name)) {
1219 result = jsValueType();
1220 return true;
1221 }
1222 return false;
1223 });
1224 return result;
1225 }
1226
1227 if (m_jsGlobalObject->hasProperty(name))
1228 return m_jsGlobalObject->property(name).type();
1229
1230 if (m_jsGlobalObject->hasMethod(name))
1231 return jsValueType();
1232
1233 return {};
1234}
1235
1236/*!
1237 * \internal
1238 *
1239 * Same as the other scopedType method, but accepts a QQmlJSRegisterContent and
1240 * also returns one. This way you not only get the type, but also the content
1241 * variant and various meta info.
1242 */
1243QQmlJSRegisterContent QQmlJSTypeResolver::scopedType(QQmlJSRegisterContent scope,
1244 const QString &name, int lookupIndex,
1245 QQmlJSScopesByIdOptions options) const
1246{
1247 const QQmlJSScope::ConstPtr contained = scope.containedType();
1248
1249 // Type names come before everything but the keywords. registerContentForName() only finds
1250 // lower-case value types if they are addressable.
1251 if (QQmlJSRegisterContent result = registerContentForName(name, scope); result.isValid())
1252 return result;
1253
1254 QQmlJSScopesById::CertainCallback<QQmlJSScope::ConstPtr> identified;
1255 if (m_objectsById.possibleScopes(name, contained, options, identified)
1256 != QQmlJSScopesById::Success::Yes) {
1257 // Could not determine component boundaries
1258 return {};
1259 }
1260
1261 if (identified.result) {
1262 // Found a definite match
1263 return m_pool->createType(
1264 identified.result, lookupIndex, QQmlJSRegisterContent::ObjectById, scope);
1265 }
1266
1267 if (QQmlJSScope::ConstPtr base = QQmlJSScope::findCurrentQMLScope(contained)) {
1268 QQmlJSRegisterContent result;
1269 if (QQmlJSUtils::searchBaseAndExtensionTypes(
1270 base, [&](const QQmlJSScope::ConstPtr &found, QQmlJSScope::ExtensionKind mode) {
1271 if (mode == QQmlJSScope::ExtensionNamespace) // no use for it here
1272 return false;
1273
1274 const QQmlJSRegisterContent resultScope = mode == QQmlJSScope::NotExtension
1275 ? scope
1276 : extensionType(found, scope);
1277
1278 if (found->hasOwnProperty(name)) {
1279 QQmlJSMetaProperty prop = found->ownProperty(name);
1280 if (!isRevisionAllowed(prop.revision(), contained))
1281 return false;
1282
1283 prop.setType(resolveParentProperty(name, base, prop.type()));
1284 result = m_pool->createProperty(
1285 prop, QQmlJSRegisterContent::InvalidLookupIndex, lookupIndex,
1286 scopeContentVariant(mode, false), resultScope);
1287 return true;
1288 }
1289
1290 if (found->hasOwnMethod(name)) {
1291 auto methods = found->ownMethods(name);
1292 for (auto it = methods.begin(); it != methods.end();) {
1293 if (!isRevisionAllowed(it->revision(), contained))
1294 it = methods.erase(it);
1295 else
1296 ++it;
1297 }
1298 if (methods.isEmpty())
1299 return false;
1300 result = m_pool->createMethod(
1301 methods, jsValueType(), scopeContentVariant(mode, true), resultScope);
1302 return true;
1303 }
1304
1305 // Unqualified enums are not allowed
1306 return false;
1307 })) {
1308 return result;
1309 }
1310 }
1311
1312 // Rule 6: The context object comes before the global object.
1313 if (const QQmlJSScope::ConstPtr root = contextObjectWithName(
1314 contained, name, componentBehavior(m_objectsById))) {
1315 const QQmlJSRegisterContent contextObject = m_pool->createType(
1316 root, QQmlJSRegisterContent::InvalidLookupIndex,
1317 QQmlJSRegisterContent::ContextObject, scope);
1318 QQmlJSRegisterContent result;
1319 QQmlJSUtils::searchBaseAndExtensionTypes(
1320 root, [&](const QQmlJSScope::ConstPtr &found, QQmlJSScope::ExtensionKind mode) {
1321 if (mode == QQmlJSScope::ExtensionNamespace)
1322 return false;
1323
1324 const QQmlJSRegisterContent resultScope = mode == QQmlJSScope::NotExtension
1325 ? contextObject
1326 : extensionType(found, contextObject);
1327
1328 if (found->hasOwnProperty(name)) {
1329 QQmlJSMetaProperty prop = found->ownProperty(name);
1330 prop.setType(resolveParentProperty(name, root, prop.type()));
1331 result = m_pool->createProperty(
1332 prop, QQmlJSRegisterContent::InvalidLookupIndex, lookupIndex,
1333 scopeContentVariant(mode, false), resultScope);
1334 return true;
1335 }
1336
1337 if (!found->hasOwnMethod(name))
1338 return false;
1339
1340 result = m_pool->createMethod(
1341 found->ownMethods(name), jsValueType(), scopeContentVariant(mode, true),
1342 resultScope);
1343 return true;
1344 });
1345 return result;
1346 }
1347
1348 if (m_jsGlobalObject->hasProperty(name)) {
1349 return m_pool->createProperty(
1350 m_jsGlobalObject->property(name), QQmlJSRegisterContent::InvalidLookupIndex,
1351 lookupIndex, QQmlJSRegisterContent::Property, m_jsGlobalObjectContent);
1352 } else if (m_jsGlobalObject->hasMethod(name)) {
1353 return m_pool->createMethod(
1354 m_jsGlobalObject->methods(name), jsValueType(),
1355 QQmlJSRegisterContent::Property, m_jsGlobalObjectContent);
1356 }
1357
1358 return {};
1359}
1360
1361/*!
1362 * \fn QQmlJSScope::ConstPtr typeForId(const QQmlJSScope::ConstPtr &scope, const QString &name, QQmlJSScopesByIdOptions options) const
1363 *
1364 * \internal
1365 *
1366 * Same as scopedType(), but assumes that the \a name is an ID and only searches
1367 * the context.
1368 */
1369
1370bool QQmlJSTypeResolver::checkEnums(
1371 QQmlJSRegisterContent scope, const QString &name,
1372 QQmlJSRegisterContent *result) const
1373{
1374 // You can't have lower case enum names in QML, even if we know the enums here.
1375 if (name.isEmpty() || !name.at(0).isUpper())
1376 return false;
1377
1378 const auto enums = scope.containedType()->ownEnumerations();
1379 for (const auto &enumeration : enums) {
1380 if (enumeration.name() == name) {
1381 *result = m_pool->createEnumeration(
1382 enumeration, QString(),
1383 QQmlJSRegisterContent::Enum,
1384 scope);
1385 return true;
1386 }
1387
1388 if (!(scope.containedType()->enforcesScopedEnums() && enumeration.isScoped())
1389 && enumeration.hasKey(name)) {
1390 *result = m_pool->createEnumeration(
1391 enumeration, name,
1392 QQmlJSRegisterContent::Enum,
1393 scope);
1394 return true;
1395 }
1396 }
1397
1398 return false;
1399}
1400
1401bool QQmlJSTypeResolver::canPopulate(
1402 const QQmlJSScope::ConstPtr &type, const QQmlJSScope::ConstPtr &passedArgumentType,
1403 bool *isExtension) const
1404{
1405 // TODO: We could allow QVariantMap and QVariantHash to be populated, but that needs extra
1406 // code in the code generator.
1407
1408 if (type.isNull()
1409 || canHold(passedArgumentType, type)
1410 || isPrimitive(passedArgumentType)
1411 || type->accessSemantics() != QQmlJSScope::AccessSemantics::Value
1412 || !type->isStructured()) {
1413 return false;
1414 }
1415
1416 if (isExtension)
1417 *isExtension = !type->extensionType().scope.isNull();
1418
1419 return true;
1420}
1421
1422QQmlJSMetaMethod QQmlJSTypeResolver::selectConstructor(
1423 const QQmlJSScope::ConstPtr &type, const QQmlJSScope::ConstPtr &passedArgumentType,
1424 bool *isExtension) const
1425{
1426 // If the "from" type can hold the target type, we should not try to coerce
1427 // it to any constructor argument.
1428 if (type.isNull()
1429 || canHold(passedArgumentType, type)
1430 || type->accessSemantics() != QQmlJSScope::AccessSemantics::Value
1431 || !type->isCreatable()) {
1432 return QQmlJSMetaMethod();
1433 }
1434
1435 auto doSelectConstructor = [&](const QQmlJSScope::ConstPtr &ctorProvider) {
1436 QQmlJSMetaMethod candidate;
1437
1438 const auto ownMethods = ctorProvider->ownMethods();
1439 for (const QQmlJSMetaMethod &method : ownMethods) {
1440 if (!method.isConstructor())
1441 continue;
1442
1443 const auto index = method.constructorIndex();
1444 Q_ASSERT(index != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
1445
1446 const auto methodArguments = method.parameters();
1447 if (methodArguments.size() != 1)
1448 continue;
1449
1450 const QQmlJSScope::ConstPtr methodArgumentType = methodArguments[0].type();
1451
1452 // Do not select the copy constructor (also not if disguised via the extension).
1453 if (methodArgumentType == type)
1454 continue;
1455
1456 if (passedArgumentType == methodArgumentType)
1457 return method;
1458
1459 // Do not select further ctors here. We don't want to do multi-step construction as that
1460 // is confusing and easily leads to infinite recursion.
1461 if (!candidate.isValid()
1462 && canPrimitivelyConvertFromTo(passedArgumentType, methodArgumentType)) {
1463 candidate = method;
1464 }
1465 }
1466
1467 return candidate;
1468 };
1469
1470 if (QQmlJSScope::ConstPtr extension = type->extensionType().scope) {
1471 const QQmlJSMetaMethod ctor = doSelectConstructor(extension);
1472 if (ctor.isValid()) {
1473 if (isExtension)
1474 *isExtension = true;
1475 return ctor;
1476 }
1477 }
1478
1479 if (isExtension)
1480 *isExtension = false;
1481
1482 return doSelectConstructor(type);
1483}
1484
1485bool QQmlJSTypeResolver::areEquivalentLists(
1486 const QQmlJSScope::ConstPtr &a, const QQmlJSScope::ConstPtr &b) const
1487{
1488 const QQmlJSScope::ConstPtr equivalentLists[2][2] = {
1489 { m_stringListType, m_stringType->listType() },
1490 { m_variantListType, m_varType->listType() }
1491 };
1492
1493 for (const auto eq : equivalentLists) {
1494 if ((a == eq[0] && b == eq[1]) || (a == eq[1] && b == eq[0]))
1495 return true;
1496 }
1497
1498 return false;
1499}
1500
1501bool QQmlJSTypeResolver::isTriviallyCopyable(const QQmlJSScope::ConstPtr &type) const
1502{
1503 // pointers are trivially copyable
1504 if (type->isReferenceType())
1505 return true;
1506
1507 // Enum values are trivially copyable
1508 if (type->scopeType() == QQmlSA::ScopeType::EnumScope)
1509 return true;
1510
1511 for (const QQmlJSScope::ConstPtr &trivial : {
1512 m_nullType, m_voidType,
1513 m_boolType, m_metaObjectType,
1514 m_realType, m_floatType,
1515 m_int8Type, m_uint8Type,
1516 m_int16Type, m_uint16Type,
1517 m_int32Type, m_uint32Type,
1518 m_int64Type, m_uint64Type }) {
1519 if (type == trivial)
1520 return true;
1521 }
1522
1523 return false;
1524}
1525
1526bool QQmlJSTypeResolver::inherits(const QQmlJSScope::ConstPtr &derived, const QQmlJSScope::ConstPtr &base) const
1527{
1528 const bool matchByName = !base->isComposite();
1529 for (QQmlJSScope::ConstPtr derivedBase = derived; derivedBase;
1530 derivedBase = derivedBase->baseType()) {
1531 if (derivedBase == base)
1532 return true;
1533 if (matchByName
1534 && !derivedBase->isComposite()
1535 && derivedBase->internalName() == base->internalName()) {
1536 return true;
1537 }
1538 }
1539 return false;
1540}
1541
1542bool QQmlJSTypeResolver::canPrimitivelyConvertFromTo(
1543 const QQmlJSScope::ConstPtr &from, const QQmlJSScope::ConstPtr &to) const
1544{
1545 if (from == to)
1546 return true;
1547 if (from == m_varType || to == m_varType)
1548 return true;
1549 if (from == m_jsValueType || to == m_jsValueType)
1550 return true;
1551 if (to == m_qQmlScriptStringType)
1552 return true;
1553 if (isNumeric(from) && isNumeric(to))
1554 return true;
1555 // We can convert everything to bool.
1556 if (to == m_boolType)
1557 return true;
1558
1559 if (from->accessSemantics() == QQmlJSScope::AccessSemantics::Reference
1560 && to == m_stringType) {
1561 return true;
1562 }
1563
1564 // Yes, our String has number constructors.
1565 if (isNumeric(from) && to == m_stringType)
1566 return true;
1567
1568 // We can convert strings to numbers, but not to enums
1569 if (from == m_stringType && isNumeric(to))
1570 return to->scopeType() != QQmlJSScope::ScopeType::EnumScope;
1571
1572 // We can always convert between strings and urls.
1573 if ((from == m_stringType && to == m_urlType)
1574 || (from == m_urlType && to == m_stringType)) {
1575 return true;
1576 }
1577
1578 // We can always convert between strings and byte arrays.
1579 if ((from == m_stringType && to == m_byteArrayType)
1580 || (from == m_byteArrayType && to == m_stringType)) {
1581 return true;
1582 }
1583
1584 if (to == m_voidType)
1585 return true;
1586
1587 if (to.isNull())
1588 return from == m_voidType;
1589
1590 const auto types = { m_dateTimeType, m_dateType, m_timeType, m_stringType };
1591 for (const auto &originType : types) {
1592 if (from != originType)
1593 continue;
1594
1595 for (const auto &targetType : types) {
1596 if (to == targetType)
1597 return true;
1598 }
1599
1600 if (to == m_realType)
1601 return true;
1602
1603 break;
1604 }
1605
1606 if (from == m_nullType && to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
1607 return true;
1608
1609 if (from == m_jsPrimitiveType) {
1610 // You can cast any primitive to a nullptr
1611 return isPrimitive(to) || to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference;
1612 }
1613
1614 if (to == m_jsPrimitiveType)
1615 return isPrimitive(from);
1616
1617 const bool matchByName = !to->isComposite();
1618 Q_ASSERT(!matchByName || !to->internalName().isEmpty());
1619 if (QQmlJSUtils::searchBaseAndExtensionTypes(
1620 from, [&](const QQmlJSScope::ConstPtr &scope, QQmlJSScope::ExtensionKind kind) {
1621 switch (kind) {
1622 case QQmlJSScope::NotExtension:
1623 case QQmlJSScope::ExtensionJavaScript:
1624 // Converting to a base type is trivially supported.
1625 // Converting to the JavaScript extension of a type just produces the type itself.
1626 // Giving the JavaScript extension as type to be converted to means we expect any
1627 // result that fulfills the given JavaScript interface.
1628 return scope == to
1629 || (matchByName && scope->internalName() == to->internalName());
1630 case QQmlJSScope::ExtensionType:
1631 case QQmlJSScope::ExtensionNamespace:
1632 break;
1633 }
1634 return false;
1635 })) {
1636 return true;
1637 }
1638
1639 if (from == m_variantListType)
1640 return to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence;
1641
1642 // We can convert anything that fits natively into QJSPrimitiveValue
1643 if (canPrimitivelyConvertFromTo(from, m_jsPrimitiveType)
1644 && canPrimitivelyConvertFromTo(m_jsPrimitiveType, to)) {
1645 return true;
1646 }
1647
1648 if (areEquivalentLists(from, to))
1649 return true;
1650
1651 if (from->isListProperty()
1652 && to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
1653 && canConvertFromTo(from->elementType(), to->elementType())) {
1654 return true;
1655 }
1656
1657 // it is possible to assing a singlar object to a list property if it could be stored in the list
1658 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
1659 && from->accessSemantics() == QQmlJSScope::AccessSemantics::Reference
1660 && from->inherits(to->elementType())) {
1661 return true;
1662 }
1663
1664 if (to == m_stringType && from->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence)
1665 return canConvertFromTo(from->elementType(), m_stringType);
1666
1667 return false;
1668}
1669
1670QQmlJSRegisterContent QQmlJSTypeResolver::lengthProperty(
1671 bool isWritable, QQmlJSRegisterContent scope) const
1672{
1673 QQmlJSMetaProperty prop;
1674 prop.setPropertyName(u"length"_s);
1675 prop.setTypeName(u"qsizetype"_s);
1676 prop.setType(sizeType());
1677 prop.setIsWritable(isWritable);
1678 return m_pool->createProperty(
1679 prop, QQmlJSRegisterContent::InvalidLookupIndex,
1680 QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::Property, scope);
1681}
1682
1683QQmlJSRegisterContent QQmlJSTypeResolver::createVariantProperty(QQmlJSRegisterContent type,
1684 const QString &name,
1685 int baseLookupIndex,
1686 int resultLookupIndex) const
1687{
1688 QQmlJSMetaProperty prop;
1689 prop.setPropertyName(name);
1690 prop.setTypeName(u"QVariant"_s);
1691 prop.setType(varType());
1692 prop.setIsWritable(true);
1693 return m_pool->createProperty(prop, baseLookupIndex, resultLookupIndex,
1694 QQmlJSRegisterContent::Property, type);
1695}
1696
1697QQmlJSRegisterContent QQmlJSTypeResolver::memberType(
1698 QQmlJSRegisterContent type, const QString &name, int baseLookupIndex,
1699 int resultLookupIndex) const
1700{
1701 QQmlJSRegisterContent result;
1702 const QQmlJSScope::ConstPtr contained = type.containedType();
1703
1704 const auto check = [&](const QQmlJSScope::ConstPtr &scope, QQmlJSScope::ExtensionKind mode) {
1705 const QQmlJSRegisterContent resultScope = mode == QQmlJSScope::NotExtension
1706 ? baseType(scope, type)
1707 : extensionType(scope, type);
1708
1709 if (mode != QQmlJSScope::ExtensionNamespace) {
1710 if (scope->hasOwnProperty(name)) {
1711 const auto prop = scope->ownProperty(name);
1712 result = m_pool->createProperty(
1713 prop, baseLookupIndex, resultLookupIndex,
1714 QQmlJSRegisterContent::Property, resultScope);
1715 return true;
1716 }
1717
1718 if (scope->hasOwnMethod(name)) {
1719 const auto methods = scope->ownMethods(name);
1720 result = m_pool->createMethod(
1721 methods, jsValueType(), QQmlJSRegisterContent::Method, resultScope);
1722 return true;
1723 }
1724 }
1725 // don't look up enums on ids...
1726 if (type.variant() == QQmlJSRegisterContent::ObjectById)
1727 return false;
1728 return checkEnums(resultScope, name, &result);
1729 };
1730
1731 // If we got a plain type reference we have to check the enums of the _scope_.
1732 if (contained == metaObjectType())
1733 return memberEnumType(type.scope(), name);
1734
1735 if (contained->inherits(qmlPropertyMapType())) {
1736 if (QQmlJSUtils::searchBaseAndExtensionTypes(type.containedType(), check))
1737 return result;
1738 return createVariantProperty(type, name, baseLookupIndex, resultLookupIndex);
1739 }
1740
1741 if (contained == variantMapType())
1742 return createVariantProperty(type, name, baseLookupIndex, resultLookupIndex);
1743
1744 if (contained == jsValueType()) {
1745 QQmlJSMetaProperty prop;
1746 prop.setPropertyName(name);
1747 prop.setTypeName(u"QJSValue"_s);
1748 prop.setType(jsValueType());
1749 prop.setIsWritable(true);
1750 return m_pool->createProperty(prop, baseLookupIndex, resultLookupIndex,
1751 QQmlJSRegisterContent::Property, type);
1752 }
1753
1754 if ((contained == stringType()
1755 || contained->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence)
1756 && name == u"length"_s) {
1757 return lengthProperty(contained != stringType(), type);
1758 }
1759
1760 if (QQmlJSUtils::searchBaseAndExtensionTypes(type.containedType(), check))
1761 return result;
1762
1763 for (auto scope = contained;
1764 scope && (QQmlSA::isFunctionScope(scope->scopeType())
1765 || scope->scopeType() == QQmlSA::ScopeType::JSLexicalScope);
1766 scope = scope->parentScope()) {
1767 if (auto ownIdentifier = scope->ownJSIdentifier(name)) {
1768 QQmlJSMetaProperty prop;
1769 prop.setPropertyName(name);
1770 prop.setTypeName(u"QJSValue"_s);
1771 prop.setType(jsValueType());
1772 prop.setIsWritable(!(ownIdentifier.value().isConst));
1773
1774 return m_pool->createProperty(
1775 prop, baseLookupIndex, resultLookupIndex,
1776 QQmlJSRegisterContent::Property,
1777 parentScope(scope, type));
1778 }
1779 }
1780
1781 // check enums before checking attached types of attached types (chained attached types)
1782 if (type.isType()) {
1783 if (auto result = memberEnumType(type.scope(), name); result.isValid())
1784 return result;
1785 }
1786
1787 if (QQmlJSScope::ConstPtr attachedBase = typeForName(name)) {
1788 if (QQmlJSScope::ConstPtr attached = attachedBase->attachedType()) {
1789 if (!genericType(attached)) {
1790 m_logger->log(u"Cannot resolve generic base of attached %1"_s.arg(
1791 attached->internalName()),
1792 qmlCompiler, attached->sourceLocation());
1793 return {};
1794 } else if (contained->accessSemantics() != QQmlJSScope::AccessSemantics::Reference) {
1795 m_logger->log(u"Cannot retrieve attached object for non-reference type %1"_s.arg(
1796 contained->internalName()),
1797 qmlCompiler, contained->sourceLocation());
1798 return {};
1799 } else {
1800 const QQmlJSRegisterContent namedType = m_pool->createType(
1801 attachedBase, QQmlJSRegisterContent::InvalidLookupIndex,
1802 QQmlJSRegisterContent::TypeByName, type);
1803
1804 return m_pool->createType(
1805 attached, resultLookupIndex, QQmlJSRegisterContent::Attachment,
1806 namedType);
1807 }
1808 }
1809 }
1810
1811 return {};
1812}
1813
1814QQmlJSRegisterContent QQmlJSTypeResolver::memberEnumType(
1815 QQmlJSRegisterContent type, const QString &name) const
1816{
1817 QQmlJSRegisterContent result;
1818
1819 if (QQmlJSUtils::searchBaseAndExtensionTypes(
1820 type.containedType(),
1821 [&](const QQmlJSScope::ConstPtr &scope, QQmlJSScope::ExtensionKind mode) {
1822 return checkEnums(mode == QQmlJSScope::NotExtension
1823 ? baseType(scope, type)
1824 : extensionType(scope, type),
1825 name, &result);
1826 })) {
1827 return result;
1828 }
1829
1830 return {};
1831}
1832
1833QQmlJSRegisterContent QQmlJSTypeResolver::memberType(
1834 QQmlJSRegisterContent type, const QString &name, int lookupIndex) const
1835{
1836 if (type.isType())
1837 return memberType(type, name, type.resultLookupIndex(), lookupIndex);
1838 if (type.isProperty() || type.isMethodCall())
1839 return memberType(type, name, type.resultLookupIndex(), lookupIndex);
1840 if (type.isEnumeration()) {
1841 const auto enumeration = type.enumeration();
1842 if (!type.enumMember().isEmpty() || !enumeration.hasKey(name))
1843 return {};
1844 return m_pool->createEnumeration(
1845 enumeration, name, QQmlJSRegisterContent::Enum, type.scope());
1846 }
1847 if (type.isMethod()) {
1848 QQmlJSMetaProperty prop;
1849 prop.setTypeName(u"QJSValue"_s);
1850 prop.setPropertyName(name);
1851 prop.setType(jsValueType());
1852 prop.setIsWritable(true);
1853 return m_pool->createProperty(
1854 prop, QQmlJSRegisterContent::InvalidLookupIndex, lookupIndex,
1855 QQmlJSRegisterContent::Property, type);
1856 }
1857 if (type.isImportNamespace()) {
1858 Q_ASSERT(type.scopeType()->isReferenceType());
1859 return registerContentForName(name, type);
1860 }
1861 if (type.isConversion()) {
1862 if (const auto result = memberType(
1863 type, name, type.resultLookupIndex(), lookupIndex);
1864 result.isValid()) {
1865 return result;
1866 }
1867
1868 if (const auto result = memberEnumType(type.scope(), name); result.isValid())
1869 return result;
1870
1871 // If the conversion consists of only undefined and one actual type,
1872 // we can produce the members of that one type.
1873 // If the value is then actually undefined, the result is an exception.
1874
1875 const auto nonVoid = extractNonVoidFromOptionalType(type);
1876
1877 // If the conversion cannot hold the original type, it loses information.
1878 return (!nonVoid.isNull() && canHold(type.conversionResultType(), nonVoid.containedType()))
1879 ? memberType(nonVoid, name, type.resultLookupIndex(), lookupIndex)
1880 : QQmlJSRegisterContent();
1881 }
1882
1883 Q_UNREACHABLE_RETURN({});
1884}
1885
1886QQmlJSRegisterContent QQmlJSTypeResolver::elementType(QQmlJSRegisterContent list) const
1887{
1888 QQmlJSScope::ConstPtr value;
1889
1890 auto valueType = [&](const QQmlJSScope::ConstPtr &scope) -> QQmlJSScope::ConstPtr {
1891 if (scope->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence)
1892 return scope->elementType();
1893
1894 if (scope == m_forInIteratorPtr)
1895 return m_sizeType;
1896
1897 if (scope == m_forOfIteratorPtr)
1898 return list.scopeType()->elementType();
1899
1900 if (scope == m_jsValueType || scope == m_varType)
1901 return m_jsValueType;
1902
1903 if (scope == m_stringType)
1904 return m_stringType;
1905
1906 return QQmlJSScope::ConstPtr();
1907 };
1908
1909 value = valueType(list.containedType());
1910
1911 if (value.isNull())
1912 return {};
1913
1914 QQmlJSMetaProperty property;
1915 property.setPropertyName(u"[]"_s);
1916 property.setTypeName(value->internalName());
1917 property.setType(value);
1918
1919 return m_pool->createProperty(
1920 property, QQmlJSRegisterContent::InvalidLookupIndex,
1921 QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::ListValue,
1922 list);
1923}
1924
1925QQmlJSRegisterContent QQmlJSTypeResolver::returnType(
1926 const QQmlJSMetaMethod &method, const QQmlJSScope::ConstPtr &returnType,
1927 QQmlJSRegisterContent scope) const
1928{
1929 return m_pool->createMethodCall(method, returnType, scope);
1930}
1931
1932QQmlJSRegisterContent QQmlJSTypeResolver::extensionType(
1933 const QQmlJSScope::ConstPtr &extension, QQmlJSRegisterContent base) const
1934{
1935 return m_pool->createType(
1936 extension, base.resultLookupIndex(), QQmlJSRegisterContent::Extension, base);
1937}
1938
1939/*!
1940 * \internal
1941 * Encodes \a base as a base type of \a derived and returns a QQmlJSRegisterContent.
1942 * "Base type" here is understood the same way as std::is_base_of would understand it.
1943 * That means, if you pass the contained type of \a derived as \a base, then \a derived
1944 * itself is returned.
1945 */
1946QQmlJSRegisterContent QQmlJSTypeResolver::baseType(
1947 const QQmlJSScope::ConstPtr &base, QQmlJSRegisterContent derived) const
1948{
1949 return m_pool->createType(
1950 base, derived.resultLookupIndex(), QQmlJSRegisterContent::BaseType, derived);
1951}
1952
1953/*!
1954 * \internal
1955 * Encodes \a parent as a parent scope of \a child and returns a QQmlJSRegisterContent.
1956 * "Parent scope" here means any scope above, but also _including_ \a child.
1957 * That means, if you pass the contained type of \a child as \a parent, then \a child
1958 * itself is returned.
1959 */
1960QQmlJSRegisterContent QQmlJSTypeResolver::parentScope(
1961 const QQmlJSScope::ConstPtr &parent, QQmlJSRegisterContent child) const
1962{
1963 return m_pool->createType(
1964 parent, child.resultLookupIndex(), QQmlJSRegisterContent::ParentScope, child);
1965}
1966
1967QQmlJSRegisterContent QQmlJSTypeResolver::iteratorPointer(
1968 QQmlJSRegisterContent listType, QQmlJS::AST::ForEachType type,
1969 int lookupIndex) const
1970{
1971 const QQmlJSScope::ConstPtr value = (type == QQmlJS::AST::ForEachType::In)
1972 ? m_int32Type
1973 : elementType(listType).containedType();
1974
1975 QQmlJSScope::ConstPtr iteratorPointer = type == QQmlJS::AST::ForEachType::In
1976 ? m_forInIteratorPtr
1977 : m_forOfIteratorPtr;
1978
1979 QQmlJSMetaProperty prop;
1980 prop.setPropertyName(u"<>"_s);
1981 prop.setTypeName(iteratorPointer->internalName());
1982 prop.setType(iteratorPointer);
1983 return m_pool->createProperty(
1984 prop, lookupIndex,
1985 QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::ListIterator,
1986 listType);
1987}
1988
1989QQmlJSScope::ConstPtr QQmlJSTypeResolver::storedType(const QQmlJSScope::ConstPtr &type) const
1990{
1991 if (type.isNull())
1992 return {};
1993 if (type == voidType())
1994 return type;
1995 if (type->isScript())
1996 return jsValueType();
1997 if (type->isComposite()) {
1998 if (const QQmlJSScope::ConstPtr nonComposite = QQmlJSScope::nonCompositeBaseType(type))
1999 return nonComposite;
2000
2001 // If we can't find the non-composite base, we really don't know what it is.
2002 return genericType(type);
2003 }
2004 if (type->filePath().isEmpty())
2005 return genericType(type);
2006 return type;
2007}
2008
2010 QQmlJSRegisterContent from, const QQmlJSScope::ConstPtr &to,
2011 QQmlJSRegisterContent scope, QQmlJSRegisterContentPool *pool)
2012{
2013 if (from.isConversion()) {
2014 return pool->createConversion(
2015 from.conversionOrigins(), to,
2016 scope.isValid() ? scope : from.conversionResultScope(),
2017 from.variant(), from.scope());
2018 }
2019
2020 return pool->createConversion(
2021 QList<QQmlJSRegisterContent>{from},
2022 to, scope, from.variant(),
2023 from.scope());
2024}
2025
2026QQmlJSRegisterContent QQmlJSTypeResolver::convert(
2027 QQmlJSRegisterContent from, QQmlJSRegisterContent to) const
2028{
2029 return doConvert(from, to.containedType(), to.scope(), m_pool.get());
2030}
2031
2032QQmlJSRegisterContent QQmlJSTypeResolver::convert(
2033 QQmlJSRegisterContent from, const QQmlJSScope::ConstPtr &to) const
2034{
2035 return doConvert(from, to, QQmlJSRegisterContent(), m_pool.get());
2036}
2037
2038QT_END_NAMESPACE
\inmodule QtQmlCompiler
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
static QQmlJSScope::ConstPtr contextObjectWithName(const QQmlJSScope::ConstPtr &scope, const QString &name, ComponentBehavior behavior)
static ComponentBehavior componentBehavior(const QQmlJSScopesById &objectsById)
static bool isRevisionAllowed(int memberRevision, const QQmlJSScope::ConstPtr &scope)
static void assertExtension(const QQmlJSScope::ConstPtr &type, QLatin1String extension)
static QQmlJSRegisterContent::ContentVariant scopeContentVariant(QQmlJSScope::ExtensionKind mode, bool isMethod)
static QQmlJSRegisterContent doConvert(QQmlJSRegisterContent from, const QQmlJSScope::ConstPtr &to, QQmlJSRegisterContent scope, QQmlJSRegisterContentPool *pool)
static QQmlJSRegisterContent::ContentVariant mergeVariants(QQmlJSRegisterContent::ContentVariant a, QQmlJSRegisterContent::ContentVariant b)