286void QQmlJSTypePropagator::generate_LoadQmlContextPropertyLookup(
int index)
291 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
292 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
294 setAccumulator(m_typeResolver->scopedType(m_function->qmlScope, name, index));
296 if (!m_state.accumulatorOut().isValid() && m_typeResolver->isPrefix(name)) {
297 setAccumulator(m_pool->createImportNamespace(
298 nameIndex, m_typeResolver->voidType(), QQmlJSRegisterContent::ModulePrefix,
299 m_function->qmlScope));
303 checkDeprecated(m_function->qmlScope.containedType(), name,
false);
305 const QQmlJSRegisterContent accumulatorOut = m_state.accumulatorOut();
307 if (!accumulatorOut.isValid()) {
308 addError(u"Cannot access value for name "_s + name);
309 handleUnqualifiedAccessAndContextProperties(name,
false);
310 setVarAccumulatorAndError();
314 if (isQmlContextObject(accumulatorOut.scope()))
315 handleUnqualifiedAccessAndContextProperties(name,
false);
317 const QQmlJSScope::ConstPtr retrieved
318 = m_typeResolver->genericType(accumulatorOut.containedType());
320 if (retrieved.isNull()) {
323 addError(u"Cannot determine generic type for "_s + name);
327 if (accumulatorOut.variant() == QQmlJSRegisterContent::ObjectById
328 && !retrieved->isReferenceType()) {
329 addError(u"Cannot retrieve a non-object type by ID: "_s + name);
345void QQmlJSTypePropagator::generate_StoreNameCommon(
int nameIndex)
347 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
348 const QQmlJSRegisterContent type = m_typeResolver->scopedType(m_function->qmlScope, name);
349 const QQmlJSRegisterContent in = m_state.accumulatorIn();
351 if (!type.isValid()) {
352 handleUnqualifiedAccess(name,
false);
353 addError(u"Cannot find name "_s + name);
357 if (isQmlContextObject(type.scope()))
358 handleUnqualifiedAccess(name,
false);
360 if (!type.isProperty()) {
361 QString message = type.isMethod() ? u"Cannot assign to method %1"_s
362 : u"Cannot assign to non-property %1"_s;
365 m_logger->log(message.arg(name), qmlReadOnlyProperty,
366 currentSourceLocation());
367 addError(u"Cannot assign to non-property "_s + name);
371 if (!type.isWritable() && !type.isList()) {
372 addError(u"Can't assign to read-only property %1"_s.arg(name));
374 m_logger->log(u"Cannot assign to read-only property %1"_s.arg(name), qmlReadOnlyProperty,
375 currentSourceLocation());
380 if (!canConvertFromTo(in, type)) {
381 addError(u"cannot convert from %1 to %2"_s
382 .arg(in.descriptiveName(), type.descriptiveName()));
385 if (m_typeResolver->canHoldUndefined(in) && !m_typeResolver->canHoldUndefined(type)) {
386 if (in.contains(m_typeResolver->voidType()))
387 addReadAccumulator(m_typeResolver->varType());
389 addReadAccumulator();
391 addReadAccumulator(type);
394 m_state.setHasExternalSideEffects();
422void QQmlJSTypePropagator::generate_LoadElement(
int base)
424 const QQmlJSRegisterContent in = m_state.accumulatorIn();
425 const QQmlJSRegisterContent baseRegister = m_state.registers[base].content;
427 const auto fallback = [&]() {
428 const QQmlJSScope::ConstPtr jsValue = m_typeResolver->jsValueType();
430 addReadAccumulator(jsValue);
431 addReadRegister(base, jsValue);
433 QQmlJSMetaProperty property;
434 property.setPropertyName(u"[]"_s);
435 property.setTypeName(jsValue->internalName());
436 property.setType(jsValue);
438 setAccumulator(m_pool->createProperty(
439 property, QQmlJSRegisterContent::InvalidLookupIndex,
440 QQmlJSRegisterContent::InvalidLookupIndex, QQmlJSRegisterContent::ListValue,
441 m_typeResolver->convert(m_typeResolver->elementType(baseRegister), jsValue)));
444 if (baseRegister.isList()) {
445 addReadRegister(base, m_typeResolver->arrayPrototype());
446 }
else if (baseRegister.contains(m_typeResolver->stringType())) {
447 addReadRegister(base, m_typeResolver->stringType());
453 if (m_typeResolver->isNumeric(in)) {
454 const auto contained = in.containedType();
455 if (m_typeResolver->isSignedInteger(contained))
456 addReadAccumulator(m_typeResolver->sizeType());
457 else if (m_typeResolver->isUnsignedInteger(contained))
458 addReadAccumulator(m_typeResolver->uint32Type());
460 addReadAccumulator(m_typeResolver->realType());
461 }
else if (m_typeResolver->isNumeric(m_typeResolver->extractNonVoidFromOptionalType(in))) {
462 addReadAccumulator();
469 setAccumulator(m_typeResolver->merge(
470 m_typeResolver->elementType(baseRegister),
471 m_typeResolver->literalType(m_typeResolver->voidType())));
547void QQmlJSTypePropagator::propagatePropertyLookup(
const QString &propertyName,
int lookupIndex)
549 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
551 m_typeResolver->memberType(
553 accumulatorIn.isImportNamespace()
554 ? m_jsUnitGenerator->stringForIndex(accumulatorIn.importNamespace())
555 + u'.' + propertyName
556 : propertyName, lookupIndex));
558 if (!m_state.accumulatorOut().isValid() && handleImportNamespaceLookup(propertyName))
561 if (m_state.accumulatorOut().variant() == QQmlJSRegisterContent::Singleton
562 && accumulatorIn.variant() == QQmlJSRegisterContent::ModulePrefix
563 && !isQmlScopeObject(accumulatorIn.scope())) {
565 u"Cannot access singleton as a property of an object. Did you want to access an attached object?"_s,
566 qmlAccessSingleton, currentSourceLocation());
567 setAccumulator(QQmlJSRegisterContent());
568 }
else if (m_state.accumulatorOut().isEnumeration()) {
569 switch (accumulatorIn.variant()) {
570 case QQmlJSRegisterContent::MetaType:
571 case QQmlJSRegisterContent::Attachment:
572 case QQmlJSRegisterContent::Enum:
573 case QQmlJSRegisterContent::ModulePrefix:
574 case QQmlJSRegisterContent::Singleton:
577 setAccumulator(QQmlJSRegisterContent());
581 if (m_state.instructionHasError || !m_state.accumulatorOut().isValid()) {
582 handleLookupError(propertyName);
586 if (m_state.accumulatorOut().isMethod() && m_state.accumulatorOut().method().size() != 1) {
587 addError(u"Cannot determine overloaded method on loadProperty"_s);
591 if (m_state.accumulatorOut().isProperty()) {
592 const QQmlJSScope::ConstPtr mathObject
593 = m_typeResolver->jsGlobalObject()->property(u"Math"_s).type();
594 if (accumulatorIn.contains(mathObject)) {
595 QQmlJSMetaProperty prop;
596 prop.setPropertyName(propertyName);
597 prop.setTypeName(u"double"_s);
598 prop.setType(m_typeResolver->realType());
600 m_pool->createProperty(
601 prop, accumulatorIn.resultLookupIndex(), lookupIndex,
603 QQmlJSRegisterContent::Property, m_state.accumulatorOut().scope())
609 if (m_state.accumulatorOut().contains(m_typeResolver->voidType())) {
610 addError(u"Type %1 does not have a property %2 for reading"_s
611 .arg(accumulatorIn.descriptiveName(), propertyName));
615 if (!m_state.accumulatorOut().property().type()) {
617 QString::fromLatin1(
"Type of property \"%2\" not found").arg(propertyName),
618 qmlMissingType, currentSourceLocation());
622 switch (m_state.accumulatorOut().variant()) {
623 case QQmlJSRegisterContent::Enum:
624 case QQmlJSRegisterContent::Singleton:
627 if (accumulatorIn.isImportNamespace())
628 addReadAccumulator();
631 addReadAccumulator();
660void QQmlJSTypePropagator::generate_StoreProperty(
int nameIndex,
int base)
662 auto callBase = m_state.registers[base].content;
663 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
665 QQmlJSRegisterContent property = m_typeResolver->memberType(callBase, propertyName);
666 if (!property.isProperty()) {
667 QString name = callBase.descriptiveName();
670 addError(u"Type %1does not have a property %2 for writing"_s.arg(name, propertyName));
674 if (property.containedType().isNull()) {
675 addError(u"Cannot determine type for property %1 of type %2"_s.arg(
676 propertyName, callBase.descriptiveName()));
680 if (!property.isWritable() && !property.containedType()->isListProperty()) {
681 addError(u"Can't assign to read-only property %1"_s.arg(propertyName));
683 m_logger->log(u"Cannot assign to read-only property %1"_s.arg(propertyName),
684 qmlReadOnlyProperty, currentSourceLocation());
689 if (!canConvertFromTo(m_state.accumulatorIn(), property)) {
690 addError(u"cannot convert from %1 to %2"_s
691 .arg(m_state.accumulatorIn().descriptiveName(), property.descriptiveName()));
704 const QQmlJSScope::ConstPtr varType = m_typeResolver->varType();
705 const QQmlJSRegisterContent readType = m_typeResolver->canHoldUndefined(m_state.accumulatorIn())
706 ? m_typeResolver->convert(property, varType)
707 : std::move(property);
708 addReadAccumulator(readType);
709 addReadRegister(base);
710 m_state.setHasExternalSideEffects();
805void QQmlJSTypePropagator::generate_CallProperty_SCconsole(
806 const QString &name,
int base,
int argc,
int argv)
809 addReadRegister(base, m_typeResolver->voidType());
812 const QQmlJSRegisterContent firstContent = m_state.registers[argv].content;
813 const QQmlJSScope::ConstPtr firstArg = firstContent.containedType();
814 switch (firstArg->accessSemantics()) {
815 case QQmlJSScope::AccessSemantics::Reference:
818 addReadRegister(argv, m_typeResolver->genericType(firstArg));
820 case QQmlJSScope::AccessSemantics::Sequence:
821 addReadRegister(argv);
824 addReadRegister(argv, m_typeResolver->stringType());
829 for (
int i = 1; i < argc; ++i) {
830 const QQmlJSRegisterContent argContent = m_state.registers[argv + i].content;
831 const QQmlJSScope::ConstPtr arg = argContent.containedType();
832 if (arg->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence)
833 addReadRegister(argv + i);
835 addReadRegister(argv + i, m_typeResolver->stringType());
843 m_state.setHasExternalSideEffects();
845 QQmlJSRegisterContent console = m_state.registers[base].content;
846 QList<QQmlJSMetaMethod> methods = console.containedType()->ownMethods(name);
847 Q_ASSERT(methods.length() == 1);
851 setAccumulator(m_typeResolver->returnType(
852 methods[0], m_typeResolver->voidType(),
853 m_typeResolver->baseType(console.containedType(), console)));
856void QQmlJSTypePropagator::generate_CallProperty(
int nameIndex,
int base,
int argc,
int argv)
858 Q_ASSERT(m_state.registers.contains(base));
859 const auto callBase = m_state.registers[base].content;
860 const QString propertyName = m_jsUnitGenerator->stringForIndex(nameIndex);
862 if (callBase.contains(m_typeResolver->mathObject())) {
863 generate_CallProperty_SCMath(propertyName, base, argc, argv);
867 if (callBase.contains(m_typeResolver->consoleObject()) && isLoggingMethod(propertyName)) {
868 generate_CallProperty_SCconsole(propertyName, base, argc, argv);
872 const auto baseType = callBase.containedType();
873 const auto member = m_typeResolver->memberType(callBase, propertyName);
875 if (!member.isMethod()) {
876 if (callBase.contains(m_typeResolver->jsValueType())
877 || callBase.contains(m_typeResolver->varType())) {
878 const auto jsValueType = m_typeResolver->jsValueType();
879 addReadRegister(base, jsValueType);
880 for (
int i = 0; i < argc; ++i)
881 addReadRegister(argv + i, jsValueType);
882 m_state.setHasExternalSideEffects();
884 QQmlJSMetaMethod method;
885 method.setIsJavaScriptFunction(
true);
886 method.setMethodName(propertyName);
887 method.setMethodType(QQmlJSMetaMethod::MethodType::Method);
889 setAccumulator(m_typeResolver->returnType(
890 method, m_typeResolver->jsValueType(), callBase));
894 setVarAccumulatorAndError();
895 addError(u"Type %1 does not have a property %2 for calling"_s
896 .arg(callBase.descriptiveName(), propertyName));
898 if (callBase.isType() && isCallingProperty(callBase.type(), propertyName))
901 if (checkForEnumProblems(callBase, propertyName))
904 std::optional<QQmlJSFixSuggestion> fixSuggestion;
906 if (
auto suggestion = QQmlJSUtils::didYouMean(propertyName, baseType->methods().keys(),
907 m_logger->filePath(), currentSourceLocation());
908 suggestion.has_value()) {
909 fixSuggestion = suggestion;
912 if (baseType->isFullyResolved() || baseType->isScript()) {
913 m_logger->log(u"Member \"%1\" not found on type \"%2\""_s.arg(
914 propertyName, callBase.containedTypeName()),
915 qmlMissingProperty, currentSourceLocation(),
true,
true, fixSuggestion);
920 checkDeprecated(baseType, propertyName,
true);
922 addReadRegister(base);
924 if (callBase.contains(m_typeResolver->stringType())) {
925 if (propertyName == u"arg"_s && argc == 1) {
926 propagateStringArgCall(callBase, argv);
931 if (baseType->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
932 && member.scope().contains(m_typeResolver->arrayPrototype())
933 && propagateArrayMethod(propertyName, argc, argv, callBase)) {
937 propagateCall(member.method(), argc, argv, member.scope());
940QQmlJSMetaMethod QQmlJSTypePropagator::bestMatchForCall(
const QList<QQmlJSMetaMethod> &methods,
941 int argc,
int argv, QStringList *errors)
943 QQmlJSMetaMethod javascriptFunction;
944 QQmlJSMetaMethod candidate;
945 bool hasMultipleCandidates =
false;
947 for (
const auto &method : methods) {
950 if (method.isJavaScriptFunction() && !javascriptFunction.isValid())
951 javascriptFunction = method;
953 if (method.returnType().isNull() && !method.returnTypeName().isEmpty()) {
954 errors->append(u"return type %1 cannot be resolved"_s
955 .arg(method.returnTypeName()));
959 const auto arguments = method.parameters();
960 if (argc != arguments.size()) {
961 errors->append(u"Function expects %1 arguments, but %2 were provided"_s
962 .arg(QString::number(arguments.size()), QString::number(argc)));
966 bool fuzzyMatch =
true;
967 bool exactMatch =
true;
968 for (
int i = 0; i < argc; ++i) {
969 const auto argumentType = arguments[i].type();
970 if (argumentType.isNull()) {
971 errors->append(u"type %1 for argument %2 cannot be resolved"_s
972 .arg(arguments[i].typeName(), QString::number(i)));
978 const auto content = m_state.registers[argv + i].content;
979 if (content.contains(argumentType))
983 if (canConvertFromTo(content, argumentType))
987 if (argumentType->isReferenceType()
988 && m_typeResolver->inherits(
989 argumentType->baseType(), content.containedType())) {
993 errors->append(u"argument %1 contains %2 but is expected to contain the type %3"_s
994 .arg(QString::number(i), content.descriptiveName(),
995 arguments[i].typeName()));
1002 }
else if (fuzzyMatch) {
1003 if (!candidate.isValid())
1006 hasMultipleCandidates =
true;
1010 if (hasMultipleCandidates)
1011 return QQmlJSMetaMethod();
1013 return candidate.isValid() ? candidate : javascriptFunction;
1056void QQmlJSTypePropagator::mergeRegister(
1057 int index,
const VirtualRegister &a,
const VirtualRegister &b)
1059 const VirtualRegister merged = {
1060 (a.content == b.content) ? a.content : m_typeResolver->merge(a.content, b.content),
1061 a.canMove && b.canMove,
1062 a.affectedBySideEffects || b.affectedBySideEffects,
1063 a.isShadowable || b.isShadowable,
1066 Q_ASSERT(merged.content.isValid());
1068 if (!merged.content.isConversion()) {
1070 m_state.annotations[currentInstructionOffset()].typeConversions[index] = merged;
1071 m_state.registers[index] = merged;
1075 auto tryPrevStateConversion = [
this](
int index,
const VirtualRegister &merged) ->
bool {
1076 auto it = m_prevStateAnnotations.find(currentInstructionOffset());
1077 if (it == m_prevStateAnnotations.end())
1080 auto conversion = it->second.typeConversions.find(index);
1081 if (conversion == it->second.typeConversions.end())
1084 const VirtualRegister &lastTry = conversion.value();
1086 Q_ASSERT(lastTry.content.isValid());
1087 if (!lastTry.content.isConversion())
1090 if (lastTry.content.conversionResultType() != merged.content.conversionResultType()
1091 || lastTry.content.conversionOrigins() != merged.content.conversionOrigins()
1092 || lastTry.canMove != merged.canMove
1093 || lastTry.affectedBySideEffects != merged.affectedBySideEffects
1094 || lastTry.isShadowable != merged.isShadowable) {
1099 m_state.annotations[currentInstructionOffset()].typeConversions[index] = lastTry;
1102 Q_ASSERT(!m_state.registers[index].affectedBySideEffects || lastTry.affectedBySideEffects);
1104 m_state.registers[index] = lastTry;
1108 if (!tryPrevStateConversion(index, merged)) {
1110 const VirtualRegister cloned = {
1111 (a == b) ? m_pool->clone(merged.content) : merged.content,
1113 merged.affectedBySideEffects,
1114 merged.isShadowable,
1116 Q_ASSERT(cloned.content.isValid());
1117 m_state.annotations[currentInstructionOffset()].typeConversions[index] = cloned;
1118 m_state.registers[index] = cloned;
1145void QQmlJSTypePropagator::propagateCall(
1146 const QList<QQmlJSMetaMethod> &methods,
int argc,
int argv,
1147 QQmlJSRegisterContent scope)
1150 const QQmlJSMetaMethod match = bestMatchForCall(methods, argc, argv, &errors);
1152 if (!match.isValid()) {
1153 setVarAccumulatorAndError();
1154 if (methods.size() == 1) {
1156 Q_ASSERT(errors.size() == 1);
1157 addError(errors.first());
1158 }
else if (errors.size() < methods.size()) {
1159 addError(u"Multiple matching overrides found. Cannot determine the right one."_s);
1161 addError(u"No matching override found. Candidates:\n"_s + errors.join(u'\n'));
1166 QQmlJSScope::ConstPtr returnType;
1167 if (match.isJavaScriptFunction())
1168 returnType = m_typeResolver->jsValueType();
1169 else if (match.isConstructor())
1170 returnType = scope.containedType();
1172 returnType = match.returnType();
1174 setAccumulator(m_typeResolver->returnType(match, returnType, scope));
1175 if (!m_state.accumulatorOut().isValid())
1176 addError(u"Cannot store return type of method %1()."_s.arg(match.methodName()));
1178 const auto types = match.parameters();
1179 for (
int i = 0; i < argc; ++i) {
1180 if (i < types.size()) {
1181 const QQmlJSScope::ConstPtr type = match.isJavaScriptFunction()
1182 ? m_typeResolver->jsValueType()
1183 : QQmlJSScope::ConstPtr(types.at(i).type());
1184 if (!type.isNull()) {
1185 addReadRegister(argv + i, type);
1189 addReadRegister(argv + i, m_typeResolver->jsValueType());
1191 m_state.setHasExternalSideEffects();
1199bool QQmlJSTypePropagator::propagateTranslationMethod(
1200 const QList<QQmlJSMetaMethod> &methods,
int argc,
int argv)
1202 if (methods.size() != 1)
1205 const QQmlJSMetaMethod method = methods.front();
1206 const QQmlJSScope::ConstPtr intType = m_typeResolver->int32Type();
1207 const QQmlJSScope::ConstPtr stringType = m_typeResolver->stringType();
1209 const QQmlJSRegisterContent returnType = m_typeResolver->returnType(
1210 method, m_typeResolver->stringType(), m_typeResolver->jsGlobalObjectContent());
1212 if (method.methodName() == u"qsTranslate"_s) {
1215 addReadRegister(argv + 3, intType);
1218 addReadRegister(argv + 2, stringType);
1221 addReadRegister(argv + 1, stringType);
1222 addReadRegister(argv, stringType);
1223 setAccumulator(returnType);
1224 propagateTranslationMethod_SAcheck(method.methodName());
1231 if (method.methodName() == u"QT_TRANSLATE_NOOP"_s) {
1234 addReadRegister(argv + 2, stringType);
1237 addReadRegister(argv + 1, stringType);
1238 addReadRegister(argv, stringType);
1239 setAccumulator(returnType);
1240 propagateTranslationMethod_SAcheck(method.methodName());
1247 if (method.methodName() == u"qsTr"_s) {
1250 addReadRegister(argv + 2, intType);
1253 addReadRegister(argv + 1, stringType);
1256 addReadRegister(argv, stringType);
1257 setAccumulator(returnType);
1258 propagateTranslationMethod_SAcheck(method.methodName());
1265 if (method.methodName() == u"QT_TR_NOOP"_s) {
1268 addReadRegister(argv + 1, stringType);
1271 addReadRegister(argv, stringType);
1272 setAccumulator(returnType);
1273 propagateTranslationMethod_SAcheck(method.methodName());
1280 if (method.methodName() == u"qsTrId"_s) {
1283 addReadRegister(argv + 1, intType);
1286 addReadRegister(argv, stringType);
1287 setAccumulator(returnType);
1288 propagateTranslationMethod_SAcheck(method.methodName());
1295 if (method.methodName() == u"QT_TRID_NOOP"_s) {
1298 addReadRegister(argv, stringType);
1299 setAccumulator(returnType);
1300 propagateTranslationMethod_SAcheck(method.methodName());
1345bool QQmlJSTypePropagator::propagateArrayMethod(
1346 const QString &name,
int argc,
int argv, QQmlJSRegisterContent baseType)
1361 const auto intType = m_typeResolver->int32Type();
1362 const auto stringType = m_typeResolver->stringType();
1363 const auto baseContained = baseType.containedType();
1364 const auto elementContained = baseContained->elementType();
1366 const auto setReturnType = [&](
const QQmlJSScope::ConstPtr type) {
1367 QQmlJSMetaMethod method;
1368 method.setIsJavaScriptFunction(
true);
1369 method.setMethodName(name);
1370 setAccumulator(m_typeResolver->returnType(method, type, baseType));
1373 if (name == u"copyWithin" && argc > 0 && argc < 4) {
1374 for (
int i = 0; i < argc; ++i) {
1375 if (!canConvertFromTo(m_state.registers[argv + i].content, intType))
1379 for (
int i = 0; i < argc; ++i)
1380 addReadRegister(argv + i, intType);
1382 m_state.setHasExternalSideEffects();
1383 setReturnType(baseContained);
1387 if (name == u"fill" && argc > 0 && argc < 4) {
1388 if (!canConvertFromTo(m_state.registers[argv].content, elementContained))
1391 for (
int i = 1; i < argc; ++i) {
1392 if (!canConvertFromTo(m_state.registers[argv + i].content, intType))
1396 addReadRegister(argv, elementContained);
1398 for (
int i = 1; i < argc; ++i)
1399 addReadRegister(argv + i, intType);
1401 m_state.setHasExternalSideEffects();
1402 setReturnType(baseContained);
1406 if (name == u"includes" && argc > 0 && argc < 3) {
1407 if (!canConvertFromTo(m_state.registers[argv].content, elementContained))
1411 if (!canConvertFromTo(m_state.registers[argv + 1].content, intType))
1413 addReadRegister(argv + 1, intType);
1416 addReadRegister(argv, elementContained);
1417 setReturnType(m_typeResolver->boolType());
1421 if (name == u"toString" || (name == u"join" && argc < 2)) {
1423 if (!canConvertFromTo(m_state.registers[argv].content, stringType))
1425 addReadRegister(argv, stringType);
1428 setReturnType(m_typeResolver->stringType());
1432 if ((name == u"pop" || name == u"shift") && argc == 0) {
1433 m_state.setHasExternalSideEffects();
1434 setReturnType(elementContained);
1438 if (name == u"push" || name == u"unshift") {
1439 for (
int i = 0; i < argc; ++i) {
1440 if (!canConvertFromTo(m_state.registers[argv + i].content, elementContained))
1444 for (
int i = 0; i < argc; ++i)
1445 addReadRegister(argv + i, elementContained);
1447 m_state.setHasExternalSideEffects();
1448 setReturnType(m_typeResolver->int32Type());
1452 if (name == u"reverse" && argc == 0) {
1453 m_state.setHasExternalSideEffects();
1454 setReturnType(baseContained);
1458 if (name == u"slice" && argc < 3) {
1459 for (
int i = 0; i < argc; ++i) {
1460 if (!canConvertFromTo(m_state.registers[argv + i].content, intType))
1464 for (
int i = 0; i < argc; ++i)
1465 addReadRegister(argv + i, intType);
1467 setReturnType(baseType.containedType()->isListProperty()
1468 ? m_typeResolver->qObjectListType()
1473 if (name == u"splice" && argc > 0) {
1474 const int startAndDeleteCount = std::min(argc, 2);
1475 for (
int i = 0; i < startAndDeleteCount; ++i) {
1476 if (!canConvertFromTo(m_state.registers[argv + i].content, intType))
1480 for (
int i = 2; i < argc; ++i) {
1481 if (!canConvertFromTo(m_state.registers[argv + i].content, elementContained))
1485 for (
int i = 0; i < startAndDeleteCount; ++i)
1486 addReadRegister(argv + i, intType);
1488 for (
int i = 2; i < argc; ++i)
1489 addReadRegister(argv + i, elementContained);
1491 m_state.setHasExternalSideEffects();
1492 setReturnType(baseContained);
1496 if ((name == u"indexOf" || name == u"lastIndexOf") && argc > 0 && argc < 3) {
1497 if (!canConvertFromTo(m_state.registers[argv].content, elementContained))
1501 if (!canConvertFromTo(m_state.registers[argv + 1].content, intType))
1503 addReadRegister(argv + 1, intType);
1506 addReadRegister(argv, elementContained);
1507 setReturnType(m_typeResolver->int32Type());
1637void QQmlJSTypePropagator::generate_Construct(
int func,
int argc,
int argv)
1639 const QQmlJSRegisterContent type = m_state.registers[func].content;
1640 if (type.contains(m_typeResolver->metaObjectType())) {
1641 const QQmlJSRegisterContent valueType = type.scope();
1642 const QQmlJSScope::ConstPtr contained = type.scopeType();
1643 if (contained->isValueType() && contained->isCreatable()) {
1644 const auto extension = contained->extensionType();
1645 if (extension.extensionSpecifier == QQmlJSScope::ExtensionType) {
1647 extension.scope->ownMethods(extension.scope->internalName()),
1648 argc, argv, valueType);
1651 contained->ownMethods(contained->internalName()), argc, argv, valueType);
1657 if (!type.isMethod()) {
1658 m_state.setHasExternalSideEffects();
1659 QQmlJSMetaMethod method;
1660 method.setMethodName(type.containedTypeName());
1661 method.setIsJavaScriptFunction(
true);
1662 method.setIsConstructor(
true);
1663 setAccumulator(m_typeResolver->returnType(method, m_typeResolver->jsValueType(), {}));
1667 if (
const auto methods = type.method();
1668 methods == m_typeResolver->jsGlobalObject()->methods(u"Date"_s)) {
1669 Q_ASSERT(methods.length() == 1);
1670 generate_Construct_SCDate(methods[0], argc, argv);
1674 if (
const auto methods = type.method();
1675 methods == m_typeResolver->jsGlobalObject()->methods(u"Array"_s)) {
1676 Q_ASSERT(methods.length() == 1);
1677 generate_Construct_SCArray(methods[0], argc, argv);
1681 m_state.setHasExternalSideEffects();
1684 QQmlJSMetaMethod match = bestMatchForCall(type.method(), argc, argv, &errors);
1685 if (!match.isValid())
1686 addError(u"Cannot determine matching constructor. Candidates:\n"_s + errors.join(u'\n'));
1687 setAccumulator(m_typeResolver->returnType(match, m_typeResolver->jsValueType(), {}));
2038void QQmlJSTypePropagator::recordEqualsType(
int lhs)
2040 const auto isNumericOrEnum = [
this](QQmlJSRegisterContent content) {
2041 return content.isEnumeration() || m_typeResolver->isNumeric(content);
2044 const auto accumulatorIn = m_state.accumulatorIn();
2045 const auto lhsRegister = m_state.registers[lhs].content;
2048 if (m_typeResolver->isPrimitive(accumulatorIn) || accumulatorIn.isEnumeration()) {
2049 if (accumulatorIn.contains(lhsRegister.containedType())
2050 || (isNumericOrEnum(accumulatorIn) && isNumericOrEnum(lhsRegister))
2051 || m_typeResolver->isPrimitive(lhsRegister)) {
2052 addReadRegister(lhs);
2053 addReadAccumulator();
2058 const auto containedAccumulatorIn = m_typeResolver->isOptionalType(accumulatorIn)
2059 ? m_typeResolver->extractNonVoidFromOptionalType(accumulatorIn).containedType()
2060 : accumulatorIn.containedType();
2062 const auto containedLhs = m_typeResolver->isOptionalType(lhsRegister)
2063 ? m_typeResolver->extractNonVoidFromOptionalType(lhsRegister).containedType()
2064 : lhsRegister.containedType();
2067 if (QQmlJSUtils::canStrictlyCompareWithVar(m_typeResolver, containedLhs, containedAccumulatorIn)
2068 || QQmlJSUtils::canCompareWithQObject(m_typeResolver, containedLhs, containedAccumulatorIn)
2069 || QQmlJSUtils::canCompareWithQUrl(m_typeResolver, containedLhs, containedAccumulatorIn)) {
2070 addReadRegister(lhs);
2071 addReadAccumulator();
2078 const QQmlJSScope::ConstPtr jsval = m_typeResolver->jsValueType();
2079 addReadRegister(lhs, jsval);
2080 addReadAccumulator(jsval);
2211void QQmlJSTypePropagator::generate_As(
int lhs)
2213 const QQmlJSRegisterContent input = checkedInputRegister(lhs);
2214 const QQmlJSScope::ConstPtr inContained = input.containedType();
2216 QQmlJSRegisterContent output;
2218 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
2219 switch (accumulatorIn.variant()) {
2220 case QQmlJSRegisterContent::Attachment:
2221 output = accumulatorIn.scope();
2223 case QQmlJSRegisterContent::MetaType:
2224 output = accumulatorIn.scope();
2225 if (output.containedType()->isComposite())
2226 addReadAccumulator(m_typeResolver->metaObjectType());
2229 output = accumulatorIn;
2233 QQmlJSScope::ConstPtr outContained = output.containedType();
2235 if (outContained->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
2239 if (m_typeResolver->inherits(inContained, outContained))
2240 output = m_pool->clone(input);
2242 output = m_pool->castTo(input, outContained);
2243 }
else if (m_typeResolver->inherits(inContained, outContained)) {
2245 output = m_pool->castTo(input, outContained);
2249 output = m_typeResolver->merge(
2250 m_pool->castTo(input, outContained),
2251 m_pool->castTo(input, m_typeResolver->voidType()));
2254 addReadRegister(lhs);
2255 setAccumulator(output);
2443QQmlJSTypePropagator::startInstruction(QV4::Moth::Instr::Type type)
2445 if (m_state.jumpTargets.contains(currentInstructionOffset())) {
2446 if (m_state.skipInstructionsUntilNextJumpTarget) {
2448 m_state.registers.clear();
2449 m_state.skipInstructionsUntilNextJumpTarget =
false;
2451 }
else if (m_state.skipInstructionsUntilNextJumpTarget
2452 && !instructionManipulatesContext(type)) {
2453 return SkipInstruction;
2456 const int currentOffset = currentInstructionOffset();
2468 for (
auto originRegisterStateIt =
2469 m_jumpOriginRegisterStateByTargetInstructionOffset.constFind(currentOffset);
2470 originRegisterStateIt != m_jumpOriginRegisterStateByTargetInstructionOffset.constEnd()
2471 && originRegisterStateIt.key() == currentOffset;
2472 ++originRegisterStateIt) {
2473 auto stateToMerge = *originRegisterStateIt;
2474 for (
auto registerIt = stateToMerge.registers.constBegin(),
2475 end = stateToMerge.registers.constEnd();
2476 registerIt != end; ++registerIt) {
2477 const int registerIndex = registerIt.key();
2479 const VirtualRegister &newType = registerIt.value();
2480 if (!newType.content.isValid()) {
2481 addError(u"When reached from offset %1, %2 is undefined"_s
2482 .arg(QString::number(stateToMerge.originatingOffset),
2483 registerName(registerIndex)));
2484 return SkipInstruction;
2487 auto currentRegister = m_state.registers.find(registerIndex);
2488 if (currentRegister != m_state.registers.end())
2489 mergeRegister(registerIndex, newType, currentRegister.value());
2491 mergeRegister(registerIndex, newType, newType);
2495 return ProcessInstruction;
2498bool QQmlJSTypePropagator::populatesAccumulator(QV4::Moth::Instr::Type instr)
const
2501 case QV4::Moth::Instr::Type::CheckException:
2502 case QV4::Moth::Instr::Type::CloneBlockContext:
2503 case QV4::Moth::Instr::Type::ConvertThisToObject:
2504 case QV4::Moth::Instr::Type::CreateCallContext:
2505 case QV4::Moth::Instr::Type::DeadTemporalZoneCheck:
2506 case QV4::Moth::Instr::Type::Debug:
2507 case QV4::Moth::Instr::Type::DeclareVar:
2508 case QV4::Moth::Instr::Type::IteratorClose:
2509 case QV4::Moth::Instr::Type::IteratorNext:
2510 case QV4::Moth::Instr::Type::IteratorNextForYieldStar:
2511 case QV4::Moth::Instr::Type::Jump:
2512 case QV4::Moth::Instr::Type::JumpFalse:
2513 case QV4::Moth::Instr::Type::JumpNoException:
2514 case QV4::Moth::Instr::Type::JumpNotUndefined:
2515 case QV4::Moth::Instr::Type::JumpTrue:
2516 case QV4::Moth::Instr::Type::MoveConst:
2517 case QV4::Moth::Instr::Type::MoveReg:
2518 case QV4::Moth::Instr::Type::MoveRegExp:
2519 case QV4::Moth::Instr::Type::PopContext:
2520 case QV4::Moth::Instr::Type::PushBlockContext:
2521 case QV4::Moth::Instr::Type::PushCatchContext:
2522 case QV4::Moth::Instr::Type::PushScriptContext:
2523 case QV4::Moth::Instr::Type::Resume:
2524 case QV4::Moth::Instr::Type::Ret:
2525 case QV4::Moth::Instr::Type::SetException:
2526 case QV4::Moth::Instr::Type::SetLookup:
2527 case QV4::Moth::Instr::Type::SetUnwindHandler:
2528 case QV4::Moth::Instr::Type::StoreElement:
2529 case QV4::Moth::Instr::Type::StoreLocal:
2530 case QV4::Moth::Instr::Type::StoreNameSloppy:
2531 case QV4::Moth::Instr::Type::StoreNameStrict:
2532 case QV4::Moth::Instr::Type::StoreProperty:
2533 case QV4::Moth::Instr::Type::StoreReg:
2534 case QV4::Moth::Instr::Type::StoreScopedLocal:
2535 case QV4::Moth::Instr::Type::StoreSuperProperty:
2536 case QV4::Moth::Instr::Type::ThrowException:
2537 case QV4::Moth::Instr::Type::ThrowOnNullOrUndefined:
2538 case QV4::Moth::Instr::Type::UnwindDispatch:
2539 case QV4::Moth::Instr::Type::UnwindToLabel:
2540 case QV4::Moth::Instr::Type::Yield:
2541 case QV4::Moth::Instr::Type::YieldStar:
2543 case QV4::Moth::Instr::Type::Add:
2544 case QV4::Moth::Instr::Type::As:
2545 case QV4::Moth::Instr::Type::BitAnd:
2546 case QV4::Moth::Instr::Type::BitAndConst:
2547 case QV4::Moth::Instr::Type::BitOr:
2548 case QV4::Moth::Instr::Type::BitOrConst:
2549 case QV4::Moth::Instr::Type::BitXor:
2550 case QV4::Moth::Instr::Type::BitXorConst:
2551 case QV4::Moth::Instr::Type::CallGlobalLookup:
2552 case QV4::Moth::Instr::Type::CallName:
2553 case QV4::Moth::Instr::Type::CallPossiblyDirectEval:
2554 case QV4::Moth::Instr::Type::CallProperty:
2555 case QV4::Moth::Instr::Type::CallPropertyLookup:
2556 case QV4::Moth::Instr::Type::CallQmlContextPropertyLookup:
2557 case QV4::Moth::Instr::Type::CallValue:
2558 case QV4::Moth::Instr::Type::CallWithReceiver:
2559 case QV4::Moth::Instr::Type::CallWithSpread:
2560 case QV4::Moth::Instr::Type::CmpEq:
2561 case QV4::Moth::Instr::Type::CmpEqInt:
2562 case QV4::Moth::Instr::Type::CmpEqNull:
2563 case QV4::Moth::Instr::Type::CmpGe:
2564 case QV4::Moth::Instr::Type::CmpGt:
2565 case QV4::Moth::Instr::Type::CmpIn:
2566 case QV4::Moth::Instr::Type::CmpInstanceOf:
2567 case QV4::Moth::Instr::Type::CmpLe:
2568 case QV4::Moth::Instr::Type::CmpLt:
2569 case QV4::Moth::Instr::Type::CmpNe:
2570 case QV4::Moth::Instr::Type::CmpNeInt:
2571 case QV4::Moth::Instr::Type::CmpNeNull:
2572 case QV4::Moth::Instr::Type::CmpStrictEqual:
2573 case QV4::Moth::Instr::Type::CmpStrictNotEqual:
2574 case QV4::Moth::Instr::Type::Construct:
2575 case QV4::Moth::Instr::Type::ConstructWithSpread:
2576 case QV4::Moth::Instr::Type::CreateClass:
2577 case QV4::Moth::Instr::Type::CreateMappedArgumentsObject:
2578 case QV4::Moth::Instr::Type::CreateRestParameter:
2579 case QV4::Moth::Instr::Type::CreateUnmappedArgumentsObject:
2580 case QV4::Moth::Instr::Type::Decrement:
2581 case QV4::Moth::Instr::Type::DefineArray:
2582 case QV4::Moth::Instr::Type::DefineObjectLiteral:
2583 case QV4::Moth::Instr::Type::DeleteName:
2584 case QV4::Moth::Instr::Type::DeleteProperty:
2585 case QV4::Moth::Instr::Type::DestructureRestElement:
2586 case QV4::Moth::Instr::Type::Div:
2587 case QV4::Moth::Instr::Type::Exp:
2588 case QV4::Moth::Instr::Type::GetException:
2589 case QV4::Moth::Instr::Type::GetIterator:
2590 case QV4::Moth::Instr::Type::GetLookup:
2591 case QV4::Moth::Instr::Type::GetOptionalLookup:
2592 case QV4::Moth::Instr::Type::GetTemplateObject:
2593 case QV4::Moth::Instr::Type::Increment:
2594 case QV4::Moth::Instr::Type::InitializeBlockDeadTemporalZone:
2595 case QV4::Moth::Instr::Type::LoadClosure:
2596 case QV4::Moth::Instr::Type::LoadConst:
2597 case QV4::Moth::Instr::Type::LoadElement:
2598 case QV4::Moth::Instr::Type::LoadFalse:
2599 case QV4::Moth::Instr::Type::LoadGlobalLookup:
2600 case QV4::Moth::Instr::Type::LoadImport:
2601 case QV4::Moth::Instr::Type::LoadInt:
2602 case QV4::Moth::Instr::Type::LoadLocal:
2603 case QV4::Moth::Instr::Type::LoadName:
2604 case QV4::Moth::Instr::Type::LoadNull:
2605 case QV4::Moth::Instr::Type::LoadOptionalProperty:
2606 case QV4::Moth::Instr::Type::LoadProperty:
2607 case QV4::Moth::Instr::Type::LoadQmlContextPropertyLookup:
2608 case QV4::Moth::Instr::Type::LoadReg:
2609 case QV4::Moth::Instr::Type::LoadRuntimeString:
2610 case QV4::Moth::Instr::Type::LoadScopedLocal:
2611 case QV4::Moth::Instr::Type::LoadSuperConstructor:
2612 case QV4::Moth::Instr::Type::LoadSuperProperty:
2613 case QV4::Moth::Instr::Type::LoadTrue:
2614 case QV4::Moth::Instr::Type::LoadUndefined:
2615 case QV4::Moth::Instr::Type::LoadZero:
2616 case QV4::Moth::Instr::Type::Mod:
2617 case QV4::Moth::Instr::Type::Mul:
2618 case QV4::Moth::Instr::Type::PushWithContext:
2619 case QV4::Moth::Instr::Type::Shl:
2620 case QV4::Moth::Instr::Type::ShlConst:
2621 case QV4::Moth::Instr::Type::Shr:
2622 case QV4::Moth::Instr::Type::ShrConst:
2623 case QV4::Moth::Instr::Type::Sub:
2624 case QV4::Moth::Instr::Type::TailCall:
2625 case QV4::Moth::Instr::Type::ToObject:
2626 case QV4::Moth::Instr::Type::TypeofName:
2627 case QV4::Moth::Instr::Type::TypeofValue:
2628 case QV4::Moth::Instr::Type::UCompl:
2629 case QV4::Moth::Instr::Type::UMinus:
2630 case QV4::Moth::Instr::Type::UNot:
2631 case QV4::Moth::Instr::Type::UPlus:
2632 case QV4::Moth::Instr::Type::UShr:
2633 case QV4::Moth::Instr::Type::UShrConst:
2636 Q_UNREACHABLE_RETURN(
false);
2651void QQmlJSTypePropagator::endInstruction(QV4::Moth::Instr::Type instr)
2653 InstructionAnnotation ¤tInstruction = m_state.annotations[currentInstructionOffset()];
2654 currentInstruction.changedRegister = m_state.changedRegister();
2655 currentInstruction.changedRegisterIndex = m_state.changedRegisterIndex();
2656 currentInstruction.readRegisters = m_state.takeReadRegisters();
2657 currentInstruction.hasExternalSideEffects = m_state.hasExternalSideEffects();
2658 currentInstruction.hasInternalSideEffects = m_state.hasInternalSideEffects();
2659 currentInstruction.isRename = m_state.isRename();
2661 bool populates = populatesAccumulator(instr);
2662 int changedIndex = m_state.changedRegisterIndex();
2665 if (instr != QV4::Moth::Instr::Type::InitializeBlockDeadTemporalZone) {
2666 Q_ASSERT((populates && changedIndex == Accumulator && m_state.accumulatorOut().isValid())
2667 || (!populates && changedIndex != Accumulator));
2670 if (!m_logger->currentFunctionHasCompileError() && !isNoop(instr)) {
2673 Q_ASSERT(m_state.hasInternalSideEffects() || changedIndex != InvalidRegister);
2676 if (changedIndex != InvalidRegister) {
2677 Q_ASSERT(m_logger->currentFunctionHasCompileError() || m_state.changedRegister().isValid());
2678 VirtualRegister &r = m_state.registers[changedIndex];
2679 r.content = m_state.changedRegister();
2681 r.affectedBySideEffects = m_state.isRename()
2682 && m_state.isRegisterAffectedBySideEffects(m_state.renameSourceRegisterIndex());
2683 m_state.clearChangedRegister();
2686 m_state.resetSideEffects();
2687 m_state.setIsRename(
false);
2688 m_state.setReadRegisters(VirtualRegisters());
2689 m_state.instructionHasError =
false;