32QQmlJSTypeResolver::QQmlJSTypeResolver(QQmlJSImporter *importer)
33 : m_pool(std::make_unique<QQmlJSRegisterContentPool>())
34 , m_imports(importer->builtinInternalNames())
35 , m_importer(importer)
37 const QQmlJSImporter::ImportedTypes &builtinTypes = m_imports;
39 m_voidType = builtinTypes.type(u"void"_s).scope;
40 assertExtension(m_voidType,
"undefined"_L1);
42 m_nullType = builtinTypes.type(u"std::nullptr_t"_s).scope;
45 m_realType = builtinTypes.type(u"double"_s).scope;
46 assertExtension(m_realType,
"Number"_L1);
48 m_floatType = builtinTypes.type(u"float"_s).scope;
49 assertExtension(m_floatType,
"Number"_L1);
51 m_int8Type = builtinTypes.type(u"qint8"_s).scope;
52 assertExtension(m_int8Type,
"Number"_L1);
54 m_uint8Type = builtinTypes.type(u"quint8"_s).scope;
55 assertExtension(m_uint8Type,
"Number"_L1);
57 m_int16Type = builtinTypes.type(u"short"_s).scope;
58 assertExtension(m_int16Type,
"Number"_L1);
60 m_uint16Type = builtinTypes.type(u"ushort"_s).scope;
61 assertExtension(m_uint16Type,
"Number"_L1);
63 m_int32Type = builtinTypes.type(u"int"_s).scope;
64 assertExtension(m_int32Type,
"Number"_L1);
66 m_uint32Type = builtinTypes.type(u"uint"_s).scope;
67 assertExtension(m_uint32Type,
"Number"_L1);
69 m_int64Type = builtinTypes.type(u"qlonglong"_s).scope;
70 Q_ASSERT(m_int64Type);
72 m_uint64Type = builtinTypes.type(u"qulonglong"_s).scope;
73 Q_ASSERT(m_uint64Type);
75 m_sizeType = builtinTypes.type(u"qsizetype"_s).scope;
76 assertExtension(m_sizeType,
"Number"_L1);
79 Q_ASSERT(m_sizeType == m_int32Type || m_sizeType == m_int64Type);
81 m_boolType = builtinTypes.type(u"bool"_s).scope;
82 assertExtension(m_boolType,
"Boolean"_L1);
84 m_stringType = builtinTypes.type(u"QString"_s).scope;
85 assertExtension(m_stringType,
"String"_L1);
87 m_stringListType = builtinTypes.type(u"QStringList"_s).scope;
88 assertExtension(m_stringListType,
"Array"_L1);
90 m_byteArrayType = builtinTypes.type(u"QByteArray"_s).scope;
91 assertExtension(m_byteArrayType,
"ArrayBuffer"_L1);
93 m_urlType = builtinTypes.type(u"QUrl"_s).scope;
94 assertExtension(m_urlType,
"URL"_L1);
96 m_dateTimeType = builtinTypes.type(u"QDateTime"_s).scope;
97 assertExtension(m_dateTimeType,
"Date"_L1);
99 m_dateType = builtinTypes.type(u"QDate"_s).scope;
100 Q_ASSERT(m_dateType);
102 m_timeType = builtinTypes.type(u"QTime"_s).scope;
103 Q_ASSERT(m_timeType);
105 m_regexpType = builtinTypes.type(u"QRegularExpression"_s).scope;
106 Q_ASSERT(m_regexpType);
108 m_variantListType = builtinTypes.type(u"QVariantList"_s).scope;
109 assertExtension(m_variantListType,
"Array"_L1);
111 m_variantMapType = builtinTypes.type(u"QVariantMap"_s).scope;
112 Q_ASSERT(m_variantMapType);
113 m_varType = builtinTypes.type(u"QVariant"_s).scope;
116 m_qmlPropertyMapType = builtinTypes.type(u"QQmlPropertyMap"_s).scope;
117 Q_ASSERT(m_qmlPropertyMapType);
119 m_jsValueType = builtinTypes.type(u"QJSValue"_s).scope;
120 Q_ASSERT(m_jsValueType);
122 m_qObjectType = builtinTypes.type(u"QObject"_s).scope;
123 assertExtension(m_qObjectType,
"Object"_L1);
125 m_qObjectListType = builtinTypes.type(u"QObjectList"_s).scope;
126 assertExtension(m_qObjectListType,
"Array"_L1);
128 m_qQmlScriptStringType = builtinTypes.type(u"QQmlScriptString"_s).scope;
129 Q_ASSERT(m_qQmlScriptStringType);
131 m_functionType = builtinTypes.type(u"function"_s).scope;
132 Q_ASSERT(m_functionType);
134 m_numberPrototype = builtinTypes.type(u"NumberPrototype"_s).scope;
135 Q_ASSERT(m_numberPrototype);
137 m_arrayPrototype = builtinTypes.type(u"ArrayPrototype"_s).scope;
138 Q_ASSERT(m_arrayPrototype);
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);
146 QQmlJSScope::Ptr emptyType = QQmlJSScope::create();
147 emptyType->setAccessSemantics(QQmlJSScope::AccessSemantics::None);
148 m_emptyType = emptyType;
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;
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;
162 m_jsGlobalObject = importer->jsGlobalObject();
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;
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;
177 m_jsGlobalObjectContent = m_pool->createType(
178 m_jsGlobalObject, QQmlJSRegisterContent::InvalidLookupIndex,
179 QQmlJSRegisterContent::GlobalObject);
264QQmlJSTypeResolver::typeForBinaryOperation(QSOperator::Op oper, QQmlJSRegisterContent left,
265 QQmlJSRegisterContent right)
const
267 Q_ASSERT(left.isValid());
268 Q_ASSERT(right.isValid());
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());
295 const QQmlJSScope::ConstPtr result = merge(leftContents, rightContents);
296 if (result == boolType())
297 return operationType(int32Type());
298 if (isNumeric(result))
299 return operationType(realType());
301 return operationType(jsPrimitiveType());
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());
309 case QSOperator::Op::Div:
310 case QSOperator::Op::Mod:
311 return operationType(realType());
312 case QSOperator::Op::As:
313 return operationType(right.containedType());
318 return operationType(merge(left.containedType(), right.containedType()));
456QQmlJSRegisterContent QQmlJSTypeResolver::registerContentForName(
457 const QString &name, QQmlJSRegisterContent scopeType)
const
459 QQmlJSScope::ConstPtr type = typeForName(name);
461 return QQmlJSRegisterContent();
463 if (type->isSingleton()) {
464 return m_pool->createType(
465 type, QQmlJSRegisterContent::InvalidLookupIndex,
466 QQmlJSRegisterContent::Singleton, scopeType);
469 if (type->isScript()) {
470 return m_pool->createType(
471 type, QQmlJSRegisterContent::InvalidLookupIndex,
472 QQmlJSRegisterContent::Script, scopeType);
475 const QQmlJSRegisterContent namedType = m_pool->createType(
476 type, QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::TypeByName,
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());
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());
494 return m_pool->createType(
495 attached, QQmlJSRegisterContent::InvalidLookupIndex,
496 QQmlJSRegisterContent::Attachment, namedType);
500 switch (type->accessSemantics()) {
501 case QQmlJSScope::AccessSemantics::None:
502 case QQmlJSScope::AccessSemantics::Reference:
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);
522 return QQmlJSRegisterContent();
655QQmlJSRegisterContent QQmlJSTypeResolver::merge(
656 QQmlJSRegisterContent a, QQmlJSRegisterContent b)
const
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);
667 QSet<QQmlJSRegisterContent> origins;
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();
677 aResultScope = a.scope();
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();
688 bResultScope = b.scope();
691 const auto mergeScopes = [&](QQmlJSRegisterContent a, QQmlJSRegisterContent b) {
694 return (a == b) ? a : merge(a, b);
697 const QQmlJSRegisterContent merged = m_pool->createConversion(
699 merge(a.containedType(), b.containedType()),
700 mergeScopes(aResultScope, bResultScope),
701 mergeVariants(a.variant(), b.variant()),
702 mergeScopes(a.scope(), b.scope()));
704 m_mergeCache.insert(key, m_pool->clone(merged));
708QQmlJSScope::ConstPtr QQmlJSTypeResolver::merge(
const QQmlJSScope::ConstPtr &a,
709 const QQmlJSScope::ConstPtr &b)
const
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) {
723 case QQmlJSScope::NotExtension:
725 if (b->inherits(scope)) {
730 case QQmlJSScope::ExtensionJavaScript:
739 case QQmlJSScope::ExtensionType:
740 case QQmlJSScope::ExtensionNamespace:
751 if (a == jsValueType() || a == varType())
753 if (b == jsValueType() || b == varType())
756 const auto isInt32Compatible = [&](
const QQmlJSScope::ConstPtr &type) {
757 return (isIntegral(type)
758 && type != uint32Type()
759 && type != int64Type()
760 && type != uint64Type())
761 || type == boolType();
764 if (isInt32Compatible(a) && isInt32Compatible(b))
767 const auto isUInt32Compatible = [&](
const QQmlJSScope::ConstPtr &type) {
768 return (isUnsignedInteger(type) && type != uint64Type()) || type == boolType();
771 if (isUInt32Compatible(a) && isUInt32Compatible(b))
774 if (isNumeric(a) && isNumeric(b))
777 if (isPrimitive(a) && isPrimitive(b))
778 return jsPrimitiveType();
780 if (
const auto base = baseOrExtension(a, b))
783 if (
const auto base = baseOrExtension(b, a))
786 if ((a == nullType() || a == boolType()) && b->isReferenceType())
789 if ((b == nullType() || b == boolType()) && a->isReferenceType())
893QQmlJSScope::ConstPtr QQmlJSTypeResolver::genericType(
894 const QQmlJSScope::ConstPtr &type,
895 ComponentIsGeneric allowComponent)
const
897 if (type->isScript())
898 return m_jsValueType;
900 if (type == m_metaObjectType)
901 return m_metaObjectType;
903 if (type->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
904 QString unresolvedBaseTypeName;
905 for (
auto base = type; base;) {
911 if (base->internalName() == u"QObject"_s) {
913 }
else if (allowComponent == ComponentIsGeneric::Yes
914 && base->internalName() == u"QQmlComponent"_s) {
918 if (
auto baseBase = base->baseType()) {
921 unresolvedBaseTypeName = base->baseTypeName();
928 if (type->filePath().isEmpty())
929 return m_jsValueType;
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());
938 return m_jsValueType;
941 if (type->isListProperty())
942 return m_listPropertyType;
944 if (type->scopeType() == QQmlSA::ScopeType::EnumScope)
945 return type->baseType();
947 if (isPrimitive(type)) {
949 if (!type->filePath().isEmpty())
959 return m_jsPrimitiveType;
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())
1153QQmlJSScope::ConstPtr QQmlJSTypeResolver::scopedType(
1154 const QQmlJSScope::ConstPtr &scope,
const QString &name,
1155 QQmlJSScopesByIdOptions options)
const
1159 if (QQmlJSScope::ConstPtr result = containedTypeForName(name))
1162 QQmlJSScopesById::CertainCallback<QQmlJSScope::ConstPtr> identified;
1163 if (m_objectsById.possibleScopes(name, scope, options, identified)
1164 != QQmlJSScopesById::Success::Yes) {
1169 if (identified.result) {
1171 return identified.result;
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)
1181 if (found->hasOwnProperty(name)) {
1182 const QQmlJSMetaProperty prop = found->ownProperty(name);
1183 if (!isRevisionAllowed(prop.revision(), scope))
1186 result = resolveParentProperty(name, base, prop.type());
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();
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)
1214 if (found->hasOwnProperty(name)) {
1215 result = resolveParentProperty(name, root, found->ownProperty(name).type());
1218 if (found->hasOwnMethod(name)) {
1219 result = jsValueType();
1227 if (m_jsGlobalObject->hasProperty(name))
1228 return m_jsGlobalObject->property(name).type();
1230 if (m_jsGlobalObject->hasMethod(name))
1231 return jsValueType();
1243QQmlJSRegisterContent QQmlJSTypeResolver::scopedType(QQmlJSRegisterContent scope,
1244 const QString &name,
int lookupIndex,
1245 QQmlJSScopesByIdOptions options)
const
1247 const QQmlJSScope::ConstPtr contained = scope.containedType();
1251 if (QQmlJSRegisterContent result = registerContentForName(name, scope); result.isValid())
1254 QQmlJSScopesById::CertainCallback<QQmlJSScope::ConstPtr> identified;
1255 if (m_objectsById.possibleScopes(name, contained, options, identified)
1256 != QQmlJSScopesById::Success::Yes) {
1261 if (identified.result) {
1263 return m_pool->createType(
1264 identified.result, lookupIndex, QQmlJSRegisterContent::ObjectById, scope);
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)
1274 const QQmlJSRegisterContent resultScope = mode == QQmlJSScope::NotExtension
1276 : extensionType(found, scope);
1278 if (found->hasOwnProperty(name)) {
1279 QQmlJSMetaProperty prop = found->ownProperty(name);
1280 if (!isRevisionAllowed(prop.revision(), contained))
1283 prop.setType(resolveParentProperty(name, base, prop.type()));
1284 result = m_pool->createProperty(
1285 prop, QQmlJSRegisterContent::InvalidLookupIndex, lookupIndex,
1286 scopeContentVariant(mode,
false), resultScope);
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);
1298 if (methods.isEmpty())
1300 result = m_pool->createMethod(
1301 methods, jsValueType(), scopeContentVariant(mode,
true), resultScope);
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)
1324 const QQmlJSRegisterContent resultScope = mode == QQmlJSScope::NotExtension
1326 : extensionType(found, contextObject);
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);
1337 if (!found->hasOwnMethod(name))
1340 result = m_pool->createMethod(
1341 found->ownMethods(name), jsValueType(), scopeContentVariant(mode,
true),
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);
1422QQmlJSMetaMethod QQmlJSTypeResolver::selectConstructor(
1423 const QQmlJSScope::ConstPtr &type,
const QQmlJSScope::ConstPtr &passedArgumentType,
1424 bool *isExtension)
const
1429 || canHold(passedArgumentType, type)
1430 || type->accessSemantics() != QQmlJSScope::AccessSemantics::Value
1431 || !type->isCreatable()) {
1432 return QQmlJSMetaMethod();
1435 auto doSelectConstructor = [&](
const QQmlJSScope::ConstPtr &ctorProvider) {
1436 QQmlJSMetaMethod candidate;
1438 const auto ownMethods = ctorProvider->ownMethods();
1439 for (
const QQmlJSMetaMethod &method : ownMethods) {
1440 if (!method.isConstructor())
1443 const auto index = method.constructorIndex();
1444 Q_ASSERT(index != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
1446 const auto methodArguments = method.parameters();
1447 if (methodArguments.size() != 1)
1450 const QQmlJSScope::ConstPtr methodArgumentType = methodArguments[0].type();
1453 if (methodArgumentType == type)
1456 if (passedArgumentType == methodArgumentType)
1461 if (!candidate.isValid()
1462 && canPrimitivelyConvertFromTo(passedArgumentType, methodArgumentType)) {
1470 if (QQmlJSScope::ConstPtr extension = type->extensionType().scope) {
1471 const QQmlJSMetaMethod ctor = doSelectConstructor(extension);
1472 if (ctor.isValid()) {
1474 *isExtension =
true;
1480 *isExtension =
false;
1482 return doSelectConstructor(type);
1542bool QQmlJSTypeResolver::canPrimitivelyConvertFromTo(
1543 const QQmlJSScope::ConstPtr &from,
const QQmlJSScope::ConstPtr &to)
const
1547 if (from == m_varType || to == m_varType)
1549 if (from == m_jsValueType || to == m_jsValueType)
1551 if (to == m_qQmlScriptStringType)
1553 if (isNumeric(from) && isNumeric(to))
1556 if (to == m_boolType)
1559 if (from->accessSemantics() == QQmlJSScope::AccessSemantics::Reference
1560 && to == m_stringType) {
1565 if (isNumeric(from) && to == m_stringType)
1569 if (from == m_stringType && isNumeric(to))
1570 return to->scopeType() != QQmlJSScope::ScopeType::EnumScope;
1573 if ((from == m_stringType && to == m_urlType)
1574 || (from == m_urlType && to == m_stringType)) {
1579 if ((from == m_stringType && to == m_byteArrayType)
1580 || (from == m_byteArrayType && to == m_stringType)) {
1584 if (to == m_voidType)
1588 return from == m_voidType;
1590 const auto types = { m_dateTimeType, m_dateType, m_timeType, m_stringType };
1591 for (
const auto &originType : types) {
1592 if (from != originType)
1595 for (
const auto &targetType : types) {
1596 if (to == targetType)
1600 if (to == m_realType)
1606 if (from == m_nullType && to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
1609 if (from == m_jsPrimitiveType) {
1611 return isPrimitive(to) || to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference;
1614 if (to == m_jsPrimitiveType)
1615 return isPrimitive(from);
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) {
1622 case QQmlJSScope::NotExtension:
1623 case QQmlJSScope::ExtensionJavaScript:
1629 || (matchByName && scope->internalName() == to->internalName());
1630 case QQmlJSScope::ExtensionType:
1631 case QQmlJSScope::ExtensionNamespace:
1639 if (from == m_variantListType)
1640 return to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence;
1643 if (canPrimitivelyConvertFromTo(from, m_jsPrimitiveType)
1644 && canPrimitivelyConvertFromTo(m_jsPrimitiveType, to)) {
1648 if (areEquivalentLists(from, to))
1651 if (from->isListProperty()
1652 && to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
1653 && canConvertFromTo(from->elementType(), to->elementType())) {
1658 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
1659 && from->accessSemantics() == QQmlJSScope::AccessSemantics::Reference
1660 && from->inherits(to->elementType())) {
1664 if (to == m_stringType && from->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence)
1665 return canConvertFromTo(from->elementType(), m_stringType);
1697QQmlJSRegisterContent QQmlJSTypeResolver::memberType(
1698 QQmlJSRegisterContent type,
const QString &name,
int baseLookupIndex,
1699 int resultLookupIndex)
const
1701 QQmlJSRegisterContent result;
1702 const QQmlJSScope::ConstPtr contained = type.containedType();
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);
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);
1718 if (scope->hasOwnMethod(name)) {
1719 const auto methods = scope->ownMethods(name);
1720 result = m_pool->createMethod(
1721 methods, jsValueType(), QQmlJSRegisterContent::Method, resultScope);
1726 if (type.variant() == QQmlJSRegisterContent::ObjectById)
1728 return checkEnums(resultScope, name, &result);
1732 if (contained == metaObjectType())
1733 return memberEnumType(type.scope(), name);
1735 if (contained->inherits(qmlPropertyMapType())) {
1736 if (QQmlJSUtils::searchBaseAndExtensionTypes(type.containedType(), check))
1738 return createVariantProperty(type, name, baseLookupIndex, resultLookupIndex);
1741 if (contained == variantMapType())
1742 return createVariantProperty(type, name, baseLookupIndex, resultLookupIndex);
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);
1754 if ((contained == stringType()
1755 || contained->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence)
1756 && name == u"length"_s) {
1757 return lengthProperty(contained != stringType(), type);
1760 if (QQmlJSUtils::searchBaseAndExtensionTypes(type.containedType(), check))
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));
1774 return m_pool->createProperty(
1775 prop, baseLookupIndex, resultLookupIndex,
1776 QQmlJSRegisterContent::Property,
1777 parentScope(scope, type));
1782 if (type.isType()) {
1783 if (
auto result = memberEnumType(type.scope(), name); result.isValid())
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());
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());
1800 const QQmlJSRegisterContent namedType = m_pool->createType(
1801 attachedBase, QQmlJSRegisterContent::InvalidLookupIndex,
1802 QQmlJSRegisterContent::TypeByName, type);
1804 return m_pool->createType(
1805 attached, resultLookupIndex, QQmlJSRegisterContent::Attachment,
1833QQmlJSRegisterContent QQmlJSTypeResolver::memberType(
1834 QQmlJSRegisterContent type,
const QString &name,
int lookupIndex)
const
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))
1844 return m_pool->createEnumeration(
1845 enumeration, name, QQmlJSRegisterContent::Enum, type.scope());
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);
1857 if (type.isImportNamespace()) {
1858 Q_ASSERT(type.scopeType()->isReferenceType());
1859 return registerContentForName(name, type);
1861 if (type.isConversion()) {
1862 if (
const auto result = memberType(
1863 type, name, type.resultLookupIndex(), lookupIndex);
1868 if (
const auto result = memberEnumType(type.scope(), name); result.isValid())
1875 const auto nonVoid = extractNonVoidFromOptionalType(type);
1878 return (!nonVoid.isNull() && canHold(type.conversionResultType(), nonVoid.containedType()))
1879 ? memberType(nonVoid, name, type.resultLookupIndex(), lookupIndex)
1880 : QQmlJSRegisterContent();
1883 Q_UNREACHABLE_RETURN({});