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
qqmljscodegenerator.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:critical reason:code-generation
4
10
11#include <private/qqmljstypepropagator_p.h>
12
13#include <private/qqmlirbuilder_p.h>
14#include <private/qqmljsscope_p.h>
15#include <private/qqmljsutils_p.h>
16#include <private/qv4compilerscanfunctions_p.h>
17#include <private/qduplicatetracker_p.h>
18
19#include <QtCore/qdir.h>
20#include <QtCore/qfileinfo.h>
21
23
24using namespace Qt::StringLiterals;
25
26/*!
27 * \internal
28 * \class QQmlJSCodeGenerator
29 *
30 * This is a final compile pass that generates C++ code from a function and the
31 * annotations produced by previous passes. Such annotations are produced by
32 * QQmlJSTypePropagator, and possibly amended by other passes.
33 */
34
35#define BYTECODE_UNIMPLEMENTED() Q_ASSERT_X(false, Q_FUNC_INFO, "not implemented");
36
37#define INJECT_TRACE_INFO(function)
38 static const bool injectTraceInfo = true;
39 if (injectTraceInfo) {
40 m_body += u"// "_s + QStringLiteral(#function) + u'\n';
41 }
42
43#define REJECT
44 return reject
45
46static bool isTypeStorable(const QQmlJSTypeResolver *resolver, const QQmlJSScope::ConstPtr &type)
47{
48 return !type.isNull() && type != resolver->nullType() && type != resolver->voidType();
49}
50
51QString QQmlJSCodeGenerator::castTargetName(const QQmlJSScope::ConstPtr &type) const
52{
53 return type->augmentedInternalName();
54}
55
56QQmlJSCodeGenerator::QQmlJSCodeGenerator(
57 const QV4::Compiler::Context *compilerContext,
58 const QV4::Compiler::JSUnitGenerator *unitGenerator, const QQmlJSTypeResolver *typeResolver,
59 QQmlJSLogger *logger, const BasicBlocks &basicBlocks,
60 const InstructionAnnotations &annotations, bool noAotValidation)
61 : QQmlJSCompilePass(unitGenerator, typeResolver, logger, basicBlocks, annotations)
62 , m_context(compilerContext)
63 , m_lookupSignaturesRecorder(logger->filePath(), typeResolver)
64 , m_noAotValidation(noAotValidation)
65{}
66
67QString QQmlJSCodeGenerator::metaTypeFromType(const QQmlJSScope::ConstPtr &type) const
68{
69 return u"QMetaType::fromType<"_s + type->augmentedInternalName() + u">()"_s;
70}
71
72QString QQmlJSCodeGenerator::metaTypeFromName(const QQmlJSScope::ConstPtr &type) const
73{
74 return u"[]() { static constexpr QByteArrayView name(\""_s
75 + QString::fromUtf8(QMetaObject::normalizedType(type->augmentedInternalName().toUtf8()))
76 + u"\"); static const auto t = QMetaType::fromName(name); return t; }()"_s;
77}
78
79QString QQmlJSCodeGenerator::compositeListMetaType(const QString &elementName) const
80{
81 return u"QQmlPrivate::compositeListMetaType(aotContext->compilationUnit, "_s
82 + (m_jsUnitGenerator->hasStringId(elementName)
83 ? QString::number(m_jsUnitGenerator->getStringId(elementName)) + u')'
84 : u"QStringLiteral(\"%1\"))"_s.arg(elementName));
85}
86
87QString QQmlJSCodeGenerator::compositeMetaType(const QString &elementName) const
88{
89 return u"QQmlPrivate::compositeMetaType(aotContext->compilationUnit, "_s
90 + (m_jsUnitGenerator->hasStringId(elementName)
91 ? QString::number(m_jsUnitGenerator->getStringId(elementName)) + u')'
92 : u"QStringLiteral(\"%1\"))"_s.arg(elementName));
93}
94
95QString QQmlJSCodeGenerator::metaObject(const QQmlJSScope::ConstPtr &objectType)
96{
97 if (objectType->isComposite()) {
98 const QString name = m_typeResolver->nameForType(objectType);
99 if (name.isEmpty()) {
100 REJECT<QString>(
101 u"retrieving the metaObject of a composite type without an element name."_s);
102 }
103 return compositeMetaType(name) + u".metaObject()"_s;
104 }
105
106 if (objectType->internalName() == u"QObject"_s
107 || objectType->internalName() == u"QQmlComponent"_s) {
108 return u'&' + objectType->internalName() + u"::staticMetaObject"_s;
109 }
110 return metaTypeFromName(objectType) + u".metaObject()"_s;
111}
112
113QString QQmlJSCodeGenerator::metaType(const QQmlJSScope::ConstPtr &type)
114{
115 if (type->isOpaqueType()) {
116 REJECT<QString>("Retrieving the metatype of an unregistered type"_L1);
117 }
118 if (type->isComposite()) {
119 const QString name = m_typeResolver->nameForType(type);
120 if (name.isEmpty()) {
121 REJECT<QString>(
122 u"retrieving the metaType of a composite type without an element name."_s);
123 }
124 return compositeMetaType(name);
125 }
126
127 if (type->isListProperty() && type->elementType()->isComposite()) {
128 const QString name = m_typeResolver->nameForType(type->elementType());
129 Q_ASSERT(!name.isEmpty()); // There can't be a list with anonymous composite value type
130 return compositeListMetaType(name);
131 }
132
133 return (m_typeResolver->genericType(type) == type)
134 ? metaTypeFromType(type)
135 : metaTypeFromName(type);
136}
137
138static QString registerName(int registerIndex, int offset)
139{
140 // 'a<n>': argument number n
141 // 'v<n>_<m>': value produced by instruction for register n at byte code offset m
142 // 'c<n>_<m>': value produced by conversion for register n at byte code offset m
143 // The output of QV4_SHOW_BYTECODE=1 corresponds to the 'm' values.
144 // We can convert a value and then store a new value in the same register with one instruction.
145 // That's why we need both 'v' and 'c'.
146
147 if (offset < 0)
148 return u"s.a%1"_s.arg(registerIndex - QQmlJSCompilePass::Argc);
149 if (registerIndex < 0)
150 return u"s.c%1_%2"_s.arg(-registerIndex).arg(offset);
151 return u"s.v%1_%2"_s.arg(registerIndex).arg(offset);
152}
153
154QQmlJSAotFunction QQmlJSCodeGenerator::run(const Function *function, bool basicBlocksValidationFailed)
155{
156 m_function = function;
157
158 if (m_context->contextType == QV4::Compiler::ContextType::Binding
159 && m_function->returnType.contains(m_typeResolver->qQmlScriptStringType())) {
160 const QString reason = u"binding for property of type QQmlScriptString; nothing to do."_s;
161 skip(reason);
162 QQmlJSAotFunction result;
163 result.skipReason = reason;
164 return result;
165 }
166
167 const auto addVariable = [&](int registerIndex, int offset, QQmlJSRegisterContent content) {
168 const QQmlJSScope::ConstPtr seenType = content.storedType();
169 // Don't generate any variables for registers that are initialized with undefined.
170 if (!isTypeStorable(m_typeResolver, seenType))
171 return;
172
173 const auto oldSize = m_registerVariables.size();
174 auto &e = m_registerVariables[content];
175 if (m_registerVariables.size() != oldSize) {
176 e.variableName = registerName(registerIndex, offset);
177 e.storedType = seenType;
178 e.initialRegisterIndex = std::abs(registerIndex);
179 } else {
180 e.variableName += u'_' + registerName(registerIndex, offset).mid(2); // skip "s."
181 }
182 ++e.numTracked;
183 };
184
185QT_WARNING_PUSH
186QT_WARNING_DISABLE_CLANG("-Wrange-loop-analysis")
187 for (auto it = m_annotations.constBegin(), iend = m_annotations.constEnd(); it != iend; ++it) {
188 const int registerIndex = it->second.changedRegisterIndex;
189 if (registerIndex != InvalidRegister)
190 addVariable(registerIndex, it.key(), it->second.changedRegister);
191 for (auto jt = it->second.typeConversions.constBegin(),
192 jend = it->second.typeConversions.constEnd();
193 jt != jend; ++jt) {
194 // Conversions get a negative register number to discern them.
195 const int registerIndex = jt.key();
196 if (registerIndex != InvalidRegister)
197 addVariable(-registerIndex, it.key(), jt.value().content);
198 }
199 }
200QT_WARNING_POP
201
202 // ensure we have m_labels for loops
203 for (const auto loopLabel : m_context->labelInfo)
204 m_labels.insert(loopLabel, u"label_%1"_s.arg(m_labels.size()));
205
206 // Initialize the first instruction's state to hold the arguments.
207 // After this, the arguments (or whatever becomes of them) are carried
208 // over into any further basic blocks automatically.
209 m_state.State::operator=(initialState(m_function));
210
211 m_pool->setAllocationMode(QQmlJSRegisterContentPool::Temporary);
212 const QByteArray byteCode = function->code;
213 decode(byteCode.constData(), static_cast<uint>(byteCode.size()));
214 m_pool->setAllocationMode(QQmlJSRegisterContentPool::Permanent);
215
216 QQmlJSAotFunction result;
217 result.includes.swap(m_includes);
218
219 if (basicBlocksValidationFailed) {
220 result.code += "// QV4_BASIC_BLOCK_VALIDATION_FAILED: This file failed compilation "_L1
221 "with basic blocks validation but compiled without it.\n"_L1;
222 }
223
224 result.code += u"// %1 at line %2, column %3\n"_s
225 .arg(m_context->name).arg(m_context->line).arg(m_context->column);
226
227 QStringList initializations;
228 QStringList markings;
229 for (auto registerIt = m_registerVariables.cbegin(), registerEnd = m_registerVariables.cend();
230 registerIt != registerEnd; ++registerIt) {
231
232 // Remove the "s.". Inside the struct we need the plain name.
233 QString declarationName = registerIt->variableName.mid(2);
234
235 const int registerIndex = registerIt->initialRegisterIndex;
236 const bool registerIsArgument = isArgument(registerIndex);
237
238 QString code = registerIt->storedType->internalName();
239
240 const QQmlJSScope::ConstPtr storedType = registerIt->storedType;
241 const bool isPointer
242 = (storedType->accessSemantics() == QQmlJSScope::AccessSemantics::Reference);
243 if (isPointer)
244 code += u" *"_s;
245 else
246 code += u' ';
247
248 if (!registerIsArgument
249 && registerIndex != Accumulator
250 && registerIndex != This
251 && !function->registerTypes[registerIndex - firstRegisterIndex()].contains(
252 m_typeResolver->voidType())) {
253 code += declarationName + u" = "_s;
254 code += convertStored(m_typeResolver->voidType(), storedType, QString());
255 } else if (registerIsArgument && argumentType(registerIndex).isStoredIn(storedType)) {
256 const int argumentIndex = registerIndex - FirstArgument;
257 const QQmlJSRegisterContent argument
258 = m_function->argumentTypes[argumentIndex];
259 const QQmlJSRegisterContent originalArgument = originalType(argument);
260
261 const bool needsConversion = argument != originalArgument;
262 if (!isPointer && registerIt->numTracked == 1 && !needsConversion) {
263 // Not a pointer, never written to, and doesn't need any initial conversion.
264 // This is a readonly argument.
265 //
266 // We would like to make the variable a const ref if it's a readonly argument,
267 // but due to the various call interfaces accepting non-const values, we can't.
268 // We rely on those calls to still not modify their arguments in place.
269 code += u'&';
270 }
271
272 code += declarationName + u" = "_s;
273
274 const auto originalContained = m_typeResolver->originalContainedType(argument);
275 QString originalValue;
276 const bool needsQVariantWrapping =
277 storedType->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence
278 && !originalContained->isReferenceType()
279 && storedType == m_typeResolver->varType()
280 && originalContained != m_typeResolver->varType();
281 if (needsQVariantWrapping) {
282 originalValue = u"QVariant(%1, argv[%2])"_s.arg(metaTypeFromName(originalContained))
283 .arg(QString::number(argumentIndex + 1));
284 } else {
285 originalValue = u"(*static_cast<"_s + castTargetName(originalArgument.storedType())
286 + u"*>(argv["_s + QString::number(argumentIndex + 1) + u"]))"_s;
287 }
288
289 if (needsConversion)
290 code += conversion(originalArgument, argument, originalValue);
291 else
292 code += originalValue;
293 } else if (isPointer) {
294 code += declarationName + u" = nullptr"_s;
295 } else {
296 code += declarationName;
297 }
298 code += u";\n"_s;
299
300 initializations.push_back(std::move(code));
301
302 if (isPointer) {
303 markings.append(u" aotContext->mark("_s + declarationName + u", markStack);\n");
304 } else if (storedType == m_typeResolver->varType()) {
305 markings.append(u" aotContext->mark("_s + declarationName + u", markStack);\n");
306 } else if (storedType == m_typeResolver->listPropertyType()) {
307 // No need to mark that since it's always backed by a property
308 } else if (storedType == m_typeResolver->variantMapType()
309 || storedType->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
310 QString marking = u" for (const auto &v : std::as_const(" + declarationName + u"))\n"
311 + u" aotContext->mark(v, markStack);\n";
312 markings.append(std::move(marking));
313 }
314 }
315
316 result.code += u"struct Storage : QQmlPrivate::AOTTrackedLocalsStorage {\n"_s;
317 result.code += u"Storage(const QQmlPrivate::AOTCompiledContext *ctxt, void **a)"_s;
318 result.code += u" : aotContext(ctxt), argv(a) {}\n"_s;
319 result.code += u"void markObjects(QV4::MarkStack *markStack) const final {"_s;
320 result.code += u" Q_UNUSED(markStack);\n"_s;
321
322 markings.sort();
323 for (const QString &marking : std::as_const(markings))
324 result.code += marking;
325
326 result.code += u"}\n"_s;
327 result.code += u"const QQmlPrivate::AOTCompiledContext *aotContext;\n"_s;
328 result.code += u"void **argv;\n"_s;
329
330 // Sort them to obtain stable output.
331 initializations.sort();
332 for (const QString &initialization : std::as_const(initializations))
333 result.code += initialization;
334
335 result.code += u"};\nStorage s(aotContext, argv);\n"_s;
336 result.code += u"aotContext->setLocals(&s);\n"_s;
337
338 result.code += m_body;
339
340
341 QString signature
342 = u" struct { QV4::ExecutableCompilationUnit *compilationUnit; } c { contextUnit };\n"
343 " const auto *aotContext = &c;\n"
344 " Q_UNUSED(aotContext);\n"_s;
345
346 if (function->returnType.isValid()) {
347 signature += u" argTypes[0] = %1;\n"_s.arg(
348 metaType(function->returnType.containedType()));
349 } else {
350 signature += u" argTypes[0] = QMetaType();\n"_s;
351 }
352 result.numArguments = function->argumentTypes.length();
353 for (qsizetype i = 0; i != result.numArguments; ++i) {
354 signature += u" argTypes[%1] = %2;\n"_s.arg(
355 QString::number(i + 1),
356 metaType(m_typeResolver->originalContainedType(function->argumentTypes[i])));
357 }
358
359 result.signature = std::move(signature);
360 return result;
361}
362
363void QQmlJSCodeGenerator::generateReturnError()
364{
365 const auto finalizeReturn = qScopeGuard([this]() { m_body += u"return;\n"_s; });
366
367 m_body += u"aotContext->setReturnValueUndefined();\n"_s;
368 const auto ret = m_function->returnType;
369 if (!ret.isValid() || ret.contains(m_typeResolver->voidType()))
370 return;
371
372 m_body += u"if (argv[0]) {\n"_s;
373
374 const auto contained = ret.containedType();
375 const auto stored = ret.storedType();
376 if (contained->isReferenceType() && stored->isReferenceType()) {
377 m_body += u" *static_cast<"_s
378 + stored->augmentedInternalName()
379 + u" *>(argv[0]) = nullptr;\n"_s;
380 } else if (contained == stored) {
381 m_body += u" *static_cast<"_s + stored->internalName() + u" *>(argv[0]) = "_s
382 + stored->internalName() + u"();\n"_s;
383 } else {
384 m_body += u" const QMetaType returnType = "_s
385 + metaType(ret.containedType()) + u";\n"_s;
386 m_body += u" returnType.destruct(argv[0]);\n"_s;
387 m_body += u" returnType.construct(argv[0]);\n "_s;
388 }
389
390 m_body += u"}\n"_s;
391}
392
393void QQmlJSCodeGenerator::generate_Ret()
394{
395 INJECT_TRACE_INFO(generate_Ret);
396
397 const auto finalizeReturn = qScopeGuard([this]() {
398 m_body += u"return;\n"_s;
399 m_skipUntilNextLabel = true;
400 resetState();
401 });
402
403 if (!m_function->returnType.isValid())
404 return;
405
406 m_body += u"if (argv[0]) {\n"_s;
407
408 const QString signalUndefined = u"aotContext->setReturnValueUndefined();\n"_s;
409 const QString in = m_state.accumulatorVariableIn;
410
411 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
412
413 if (in.isEmpty()) {
414 if (accumulatorIn.isStoredIn(m_typeResolver->voidType()))
415 m_body += signalUndefined;
416 } else if (accumulatorIn.isStoredIn(m_typeResolver->varType())) {
417 m_body += u" if (!"_s + in + u".isValid())\n"_s;
418 m_body += u" "_s + signalUndefined;
419 } else if (accumulatorIn.isStoredIn(m_typeResolver->jsPrimitiveType())) {
420 m_body += u" if ("_s + in + u".type() == QJSPrimitiveValue::Undefined)\n"_s;
421 m_body += u" "_s + signalUndefined;
422 } else if (accumulatorIn.isStoredIn(m_typeResolver->jsValueType())) {
423 m_body += u" if ("_s + in + u".isUndefined())\n"_s;
424 m_body += u" "_s + signalUndefined;
425 }
426
427 if (m_function->returnType.contains(m_typeResolver->voidType())) {
428 m_body += u"}\n"_s;
429 return;
430 }
431
432 const auto contained = m_function->returnType.containedType();
433 const auto stored = m_function->returnType.storedType();
434 if (contained == stored || (contained->isReferenceType() && stored->isReferenceType())) {
435 // We can always std::move here, no matter what the optimization pass has detected. The
436 // function returns and nothing can access the accumulator register anymore afterwards.
437 m_body += u" *static_cast<"_s
438 + stored->augmentedInternalName()
439 + u" *>(argv[0]) = "_s
440 + conversion(
441 accumulatorIn, m_function->returnType,
442 m_typeResolver->isTriviallyCopyable(accumulatorIn.storedType())
443 ? in
444 : u"std::move("_s + in + u')')
445 + u";\n"_s;
446 } else if (accumulatorIn.contains(contained)) {
447 m_body += u" const QMetaType returnType = "_s + contentType(accumulatorIn, in)
448 + u";\n"_s;
449 m_body += u" returnType.destruct(argv[0]);\n"_s;
450 m_body += u" returnType.construct(argv[0], "_s
451 + contentPointer(accumulatorIn, in) + u");\n"_s;
452 } else {
453 m_body += u" const auto converted = "_s
454 + conversion(accumulatorIn, m_function->returnType,
455 consumedAccumulatorVariableIn()) + u";\n"_s;
456 m_body += u" const QMetaType returnType = "_s
457 + contentType(m_function->returnType, u"converted"_s)
458 + u";\n"_s;
459 m_body += u" returnType.destruct(argv[0]);\n"_s;
460 m_body += u" returnType.construct(argv[0], "_s
461 + contentPointer(m_function->returnType, u"converted"_s) + u");\n"_s;
462 }
463
464 m_body += u"}\n"_s;
465}
466
467void QQmlJSCodeGenerator::generate_Debug()
468{
470}
471
472static QString toNumericString(double value)
473{
474 if (value >= std::numeric_limits<int>::min() && value <= std::numeric_limits<int>::max()) {
475 const int i = value;
476 if (i == value)
477 return QString::number(i);
478 }
479
480 switch (qFpClassify(value)) {
481 case FP_INFINITE: {
482 const QString inf = u"std::numeric_limits<double>::infinity()"_s;
483 return std::signbit(value) ? (u'-' + inf) : inf;
484 }
485 case FP_NAN:
486 return u"std::numeric_limits<double>::quiet_NaN()"_s;
487 case FP_ZERO:
488 return std::signbit(value) ? u"-0.0"_s : u"0"_s;
489 default:
490 break;
491 }
492
493 return QString::number(value, 'f', std::numeric_limits<double>::max_digits10);
494}
495
496void QQmlJSCodeGenerator::generate_LoadConst(int index)
497{
498 INJECT_TRACE_INFO(generate_LoadConst);
499
500 // You cannot actually get it to generate LoadConst for anything but double. We have
501 // a numer of specialized instructions for the other types, after all. However, let's
502 // play it safe.
503
504 const QV4::ReturnedValue encodedConst = m_jsUnitGenerator->constant(index);
505 const QV4::StaticValue value = QV4::StaticValue::fromReturnedValue(encodedConst);
506 const QQmlJSScope::ConstPtr type = m_typeResolver->typeForConst(encodedConst);
507
508 m_body += m_state.accumulatorVariableOut + u" = "_s;
509 if (type == m_typeResolver->realType()) {
510 m_body += conversion(
511 type, m_state.accumulatorOut(),
512 toNumericString(value.doubleValue()));
513 } else if (type == m_typeResolver->int32Type()) {
514 m_body += conversion(
515 type, m_state.accumulatorOut(),
516 QString::number(value.integerValue()));
517 } else if (type == m_typeResolver->boolType()) {
518 m_body += conversion(
519 type, m_state.accumulatorOut(),
520 value.booleanValue() ? u"true"_s : u"false"_s);
521 } else if (type == m_typeResolver->voidType()) {
522 m_body += conversion(
523 type, m_state.accumulatorOut(),
524 QString());
525 } else if (type == m_typeResolver->nullType()) {
526 m_body += conversion(
527 type, m_state.accumulatorOut(),
528 u"nullptr"_s);
529 } else {
530 REJECT(u"unsupported constant type"_s);
531 }
532
533 m_body += u";\n"_s;
534}
535
536void QQmlJSCodeGenerator::generate_LoadZero()
537{
538 INJECT_TRACE_INFO(generate_LoadZero);
539
540 m_body += m_state.accumulatorVariableOut;
541 m_body += u" = "_s + conversion(
542 m_typeResolver->int32Type(), m_state.accumulatorOut(), u"0"_s);
543 m_body += u";\n"_s;
544}
545
546void QQmlJSCodeGenerator::generate_LoadTrue()
547{
548 INJECT_TRACE_INFO(generate_LoadTrue);
549
550 m_body += m_state.accumulatorVariableOut;
551 m_body += u" = "_s + conversion(
552 m_typeResolver->boolType(), m_state.accumulatorOut(), u"true"_s);
553 m_body += u";\n"_s;
554}
555
556void QQmlJSCodeGenerator::generate_LoadFalse()
557{
558 INJECT_TRACE_INFO(generate_LoadFalse);
559
560 m_body += m_state.accumulatorVariableOut;
561 m_body += u" = "_s + conversion(
562 m_typeResolver->boolType(), m_state.accumulatorOut(), u"false"_s);
563 m_body += u";\n"_s;
564}
565
566void QQmlJSCodeGenerator::generate_LoadNull()
567{
568 INJECT_TRACE_INFO(generate_LoadNull);
569
570 m_body += m_state.accumulatorVariableOut + u" = "_s;
571 m_body += conversion(m_typeResolver->nullType(), m_state.accumulatorOut(),
572 u"nullptr"_s);
573 m_body += u";\n"_s;
574}
575
576void QQmlJSCodeGenerator::generate_LoadUndefined()
577{
578 INJECT_TRACE_INFO(generate_LoadUndefined);
579
580 m_body += m_state.accumulatorVariableOut + u" = "_s;
581 m_body += conversion(m_typeResolver->voidType(), m_state.accumulatorOut(),
582 QString());
583 m_body += u";\n"_s;
584}
585
586void QQmlJSCodeGenerator::generate_LoadInt(int value)
587{
588 INJECT_TRACE_INFO(generate_LoadInt);
589
590 m_body += m_state.accumulatorVariableOut;
591 m_body += u" = "_s;
592 m_body += conversion(m_typeResolver->int32Type(), m_state.accumulatorOut(),
593 QString::number(value));
594 m_body += u";\n"_s;
595}
596
597void QQmlJSCodeGenerator::generate_MoveConst(int constIndex, int destTemp)
598{
599 INJECT_TRACE_INFO(generate_MoveConst);
600
601 Q_ASSERT(destTemp == m_state.changedRegisterIndex());
602
603 auto var = changedRegisterVariable();
604 if (var.isEmpty())
605 return; // Do not load 'undefined'
606
607 const auto v4Value = QV4::StaticValue::fromReturnedValue(
608 m_jsUnitGenerator->constant(constIndex));
609
610 const auto changed = m_state.changedRegister();
611 QQmlJSScope::ConstPtr contained;
612 QString input;
613
614 m_body += var + u" = "_s;
615 if (v4Value.isNull()) {
616 contained = m_typeResolver->nullType();
617 } else if (v4Value.isUndefined()) {
618 contained = m_typeResolver->voidType();
619 } else if (v4Value.isBoolean()) {
620 contained = m_typeResolver->boolType();
621 input = v4Value.booleanValue() ? u"true"_s : u"false"_s;
622 } else if (v4Value.isInteger()) {
623 contained = m_typeResolver->int32Type();
624 input = QString::number(v4Value.int_32());
625 } else if (v4Value.isDouble()) {
626 contained = m_typeResolver->realType();
627 input = toNumericString(v4Value.doubleValue());
628 } else {
629 REJECT(u"unknown const type"_s);
630 }
631 m_body += conversion(contained, changed, input) + u";\n"_s;
632}
633
634void QQmlJSCodeGenerator::generate_LoadReg(int reg)
635{
636 INJECT_TRACE_INFO(generate_LoadReg);
637
638 if (registerVariable(reg) == m_state.accumulatorVariableOut)
639 return;
640
641 m_body += m_state.accumulatorVariableOut;
642 m_body += u" = "_s;
643 m_body += conversion(
644 registerType(reg), m_state.accumulatorOut(), consumedRegisterVariable(reg));
645 m_body += u";\n"_s;
646}
647
648void QQmlJSCodeGenerator::generate_StoreReg(int reg)
649{
650 INJECT_TRACE_INFO(generate_StoreReg);
651
652 Q_ASSERT(m_state.changedRegisterIndex() == reg);
653 Q_ASSERT(m_state.accumulatorIn().isValid());
654 const QString var = changedRegisterVariable();
655 if (var.isEmpty())
656 return; // don't store "undefined"
657 if (var == m_state.accumulatorVariableIn)
658 return;
659 m_body += var;
660 m_body += u" = "_s;
661 m_body += conversion(m_state.accumulatorIn(), m_state.changedRegister(),
662 consumedAccumulatorVariableIn());
663 m_body += u";\n"_s;
664}
665
666void QQmlJSCodeGenerator::generate_MoveReg(int srcReg, int destReg)
667{
668 INJECT_TRACE_INFO(generate_MoveReg);
669
670 Q_ASSERT(m_state.changedRegisterIndex() == destReg);
671 const QString destRegName = changedRegisterVariable();
672 if (destRegName.isEmpty())
673 return; // don't store things we cannot store.
674 if (destRegName == registerVariable(srcReg))
675 return; // don't emit redundant copy statements
676 m_body += destRegName;
677 m_body += u" = "_s;
678 m_body += conversion(
679 registerType(srcReg), m_state.changedRegister(), consumedRegisterVariable(srcReg));
680 m_body += u";\n"_s;
681}
682
683void QQmlJSCodeGenerator::generate_LoadImport(int index)
684{
685 Q_UNUSED(index)
687}
688
689void QQmlJSCodeGenerator::generate_LoadLocal(int index)
690{
691 Q_UNUSED(index);
692 REJECT(u"LoadLocal"_s);
693}
694
695void QQmlJSCodeGenerator::generate_StoreLocal(int index)
696{
697 Q_UNUSED(index)
699}
700
701void QQmlJSCodeGenerator::generate_LoadScopedLocal(int scope, int index)
702{
703 Q_UNUSED(scope)
704 Q_UNUSED(index)
706}
707
708void QQmlJSCodeGenerator::generate_StoreScopedLocal(int scope, int index)
709{
710 Q_UNUSED(scope)
711 Q_UNUSED(index)
713}
714
715void QQmlJSCodeGenerator::generate_LoadRuntimeString(int stringId)
716{
717 INJECT_TRACE_INFO(generate_LoadRuntimeString);
718
719 m_body += m_state.accumulatorVariableOut;
720 m_body += u" = "_s;
721 m_body += conversion(m_typeResolver->stringType(), m_state.accumulatorOut(),
722 QQmlJSUtils::toLiteral(m_jsUnitGenerator->stringForIndex(stringId)));
723 m_body += u";\n"_s;
724}
725
726void QQmlJSCodeGenerator::generate_MoveRegExp(int regExpId, int destReg)
727{
728 Q_UNUSED(regExpId)
729 Q_UNUSED(destReg)
730 REJECT(u"MoveRegExp"_s);
731}
732
733void QQmlJSCodeGenerator::generate_LoadClosure(int value)
734{
735 Q_UNUSED(value)
736 REJECT(u"LoadClosure"_s);
737}
738
739void QQmlJSCodeGenerator::generate_LoadName(int nameIndex)
740{
741 Q_UNUSED(nameIndex)
742 REJECT(u"LoadName"_s);
743}
744
745void QQmlJSCodeGenerator::generate_LoadGlobalLookup(int index)
746{
747 INJECT_TRACE_INFO(generate_LoadGlobalLookup);
748
749 AccumulatorConverter registers(this);
750
751 const QString lookup = u"aotContext->loadGlobalLookup("_s + QString::number(index) + u", "_s
752 + contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut) + u')';
753 const QString initialization = u"aotContext->initLoadGlobalLookup("_s
754 + QString::number(index) + u", "_s
755 + contentType(m_state.accumulatorOut(), m_state.accumulatorVariableOut) + u')';
756 const QString preparation = getLookupPreparation(
757 m_state.accumulatorOut(), m_state.accumulatorVariableOut, index);
758 generateLookup(lookup, initialization, preparation);
759}
760
761void QQmlJSCodeGenerator::generate_LoadQmlContextPropertyLookup(int index)
762{
763 INJECT_TRACE_INFO(generate_LoadQmlContextPropertyLookup);
764
765 AccumulatorConverter registers(this);
766
767 const QQmlJSScope::ConstPtr stored = m_state.accumulatorOut().storedType();
768
769 const int nameIndex = m_jsUnitGenerator->lookupNameIndex(index);
770 if (m_state.accumulatorOut().scope().contains(m_typeResolver->jsGlobalObject())) {
771 // This produces a QJSValue. The QQmlJSMetaProperty used to analyze it may have more details
772 // but the QQmlJSAotContext API does not reflect them.
773 m_body += m_state.accumulatorVariableOut + u" = "_s
774 + conversion(
775 m_typeResolver->jsValueType(), m_state.accumulatorOut(),
776 u"aotContext->javaScriptGlobalProperty("_s + QString::number(nameIndex) + u")")
777 + u";\n"_s;
778 return;
779 }
780
781 const QString indexString = QString::number(index);
782 if (m_state.accumulatorOut().variant() == QQmlJSRegisterContent::ObjectById) {
783 const QString lookup = u"aotContext->loadContextIdLookup("_s
784 + indexString + u", "_s
785 + contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut) + u')';
786 const QString initialization = u"aotContext->initLoadContextIdLookup("_s
787 + indexString + u')';
788 generateLookup(lookup, initialization);
789 return;
790 }
791
792 const bool isProperty = m_state.accumulatorOut().isProperty();
793 if (isProperty) {
794 const QString lookup = u"aotContext->loadScopeObjectPropertyLookup("_s
795 + indexString + u", "_s
796 + contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut) + u')';
797 const QString initialization = u"aotContext->initLoadScopeObjectPropertyLookup("_s
798 + indexString + u')';
799 const QString preparation = getLookupPreparation(
800 m_state.accumulatorOut(), m_state.accumulatorVariableOut, index);
801
802 recordPropertyLookup(m_typeResolver->original(m_state.accumulatorOut()).scopeType(),
803 originalProperty(m_state.accumulatorOut()));
804 generateLookup(lookup, initialization, preparation);
805 } else if (m_state.accumulatorOut().isType() || m_state.accumulatorOut().isImportNamespace()) {
806 generateTypeLookup(index);
807 } else {
808 REJECT(u"lookup of %1"_s.arg(m_state.accumulatorOut().descriptiveName()));
809 }
810}
811
812void QQmlJSCodeGenerator::generate_StoreNameSloppy(int nameIndex)
813{
814 INJECT_TRACE_INFO(generate_StoreNameSloppy);
815
816 const QString name = m_jsUnitGenerator->stringForIndex(nameIndex);
817 const QQmlJSRegisterContent type = m_typeResolver->scopedType(m_function->qmlScope, name);
818 Q_ASSERT(type.isProperty());
819
820 switch (type.variant()) {
821 case QQmlJSRegisterContent::Property: {
822 // Do not convert here. We may intentionally pass the "wrong" type, for example to trigger
823 // a property reset.
824 m_body += u"aotContext->storeNameSloppy("_s + QString::number(nameIndex)
825 + u", "_s
826 + contentPointer(m_state.accumulatorIn(), m_state.accumulatorVariableIn)
827 + u", "_s
828 + contentType(m_state.accumulatorIn(), m_state.accumulatorVariableIn) + u')';
829 m_body += u";\n"_s;
830 break;
831 }
832 case QQmlJSRegisterContent::Method:
833 REJECT(u"assignment to scope method"_s);
834 default:
835 Q_UNREACHABLE();
836 }
837}
838
839void QQmlJSCodeGenerator::generate_StoreNameStrict(int name)
840{
841 Q_UNUSED(name)
843}
844
845void QQmlJSCodeGenerator::generate_LoadElement(int base)
846{
847 INJECT_TRACE_INFO(generate_LoadElement);
848
849 const QQmlJSRegisterContent baseType = registerType(base);
850
851 if (!baseType.isList() && !baseType.isStoredIn(m_typeResolver->stringType()))
852 REJECT(u"LoadElement with non-list base type "_s + baseType.descriptiveName());
853
854 const QString voidAssignment = u" "_s + m_state.accumulatorVariableOut + u" = "_s +
855 conversion(literalType(m_typeResolver->voidType()),
856 m_state.accumulatorOut(), QString()) + u";\n"_s;
857
858 AccumulatorConverter registers(this);
859
860 QString indexName = m_state.accumulatorVariableIn;
861 QQmlJSScope::ConstPtr indexType;
862 if (m_typeResolver->isNumeric(m_state.accumulatorIn())) {
863 indexType = m_state.accumulatorIn().containedType();
864 } else if (m_state.accumulatorIn().isConversion()) {
865 const auto target = m_typeResolver->extractNonVoidFromOptionalType(m_state.accumulatorIn());
866 if (m_typeResolver->isNumeric(target)) {
867 indexType = target.containedType();
868 m_body += u"if (!" + indexName + u".metaType().isValid())\n"
869 + voidAssignment
870 + u"else ";
871 indexName = convertStored(
872 m_state.accumulatorIn().storedType(), indexType, indexName);
873 } else {
874 REJECT(u"LoadElement with non-numeric argument"_s);
875 }
876 }
877
878 const QString baseName = registerVariable(base);
879
880 if (!m_typeResolver->isNativeArrayIndex(indexType)) {
881 m_body += u"if (!QJSNumberCoercion::isArrayIndex("_s + indexName + u"))\n"_s
882 + voidAssignment
883 + u"else "_s;
884 } else if (!m_typeResolver->isUnsignedInteger(indexType)) {
885 m_body += u"if ("_s + indexName + u" < 0)\n"_s
886 + voidAssignment
887 + u"else "_s;
888 }
889
890 if (baseType.isStoredIn(m_typeResolver->listPropertyType())) {
891 // Our QQmlListProperty only keeps plain QObject*.
892 m_body += u"if ("_s + indexName + u" < "_s + baseName
893 + u".count(&"_s + baseName + u"))\n"_s;
894 m_body += u" "_s + m_state.accumulatorVariableOut + u" = "_s +
895 conversion(m_typeResolver->qObjectType(), m_state.accumulatorOut(),
896 baseName + u".at(&"_s + baseName + u", "_s
897 + indexName + u')') + u";\n"_s;
898 m_body += u"else\n"_s
899 + voidAssignment;
900 return;
901 }
902
903 // Since we can do .at() below, we know that we can natively store the element type.
904 QQmlJSRegisterContent elementType = m_typeResolver->elementType(baseType);
905 elementType = m_pool->storedIn(
906 elementType, m_typeResolver->storedType(elementType.containedType()));
907
908 QString access = baseName + u".at("_s + indexName + u')';
909
910 // TODO: Once we get a char type in QML, use it here.
911 if (baseType.isStoredIn(m_typeResolver->stringType()))
912 access = u"QString("_s + access + u")"_s;
913 else if (isRegisterAffectedBySideEffects(base))
914 REJECT(u"LoadElement on a sequence potentially affected by side effects"_s);
915 else if (baseType.storedType()->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence)
916 REJECT(u"LoadElement on a sequence wrapped in a non-sequence type"_s);
917
918 m_body += u"if ("_s + indexName + u" < "_s + baseName + u".size())\n"_s;
919 m_body += u" "_s + m_state.accumulatorVariableOut + u" = "_s +
920 conversion(elementType, m_state.accumulatorOut(), access) + u";\n"_s;
921 m_body += u"else\n"_s
922 + voidAssignment;
923}
924
925void QQmlJSCodeGenerator::generate_StoreElement(int base, int index)
926{
927 INJECT_TRACE_INFO(generate_StoreElement);
928
929 const QQmlJSRegisterContent baseType = registerType(base);
930 const QQmlJSScope::ConstPtr indexType = registerType(index).containedType();
931
932 if (!m_typeResolver->isNumeric(indexType) || !baseType.isList())
933 REJECT(u"StoreElement with non-list base type or non-numeric arguments"_s);
934
935 if (baseType.storedType()->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence)
936 REJECT(u"indirect StoreElement"_s);
937
938 const QString baseName = registerVariable(base);
939 const QString indexName = registerVariable(index);
940
941 const auto elementType = m_typeResolver->genericType(
942 m_typeResolver->elementType(baseType).containedType());
943
944 addInclude(u"QtQml/qjslist.h"_s);
945 if (!m_typeResolver->isNativeArrayIndex(indexType))
946 m_body += u"if (QJSNumberCoercion::isArrayIndex("_s + indexName + u")) {\n"_s;
947 else if (!m_typeResolver->isUnsignedInteger(indexType))
948 m_body += u"if ("_s + indexName + u" >= 0) {\n"_s;
949 else
950 m_body += u"{\n"_s;
951
952 if (baseType.isStoredIn(m_typeResolver->listPropertyType())) {
953 m_body += u" if ("_s + indexName + u" < "_s + baseName + u".count(&"_s + baseName
954 + u"))\n"_s;
955 m_body += u" "_s + baseName + u".replace(&"_s + baseName
956 + u", "_s + indexName + u", "_s;
957 m_body += conversion(m_state.accumulatorIn(), elementType, m_state.accumulatorVariableIn)
958 + u");\n"_s;
959 m_body += u"}\n"_s;
960 return;
961 }
962
963 if (isRegisterAffectedBySideEffects(base))
964 REJECT(u"StoreElement on a sequence potentially affected by side effects"_s);
965 if (isRegisterAffectedBySideEffects(Accumulator))
966 REJECT(u"StoreElement of a value potentially affected by side effects"_s);
967
968 m_body += u" if ("_s + indexName + u" >= " + baseName + u".size())\n"_s;
969 m_body += u" QJSList(&"_s + baseName + u", aotContext->engine).resize("_s
970 + indexName + u" + 1);\n"_s;
971 m_body += u" "_s + baseName + u'[' + indexName + u"] = "_s;
972 m_body += conversion(m_state.accumulatorIn(), elementType, m_state.accumulatorVariableIn)
973 + u";\n"_s;
974 m_body += u"}\n"_s;
975
976 generateWriteBack(base);
977}
978
979void QQmlJSCodeGenerator::generate_LoadProperty(int nameIndex)
980{
981 Q_UNUSED(nameIndex)
982 REJECT(u"LoadProperty"_s);
983}
984
985void QQmlJSCodeGenerator::generate_LoadOptionalProperty(int name, int offset)
986{
987 Q_UNUSED(name)
988 Q_UNUSED(offset)
990}
991
992void QQmlJSCodeGenerator::generateEnumLookup(int index)
993{
994 const QString enumMember = m_state.accumulatorOut().enumMember();
995
996 // If we're referring to the type, there's nothing to do.
997 // However, we should not get here since no one can ever use the enum metatype.
998 // The lookup is dead code and should be optimized away.
999 // ... unless you are actually trying to store the metatype itself in a property.
1000 // We cannot compile such code.
1001 if (enumMember.isEmpty())
1002 REJECT(u"Lookup of enum metatype"_s);
1003
1004 // If the metaenum has the value, just use it and skip all the rest.
1005 const QQmlJSMetaEnum metaEnum = m_state.accumulatorOut().enumeration();
1006 if (metaEnum.hasValues()) {
1007 m_body += m_state.accumulatorVariableOut + u" = "_s
1008 + QString::number(metaEnum.value(enumMember));
1009 m_body += u";\n"_s;
1010 const QQmlJSRegisterContent &scope = m_state.accumulatorOut().scope();
1011 recordEnumKeyLookup(m_typeResolver->originalContainedType(scope), metaEnum, enumMember);
1012 return;
1013 }
1014
1015 const QQmlJSScope::ConstPtr scopeType = m_state.accumulatorOut().scopeType();
1016
1017 // Otherwise we would have found an enum with values.
1018 Q_ASSERT(!scopeType->isComposite());
1019
1020 const QString enumName = metaEnum.isFlag() ? metaEnum.alias() : metaEnum.name();
1021 if (enumName.isEmpty()) {
1022 if (metaEnum.isFlag() && !metaEnum.name().isEmpty()) {
1023 REJECT(u"qmltypes misses name entry for flag; "
1024 "did you pass the enum type to Q_FLAG instead of the QFlag type?"
1025 "\nType is %1, enum name is %2"_s.arg(scopeType->internalName(), metaEnum.name()));
1026 }
1027 REJECT(u"qmltypes misses name entry for enum"_s);
1028 }
1029 const QString lookup = u"aotContext->getEnumLookup("_s + QString::number(index)
1030 + u", &"_s + m_state.accumulatorVariableOut + u')';
1031 const QString initialization = u"aotContext->initGetEnumLookup("_s
1032 + QString::number(index) + u", "_s + metaObject(scopeType)
1033 + u", \""_s + enumName + u"\", \""_s + enumMember
1034 + u"\")"_s;
1035
1036 // No need to record C++ enums lookup as they are resolved at runtime
1037 generateLookup(lookup, initialization);
1038}
1039
1040void QQmlJSCodeGenerator::generateTypeLookup(int index)
1041{
1042 const QString indexString = QString::number(index);
1043 const QQmlJSRegisterContent accumulatorIn = m_state.registers.value(Accumulator).content;
1044 const QString namespaceString
1045 = accumulatorIn.isImportNamespace()
1046 ? QString::number(accumulatorIn.importNamespace())
1047 : u"QQmlPrivate::AOTCompiledContext::InvalidStringId"_s;
1048
1049 switch (m_state.accumulatorOut().variant()) {
1050 case QQmlJSRegisterContent::Singleton: {
1051 rejectIfNonQObjectOut(u"non-QObject singleton type"_s);
1052 const QString lookup = u"aotContext->loadSingletonLookup("_s + indexString
1053 + u", &"_s + m_state.accumulatorVariableOut + u')';
1054 const QString initialization = u"aotContext->initLoadSingletonLookup("_s + indexString
1055 + u", "_s + namespaceString + u')';
1056 generateLookup(lookup, initialization);
1057 break;
1058 }
1059 case QQmlJSRegisterContent::ModulePrefix:
1060 break;
1061 case QQmlJSRegisterContent::Attachment: {
1062 rejectIfNonQObjectOut(u"non-QObject attached type"_s);
1063 const QString lookup = u"aotContext->loadAttachedLookup("_s + indexString
1064 + u", aotContext->qmlScopeObject, &"_s + m_state.accumulatorVariableOut + u')';
1065 const QString initialization = u"aotContext->initLoadAttachedLookup("_s + indexString
1066 + u", "_s + namespaceString + u", aotContext->qmlScopeObject)"_s;
1067 generateLookup(lookup, initialization);
1068 break;
1069 }
1070 case QQmlJSRegisterContent::Script:
1071 REJECT(u"script lookup"_s);
1072 case QQmlJSRegisterContent::MetaType: {
1073 if (!m_state.accumulatorOut().isStoredIn(m_typeResolver->metaObjectType())) {
1074 // TODO: Can we trigger this somehow?
1075 // It might be impossible, but we better be safe here.
1076 REJECT(u"meta-object stored in different type"_s);
1077 }
1078 const QString lookup = u"aotContext->loadTypeLookup("_s + indexString
1079 + u", &"_s + m_state.accumulatorVariableOut + u')';
1080 const QString initialization = u"aotContext->initLoadTypeLookup("_s + indexString
1081 + u", "_s + namespaceString + u")"_s;
1082 generateLookup(lookup, initialization);
1083 break;
1084 }
1085 default:
1086 Q_UNREACHABLE();
1087 }
1088}
1089
1090void QQmlJSCodeGenerator::generateVariantEqualityComparison(
1091 QQmlJSRegisterContent nonStorableContent, const QString &registerName, bool invert)
1092{
1093 const auto nonStorableType = nonStorableContent.containedType();
1094 QQmlJSScope::ConstPtr comparedType = (nonStorableType == m_typeResolver->nullType())
1095 ? m_typeResolver->nullType()
1096 : m_typeResolver->voidType();
1097
1098 // The common operations for both nulltype and voidtype
1099 m_body += u"if ("_s + registerName
1100 + u".metaType() == QMetaType::fromType<QJSPrimitiveValue>()) {\n"_s
1101 + m_state.accumulatorVariableOut + u" = "_s
1102 + conversion(m_typeResolver->boolType(), m_state.accumulatorOut(),
1103 u"static_cast<const QJSPrimitiveValue *>("_s + registerName
1104 + u".constData())"_s + u"->type() "_s
1105 + (invert ? u"!="_s : u"=="_s)
1106 + (comparedType == m_typeResolver->nullType()
1107 ? u"QJSPrimitiveValue::Null"_s
1108 : u"QJSPrimitiveValue::Undefined"_s))
1109 + u";\n} else if ("_s + registerName
1110 + u".metaType() == QMetaType::fromType<QJSValue>()) {\n"_s
1111 + m_state.accumulatorVariableOut + u" = "_s
1112 + conversion(m_typeResolver->boolType(), m_state.accumulatorOut(),
1113 (invert ? u"!"_s : QString()) + u"static_cast<const QJSValue *>("_s
1114 + registerName + u".constData())"_s + u"->"_s
1115 + (comparedType == m_typeResolver->nullType()
1116 ? u"isNull()"_s
1117 : u"isUndefined()"_s))
1118 + u";\n}"_s;
1119
1120 // Generate nullType specific operations (the case when variant contains QObject * or
1121 // std::nullptr_t)
1122 if (nonStorableType == m_typeResolver->nullType()) {
1123 m_body += u"else if ("_s + registerName
1124 + u".metaType().flags().testFlag(QMetaType::PointerToQObject)) {\n"_s
1125 + m_state.accumulatorVariableOut + u" = "_s
1126 + conversion(m_typeResolver->boolType(), m_state.accumulatorOut(),
1127 u"*static_cast<QObject *const *>("_s + registerName
1128 + u".constData())"_s + (invert ? u"!="_s : u"=="_s)
1129 + u" nullptr"_s)
1130 + u";\n} else if ("_s + registerName
1131 + u".metaType() == QMetaType::fromType<std::nullptr_t>()) {\n"_s
1132 + m_state.accumulatorVariableOut + u" = "_s
1133 + conversion(m_typeResolver->boolType(), m_state.accumulatorOut(),
1134 (invert ? u"false"_s : u"true"_s))
1135 + u";\n}\n"_s;
1136 }
1137
1138 // fallback case (if variant contains a different type, then it is not null or undefined)
1139 m_body += u"else {\n"_s + m_state.accumulatorVariableOut + u" = "_s
1140 + conversion(m_typeResolver->boolType(), m_state.accumulatorOut(),
1141 (invert ? (registerName + u".isValid() ? true : false"_s)
1142 : (registerName + u".isValid() ? false : true"_s)))
1143 + u";\n}"_s;
1144}
1145
1146void QQmlJSCodeGenerator::generateVariantEqualityComparison(
1147 QQmlJSRegisterContent storableContent, const QString &typedRegisterName,
1148 const QString &varRegisterName, bool invert)
1149{
1150 // enumerations are ===-equal to their underlying type and they are stored as such.
1151 // Therefore, use the underlying type right away.
1152 const QQmlJSScope::ConstPtr contained = storableContent.isEnumeration()
1153 ? storableContent.storedType()
1154 : storableContent.containedType();
1155
1156 const QQmlJSScope::ConstPtr boolType = m_typeResolver->boolType();
1157 if (contained->isReferenceType()) {
1158 const QQmlJSScope::ConstPtr comparable = m_typeResolver->qObjectType();
1159 const QString cmpExpr = (invert ? u"!"_s : QString()) + u"(("
1160 + varRegisterName + u".metaType().flags() & QMetaType::PointerToQObject) "_s
1161 + u" && "_s + conversion(storableContent, comparable, typedRegisterName) + u" == "_s
1162 + conversion(m_typeResolver->varType(), comparable, varRegisterName) + u')';
1163
1164 m_body += m_state.accumulatorVariableOut + u" = "_s
1165 + conversion(boolType, m_state.accumulatorOut(), cmpExpr) + u";\n";
1166 return;
1167 }
1168
1169 if (m_typeResolver->isPrimitive(contained)) {
1170 const QQmlJSScope::ConstPtr comparable = m_typeResolver->jsPrimitiveType();
1171 const QString cmpExpr = (invert ? u"!"_s : QString())
1172 + conversion(storableContent, comparable, typedRegisterName)
1173 + u".strictlyEquals("_s
1174 + conversion(m_typeResolver->varType(), comparable, varRegisterName) + u')';
1175
1176 m_body += m_state.accumulatorVariableOut + u" = "_s
1177 + conversion(boolType, m_state.accumulatorOut(), cmpExpr) + u";\n"_s;
1178 return;
1179 }
1180
1181 REJECT(u"comparison of non-primitive, non-object type to var"_s);
1182}
1183
1184void QQmlJSCodeGenerator::generateArrayInitializer(int argc, int argv)
1185{
1186 const QQmlJSScope::ConstPtr stored = m_state.accumulatorOut().storedType();
1187 const QQmlJSScope::ConstPtr element = stored->elementType();
1188 Q_ASSERT(element);
1189
1190 QStringList initializer;
1191 for (int i = 0; i < argc; ++i) {
1192 initializer += convertStored(
1193 registerType(argv + i).storedType(), element,
1194 consumedRegisterVariable(argv + i));
1195 }
1196
1197 m_body += m_state.accumulatorVariableOut + u" = "_s + stored->internalName() + u'{';
1198 m_body += initializer.join(u", "_s);
1199 m_body += u"};\n";
1200}
1201
1202void QQmlJSCodeGenerator::generateWriteBack(int registerIndex)
1203{
1204 QString writeBackRegister = registerVariable(registerIndex);
1205 bool writeBackAffectedBySideEffects = isRegisterAffectedBySideEffects(registerIndex);
1206
1207 for (QQmlJSRegisterContent writeBack = registerType(registerIndex);
1208 !writeBack.storedType()->isReferenceType();) {
1209
1210 switch (writeBack.variant()) {
1211 case QQmlJSRegisterContent::Literal:
1212 case QQmlJSRegisterContent::Operation:
1213 // If the value type or list was produced as a literal or as result
1214 // of an operation (like DefineArray ...), we don't have to write back.
1215 return;
1216 default:
1217 break;
1218 }
1219
1220 if (writeBackAffectedBySideEffects)
1221 REJECT(u"write-back of value affected by side effects"_s);
1222
1223 if (writeBack.isConversion())
1224 REJECT(u"write-back of converted value"_s);
1225
1226 const int lookupIndex = writeBack.resultLookupIndex();
1227
1228 // This is essential for the soundness of the type system.
1229 //
1230 // If a value or a list is returned from a function, we cannot know
1231 // whether it is a copy or a reference. Therefore, we cannot know whether
1232 // we have to write it back and so we have to REJECT any write on it.
1233 //
1234 // Only if we are sure that the value is locally created we can be sure
1235 // we don't have to write it back. In this latter case we could allow
1236 // a modification that doesn't write back.
1237 if (lookupIndex == -1)
1238 REJECT(u"write-back of non-lookup"_s);
1239
1240 const QString writeBackIndexString = QString::number(lookupIndex);
1241
1242 const QQmlJSRegisterContent::ContentVariant variant = writeBack.variant();
1243 if (variant == QQmlJSRegisterContent::Property && isQmlScopeObject(writeBack.scope())) {
1244 const QString lookup = u"aotContext->writeBackScopeObjectPropertyLookup("_s
1245 + writeBackIndexString
1246 + u", "_s + contentPointer(writeBack, writeBackRegister) + u')';
1247 const QString initialization = u"aotContext->initLoadScopeObjectPropertyLookup("_s
1248 + writeBackIndexString + u')';
1249 recordPropertyLookup(m_typeResolver->originalContainedType(writeBack.scope()),
1250 originalProperty(writeBack));
1251 generateLookup(lookup, initialization);
1252 break;
1253 }
1254
1255
1256 QQmlJSRegisterContent outerContent;
1257 QString outerRegister;
1258 bool outerAffectedBySideEffects = false;
1259 for (auto it = m_state.lookups.constBegin(), end = m_state.lookups.constEnd();
1260 it != end; ++it) {
1261 if (it.value().content.resultLookupIndex() == writeBack.baseLookupIndex()) {
1262 outerContent = it.value().content;
1263 outerRegister = lookupVariable(outerContent.resultLookupIndex());
1264 outerAffectedBySideEffects = it.value().affectedBySideEffects;
1265 break;
1266 }
1267 }
1268
1269 // If the lookup doesn't exist, it was killed by state merge.
1270 if (!outerContent.isValid())
1271 REJECT(u"write-back of lookup across jumps or merges."_s);
1272
1273 Q_ASSERT(!outerRegister.isEmpty());
1274
1275 switch (writeBack.variant()) {
1276 case QQmlJSRegisterContent::Property: {
1277 const QQmlJSMetaProperty property = originalProperty(writeBack);
1278 if (writeBack.scopeType()->isReferenceType()) {
1279 const QString lookup = u"aotContext->writeBackObjectLookup("_s
1280 + writeBackIndexString
1281 + u", "_s + outerRegister
1282 + u", "_s + contentPointer(writeBack, writeBackRegister) + u')';
1283
1284 const QString initialization = (m_state.registers[registerIndex].isShadowable
1285 ? u"aotContext->initGetObjectLookupAsVariant("_s
1286 : u"aotContext->initGetObjectLookup("_s)
1287 + writeBackIndexString + u", "_s + outerRegister + u')';
1288 recordPropertyLookup(m_typeResolver->originalContainedType(writeBack.scope()), property);
1289 generateLookup(lookup, initialization);
1290 } else {
1291 const QString valuePointer = contentPointer(outerContent, outerRegister);
1292 const QString lookup = u"aotContext->writeBackValueLookup("_s
1293 + writeBackIndexString
1294 + u", "_s + valuePointer
1295 + u", "_s + contentPointer(writeBack, writeBackRegister) + u')';
1296 const QString initialization = u"aotContext->initGetValueLookup("_s
1297 + writeBackIndexString
1298 + u", "_s + metaObject(writeBack.scopeType()) + u')';
1299 recordPropertyLookup(m_typeResolver->originalContainedType(writeBack.scope()), property);
1300 generateLookup(lookup, initialization);
1301 }
1302 break;
1303 }
1304 default:
1305 REJECT(u"SetLookup on value types (because of missing write-back)"_s);
1306 }
1307
1308 writeBackRegister = outerRegister;
1309 writeBack = outerContent;
1310 writeBackAffectedBySideEffects = outerAffectedBySideEffects;
1311 }
1312}
1313
1314void QQmlJSCodeGenerator::rejectIfNonQObjectOut(const QString &error)
1315{
1316 if (m_state.accumulatorOut().storedType()->accessSemantics()
1317 != QQmlJSScope::AccessSemantics::Reference) {
1318 REJECT(error);
1319 }
1320}
1321
1322void QQmlJSCodeGenerator::rejectIfBadArray()
1323{
1324 const QQmlJSScope::ConstPtr stored = m_state.accumulatorOut().storedType();
1325 if (stored->accessSemantics() != QQmlJSScope::AccessSemantics::Sequence) {
1326 // This rejects any attempt to store the list into a QVariant.
1327 // Therefore, we don't have to adjust the contained type below.
1328
1329 REJECT(u"storing an array in a non-sequence type"_s);
1330 } else if (stored->isListProperty()) {
1331 // We can, technically, generate code for this. But it's dangerous:
1332 //
1333 // const QString storage = m_state.accumulatorVariableOut + u"_storage"_s;
1334 // m_body += stored->internalName() + u"::ListType " + storage
1335 // + u" = {"_s + initializer.join(u", "_s) + u"};\n"_s;
1336 // m_body += m_state.accumulatorVariableOut
1337 // + u" = " + stored->internalName() + u"(nullptr, &"_s + storage + u");\n"_s;
1338
1339 REJECT(u"creating a QQmlListProperty not backed by a property"_s);
1340
1341 }
1342}
1343
1344/*!
1345 * \internal
1346 *
1347 * generates a check for the content pointer to be valid.
1348 * Returns true if the content pointer needs to be retrieved from a QVariant, or
1349 * false if the variable can be used as-is.
1350 */
1351bool QQmlJSCodeGenerator::generateContentPointerCheck(
1352 const QQmlJSScope::ConstPtr &required, QQmlJSRegisterContent actual,
1353 const QString &variable, const QString &errorMessage)
1354{
1355 const QQmlJSScope::ConstPtr scope = required;
1356 const QQmlJSScope::ConstPtr input = actual.containedType();
1357 if (QQmlJSUtils::searchBaseAndExtensionTypes(input,
1358 [&](const QQmlJSScope::ConstPtr &base) { return base == scope; })) {
1359 return false;
1360 }
1361
1362 if (!m_typeResolver->canHold(input, scope)) {
1363 REJECT<bool>(u"lookup of members of %1 in %2"_s
1364 .arg(scope->internalName(), input->internalName()));
1365 }
1366
1367 bool needsVarContentConversion = false;
1368 QString processedErrorMessage;
1369 if (actual.storedType()->isReferenceType()) {
1370 // Since we have verified the type in qqmljstypepropagator.cpp we now know
1371 // that we can only have either null or the actual type here. Therefore,
1372 // it's enough to check the pointer for null.
1373 m_body += u"if ("_s + variable + u" == nullptr) {\n "_s;
1374 processedErrorMessage = errorMessage.arg(u"null");
1375 } else if (actual.isStoredIn(m_typeResolver->varType())) {
1376 // Since we have verified the type in qqmljstypepropagator.cpp we now know
1377 // that we can only have either undefined or the actual type here. Therefore,
1378 // it's enough to check the QVariant for isValid().
1379 m_body += u"if (!"_s + variable + u".isValid()) {\n "_s;
1380 needsVarContentConversion = true;
1381 processedErrorMessage = errorMessage.arg(u"undefined");
1382 } else {
1383 REJECT<bool>(u"retrieving metatype from %1"_s.arg(actual.descriptiveName()));
1384 }
1385
1386 generateSetInstructionPointer();
1387 m_body += u" aotContext->engine->throwError(QJSValue::TypeError, "_s;
1388 m_body += u"QLatin1String(\"%1\"));\n"_s.arg(processedErrorMessage);
1389 generateReturnError();
1390 m_body += u"}\n"_s;
1391 return needsVarContentConversion;
1392}
1393
1394QString QQmlJSCodeGenerator::generateCallConstructor(
1395 const QQmlJSMetaMethod &ctor, const QList<QQmlJSRegisterContent> &argumentTypes,
1396 const QStringList &arguments, const QString &metaType, const QString &metaObject)
1397{
1398 const auto parameterTypes = ctor.parameters();
1399 Q_ASSERT(parameterTypes.length() == argumentTypes.length());
1400
1401 // We need to store the converted arguments in a temporaries because they might not be lvalues.
1402 QStringList argPointers;
1403
1404 QString result = u"[&](){\n"_s;
1405 for (qsizetype i = 0, end = parameterTypes.length(); i < end; ++i) {
1406 const QQmlJSRegisterContent argumentType = argumentTypes[i];
1407 const QQmlJSScope::ConstPtr parameterType = parameterTypes[i].type();
1408 const QString argument = arguments[i];
1409 const QString arg = u"arg"_s + QString::number(i);
1410
1411 result += u" auto "_s + arg + u" = "_s;
1412 if (argumentType.contains(parameterType)) {
1413 result += argument;
1414 argPointers.append(contentPointer(argumentType, arg));
1415 } else {
1416 const QQmlJSRegisterContent parameterTypeConversion
1417 = m_pool->storedIn(
1418 m_typeResolver->convert(argumentType, parameterType),
1419 m_typeResolver->genericType(parameterType));
1420 result += conversion(argumentType, parameterTypeConversion, argument);
1421 argPointers.append(contentPointer(parameterTypeConversion, arg));
1422 }
1423 result += u";\n"_s;
1424 }
1425
1426 result += u" void *args[] = {"_s + argPointers.join(u',') + u"};\n"_s;
1427 result += u" return aotContext->constructValueType("_s + metaType + u", "_s + metaObject
1428 + u", "_s + QString::number(int(ctor.constructorIndex())) + u", args);\n"_s;
1429
1430 return result + u"}()"_s;
1431}
1432
1434 const QQmlJSTypeResolver *typeResolver, const QQmlJSRegisterContent &baseType)
1435{
1436 const QQmlJSScope::ConstPtr contained = baseType.containedType();
1437 switch (contained->accessSemantics()) {
1440 // References and namespaces can't be affected by side effects.
1441 return false;
1443 // Value types can have inner objects, and we may have pre-created them where the
1444 // interpreter keeps them in JavaScript object form for longer.
1445 // TODO: We can probably improve here.
1446 return !typeResolver->isPrimitive(contained);
1448 // List properties are never affected by side effects
1449 if (contained->isListProperty())
1450 return false;
1451
1452 switch (baseType.variant()) {
1453 case QQmlJSRegisterContent::Operation:
1454 case QQmlJSRegisterContent::Literal: {
1455 // Stack-created lists of primitives and pointers can't be affected by side effects
1456 const QQmlJSScope::ConstPtr elementContained = contained->elementType();
1457 return !elementContained->isReferenceType()
1458 && !typeResolver->isPrimitive(elementContained);
1459 }
1460 default:
1461 return true;
1462 }
1463 }
1464 }
1465 return true;
1466}
1467
1468bool QQmlJSCodeGenerator::isRegisterAffectedBySideEffects(int registerIndex)
1469{
1470 if (!m_state.isRegisterAffectedBySideEffects(registerIndex))
1471 return false;
1472
1473 QQmlJSRegisterContent baseType = registerType(registerIndex);
1474 if (baseType.isConversion()) {
1475 // A conversion can be affected by side effects if any of its origins can.
1476 // Conversions are unrolled on creation, so we don't have to recurse.
1477
1478 const auto &origins = baseType.conversionOrigins();
1479 for (QQmlJSRegisterContent origin : origins) {
1480 if (canTypeBeAffectedBySideEffects(m_typeResolver, m_typeResolver->original(origin)))
1481 return true;
1482 }
1483
1484 return false;
1485 }
1486
1487 return canTypeBeAffectedBySideEffects(m_typeResolver, m_typeResolver->original(baseType));
1488}
1489
1490QString QQmlJSCodeGenerator::resolveValueTypeContentPointer(
1491 const QQmlJSScope::ConstPtr &required, QQmlJSRegisterContent actual,
1492 const QString &variable, const QString &errorMessage)
1493{
1494 if (generateContentPointerCheck(required, actual, variable, errorMessage))
1495 return variable + u".data()"_s;
1496 return contentPointer(actual, variable);
1497}
1498
1499QString QQmlJSCodeGenerator::resolveQObjectPointer(
1500 const QQmlJSScope::ConstPtr &required, QQmlJSRegisterContent actual,
1501 const QString &variable, const QString &errorMessage)
1502{
1503 if (generateContentPointerCheck(required, actual, variable, errorMessage))
1504 return u"*static_cast<QObject *const *>("_s + variable + u".constData())"_s;
1505 return variable;
1506}
1507
1508void QQmlJSCodeGenerator::generate_GetLookup(int index)
1509{
1510 INJECT_TRACE_INFO(generate_GetLookup);
1511 generate_GetLookupHelper(index);
1512}
1513
1514QString QQmlJSCodeGenerator::generateVariantMapGetLookup(
1515 const QString &map, const int nameIndex)
1516{
1517 const QString mapLookup = map
1518 + u"["_s + QQmlJSUtils::toLiteral(m_jsUnitGenerator->lookupName(nameIndex)) + u"]"_s;
1519
1520 return m_state.accumulatorVariableOut + u" = "_s
1521 + conversion(m_typeResolver->varType(), m_state.accumulatorOut(), mapLookup)
1522 + u";\n"_s;
1523}
1524
1525QString QQmlJSCodeGenerator::generateVariantMapSetLookup(
1526 const QString &map, const int nameIndex,
1527 const QQmlJSScope::ConstPtr &property, const QString &variableIn)
1528{
1529 const QString mapLookup = map
1530 + u"["_s + QQmlJSUtils::toLiteral(m_jsUnitGenerator->lookupName(nameIndex)) + u"]"_s;
1531
1532 return mapLookup + u" = "_s
1533 + conversion(property, m_typeResolver->varType(), variableIn)
1534 + u";\n"_s;
1535}
1536
1537void QQmlJSCodeGenerator::generate_GetLookupHelper(int index)
1538{
1539 if (m_state.accumulatorOut().isMethod())
1540 REJECT(u"lookup of function property."_s);
1541
1542 if (m_state.accumulatorOut().scope().contains(m_typeResolver->mathObject())) {
1543 QString name = m_jsUnitGenerator->lookupName(index);
1544
1545 double value{};
1546 if (name == u"E") {
1547 value = std::exp(1.0);
1548 } else if (name == u"LN10") {
1549 value = log(10.0);
1550 } else if (name == u"LN2") {
1551 value = log(2.0);
1552 } else if (name == u"LOG10E") {
1553 value = log10(std::exp(1.0));
1554 } else if (name == u"LOG2E") {
1555 value = log2(std::exp(1.0));
1556 } else if (name == u"PI") {
1557 value = 3.14159265358979323846;
1558 } else if (name == u"SQRT1_2") {
1559 value = std::sqrt(0.5);
1560 } else if (name == u"SQRT2") {
1561 value = std::sqrt(2.0);
1562 } else {
1563 Q_UNREACHABLE();
1564 }
1565
1566 m_body += m_state.accumulatorVariableOut + u" = "_s
1567 + conversion(m_typeResolver->realType(), m_state.accumulatorOut(), toNumericString(value))
1568 + u";\n"_s;
1569 return;
1570 }
1571
1572 if (m_state.accumulatorOut().isImportNamespace()) {
1573 Q_ASSERT(m_state.accumulatorOut().variant() == QQmlJSRegisterContent::ModulePrefix);
1574 // If we have an object module prefix, we need to pass through the original object.
1575 if (m_state.accumulatorVariableIn != m_state.accumulatorVariableOut) {
1576 m_body += m_state.accumulatorVariableOut + u" = "_s
1577 + conversion(m_state.accumulatorIn(), m_state.accumulatorOut(),
1578 m_state.accumulatorVariableIn)
1579 + u";\n"_s;
1580 }
1581 return;
1582 }
1583
1584 AccumulatorConverter registers(this);
1585
1586 if (m_state.accumulatorOut().isEnumeration()) {
1587 generateEnumLookup(index);
1588 return;
1589 }
1590
1591 const QString indexString = QString::number(index);
1592 const QString namespaceString = m_state.accumulatorIn().isImportNamespace()
1593 ? QString::number(m_state.accumulatorIn().importNamespace())
1594 : u"QQmlPrivate::AOTCompiledContext::InvalidStringId"_s;
1595 const auto accumulatorIn = m_state.accumulatorIn();
1596 const QQmlJSRegisterContent scope = m_state.accumulatorOut().scope();
1597 const QQmlJSRegisterContent originalScope
1598 = scope.original().isValid() ? scope.original() : scope;
1599 const bool isReferenceType = originalScope.containedType()->isReferenceType();
1600
1601 switch (m_state.accumulatorOut().variant()) {
1602 case QQmlJSRegisterContent::Attachment: {
1603 if (isQmlScopeObject(m_state.accumulatorOut().attachee())) {
1604 generateTypeLookup(index);
1605 return;
1606 }
1607 if (!isReferenceType) {
1608 // This can happen on incomplete type information. We contextually know that the
1609 // type must be a QObject, but we cannot construct the inheritance chain. Then we
1610 // store it in a generic type. Technically we could even convert it to QObject*, but
1611 // that would be expensive.
1612 REJECT(u"attached object for non-QObject type"_s);
1613 }
1614
1615 if (!m_state.accumulatorIn().storedType()->isReferenceType()) {
1616 // This can happen if we retroactively determine that the property might not be
1617 // what we think it is (ie, it can be shadowed).
1618 REJECT(u"attached object of potentially non-QObject base"_s);
1619 }
1620
1621 rejectIfNonQObjectOut(u"non-QObject attached type"_s);
1622
1623 const QString lookup = u"aotContext->loadAttachedLookup("_s + indexString
1624 + u", "_s + m_state.accumulatorVariableIn
1625 + u", &"_s + m_state.accumulatorVariableOut + u')';
1626 const QString initialization = u"aotContext->initLoadAttachedLookup("_s
1627 + indexString + u", "_s + namespaceString + u", "_s
1628 + m_state.accumulatorVariableIn + u')';
1629 generateLookup(lookup, initialization);
1630 return;
1631 }
1632 case QQmlJSRegisterContent::Singleton:
1633 case QQmlJSRegisterContent::Script:
1634 case QQmlJSRegisterContent::MetaType: {
1635 generateTypeLookup(index);
1636 return;
1637 }
1638 default:
1639 break;
1640 }
1641
1642 Q_ASSERT(m_state.accumulatorOut().isProperty());
1643
1644 if (accumulatorIn.isStoredIn(m_typeResolver->jsValueType())) {
1645 REJECT(u"lookup in QJSValue"_s);
1646 } else if (isReferenceType) {
1647 const QString inputPointer = resolveQObjectPointer(
1648 scope.containedType(), accumulatorIn, m_state.accumulatorVariableIn,
1649 u"Cannot read property '%1' of %2"_s.arg(
1650 m_jsUnitGenerator->lookupName(index)));
1651 const QString lookup = u"aotContext->getObjectLookup("_s + indexString
1652 + u", "_s + inputPointer + u", "_s
1653 + contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut) + u')';
1654 const QString initialization = (m_state.isShadowable()
1655 ? u"aotContext->initGetObjectLookupAsVariant("_s
1656 : u"aotContext->initGetObjectLookup("_s)
1657 + indexString + u", "_s + inputPointer + u')';
1658 const QString preparation = getLookupPreparation(
1659 m_state.accumulatorOut(), m_state.accumulatorVariableOut, index);
1660 recordPropertyLookup(m_typeResolver->originalContainedType(m_state.accumulatorOut().scope()),
1661 originalProperty(m_state.accumulatorOut()));
1662 generateLookup(lookup, initialization, preparation);
1663 } else if ((originalScope.containedType()->accessSemantics()
1664 == QQmlJSScope::AccessSemantics::Sequence
1665 || originalScope.contains(m_typeResolver->stringType()))
1666 && m_jsUnitGenerator->lookupName(index) == u"length"_s) {
1667 const QQmlJSScope::ConstPtr stored = accumulatorIn.storedType();
1668 if (stored->isListProperty()) {
1669 m_body += m_state.accumulatorVariableOut + u" = "_s;
1670 m_body += conversion(
1671 originalType(m_state.accumulatorOut()),
1672 m_state.accumulatorOut(),
1673 m_state.accumulatorVariableIn + u".count("_s + u'&'
1674 + m_state.accumulatorVariableIn + u')');
1675 m_body += u";\n"_s;
1676 } else if (stored->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
1677 || stored == m_typeResolver->stringType()) {
1678 m_body += m_state.accumulatorVariableOut + u" = "_s
1679 + conversion(originalType(m_state.accumulatorOut()),
1680 m_state.accumulatorOut(),
1681 m_state.accumulatorVariableIn + u".length()"_s)
1682 + u";\n"_s;
1683 } else if (originalScope.contains(m_typeResolver->stringType())) {
1684 m_body += m_state.accumulatorVariableOut + u" = "_s
1685 + conversion(
1686 m_typeResolver->sizeType(), m_state.accumulatorOut(),
1687 conversion(m_state.accumulatorIn(), m_typeResolver->stringType(),
1688 m_state.accumulatorVariableIn)
1689 + u".length()"_s)
1690 + u";\n"_s;
1691 } else {
1692 REJECT(u"access to 'length' property of sequence wrapped in non-sequence"_s);
1693 }
1694 } else if (accumulatorIn.isStoredIn(m_typeResolver->variantMapType())) {
1695 m_body += generateVariantMapGetLookup(m_state.accumulatorVariableIn, index);
1696 } else {
1697 if (m_state.isRegisterAffectedBySideEffects(Accumulator))
1698 REJECT(u"reading from a value that's potentially affected by side effects"_s);
1699
1700 const QString inputContentPointer = resolveValueTypeContentPointer(
1701 scope.containedType(), accumulatorIn, m_state.accumulatorVariableIn,
1702 u"Cannot read property '%1' of %2"_s.arg(
1703 m_jsUnitGenerator->lookupName(index)));
1704
1705 if (scope.contains(m_typeResolver->variantMapType())) {
1706 m_body += generateVariantMapGetLookup(
1707 u"(*static_cast<const QVariantMap *>("_s
1708 + inputContentPointer + u"))"_s, index);
1709 return;
1710 }
1711
1712 const QString lookup = u"aotContext->getValueLookup("_s + indexString
1713 + u", "_s + inputContentPointer
1714 + u", "_s + contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut)
1715 + u')';
1716 const QString initialization = u"aotContext->initGetValueLookup("_s
1717 + indexString + u", "_s
1718 + metaObject(scope.containedType()) + u')';
1719 const QString preparation = getLookupPreparation(
1720 m_state.accumulatorOut(), m_state.accumulatorVariableOut, index);
1721 recordPropertyLookup(m_typeResolver->originalContainedType(scope),
1722 originalProperty(m_state.accumulatorOut()));
1723 generateLookup(lookup, initialization, preparation);
1724 }
1725}
1726
1727void QQmlJSCodeGenerator::generate_GetOptionalLookup(int index, int offset)
1728{
1729 INJECT_TRACE_INFO(generate_GetOptionalLookup);
1730
1731 const QQmlJSRegisterContent accumulatorIn = m_state.accumulatorIn();
1732 QString accumulatorVarIn = m_state.accumulatorVariableIn;
1733
1734 const auto &annotation = m_annotations[currentInstructionOffset()];
1735 if (accumulatorIn.storedType()->isReferenceType()) {
1736 m_body += u"if (!%1)\n"_s.arg(accumulatorVarIn);
1737 generateJumpCodeWithTypeConversions(offset);
1738 } else if (accumulatorIn.isStoredIn(m_typeResolver->varType())) {
1739 m_body += u"if (!%1.isValid() || ((%1.metaType().flags() & QMetaType::PointerToQObject) "
1740 "&& %1.value<QObject *>() == nullptr))\n"_s.arg(accumulatorVarIn);
1741 generateJumpCodeWithTypeConversions(offset);
1742 } else if (accumulatorIn.isStoredIn(m_typeResolver->jsPrimitiveType())) {
1743 m_body += u"if (%1.equals(QJSPrimitiveUndefined()) "
1744 "|| %1.equals(QJSPrimitiveNull()))\n"_s.arg(accumulatorVarIn);
1745 generateJumpCodeWithTypeConversions(offset);
1746 } else if (annotation.changedRegisterIndex == Accumulator
1747 && annotation.changedRegister.variant() == QQmlJSRegisterContent::Enum) {
1748 // Nothing
1749 } else if (accumulatorIn.isStoredIn(m_typeResolver->jsValueType())) {
1750 m_body += u"if (%1.isNull() || %1.isUndefined())\n"_s.arg(accumulatorVarIn);
1751 generateJumpCodeWithTypeConversions(offset);
1752 } else if (!m_typeResolver->canHoldUndefined(accumulatorIn.storage())) {
1753 // The base cannot hold undefined and isn't a reference type, generate a regular get lookup
1754 } else {
1755 Q_UNREACHABLE(); // No other accumulatorIn stored types should be possible
1756 }
1757
1758 generate_GetLookupHelper(index);
1759}
1760
1761void QQmlJSCodeGenerator::generate_StoreProperty(int nameIndex, int baseReg)
1762{
1763 Q_UNUSED(nameIndex)
1764 Q_UNUSED(baseReg)
1765 REJECT(u"StoreProperty"_s);
1766}
1767
1768// TODO: This shouldn't be necessary. If the content can be stored directly, then it should
1769// be stored and used directly. If it cannot be stored directly, it should be stored
1770// as QVariant, but then we cannot dereference the content pointer either.
1771static QString derefContentPointer(const QString &contentPointer)
1772{
1773 Q_ASSERT(contentPointer.startsWith(u'&') || contentPointer[0].isLetterOrNumber());
1774 return contentPointer.startsWith(u'&') ? contentPointer.mid(1) : (u'*' + contentPointer);
1775}
1776
1777void QQmlJSCodeGenerator::generate_SetLookup(int index, int baseReg)
1778{
1779 INJECT_TRACE_INFO(generate_SetLookup);
1780
1781 const QString indexString = QString::number(index);
1782 const QQmlJSScope::ConstPtr valueType = m_state.accumulatorIn().storedType();
1783 const QQmlJSRegisterContent property = m_state.readAccumulator();
1784 Q_ASSERT(property.isConversion());
1785 const QQmlJSRegisterContent original
1786 = m_typeResolver->original(property.conversionResultScope());
1787 const QQmlJSScope::ConstPtr originalScope = original.containedType();
1788
1789 const QString &propertyName = m_jsUnitGenerator->lookupName(index);
1790 if (property.storedType().isNull()) {
1791 REJECT(u"SetLookup. Could not find property " + propertyName + u" on type "
1792 + originalScope->internalName());
1793 }
1794
1795 const QString object = registerVariable(baseReg);
1796 m_body += u"{\n"_s;
1797 QString variableIn;
1798 if (!m_state.accumulatorIn().contains(property.containedType())) {
1799 m_body += property.storedType()->augmentedInternalName() + u" converted = "_s
1800 + conversion(m_state.accumulatorIn(), property, consumedAccumulatorVariableIn())
1801 + u";\n"_s;
1802 variableIn = contentPointer(property, u"converted"_s);
1803 } else {
1804 variableIn = contentPointer(property, m_state.accumulatorVariableIn);
1805 }
1806
1807 switch (originalScope->accessSemantics()) {
1808 case QQmlJSScope::AccessSemantics::Reference: {
1809 const QString basePointer = resolveQObjectPointer(
1810 originalScope, registerType(baseReg), object,
1811 u"TypeError: Value is %1 and could not be converted to an object"_s);
1812
1813 const QString lookup = u"aotContext->setObjectLookup("_s + indexString
1814 + u", "_s + basePointer + u", "_s + variableIn + u')';
1815
1816 // We use the asVariant lookup also for non-shadowable properties if the input can hold
1817 // undefined since that may be a reset. See QQmlJSTypePropagator::generate_StoreProperty().
1818 const QString initialization
1819 = (property.contains(m_typeResolver->varType())
1820 ? u"aotContext->initSetObjectLookupAsVariant("_s
1821 : u"aotContext->initSetObjectLookup("_s)
1822 + indexString + u", "_s + basePointer + u')';
1823 recordPropertyLookup(originalScope, originalScope->property(propertyName));
1824 generateLookup(lookup, initialization);
1825 break;
1826 }
1827 case QQmlJSScope::AccessSemantics::Sequence: {
1828 if (propertyName != u"length"_s)
1829 REJECT(u"setting non-length property on a sequence type"_s);
1830
1831 if (!originalScope->isListProperty())
1832 REJECT(u"resizing sequence types (because of missing write-back)"_s);
1833
1834 // We can resize without write back on a list property because it's actually a reference.
1835 m_body += u"const int begin = "_s + object + u".count(&" + object + u");\n"_s;
1836 m_body += u"const int end = "_s + derefContentPointer(variableIn) + u";\n"_s;
1837 m_body += u"for (int i = begin; i < end; ++i)\n"_s;
1838 m_body += u" "_s + object + u".append(&"_s + object + u", nullptr);\n"_s;
1839 m_body += u"for (int i = begin; i > end; --i)\n"_s;
1840 m_body += u" "_s + object + u".removeLast(&"_s + object + u')'
1841 + u";\n"_s;
1842 break;
1843 }
1844 case QQmlJSScope::AccessSemantics::Value: {
1845 const QQmlJSRegisterContent base = registerType(baseReg);
1846 if (base.isStoredIn(m_typeResolver->variantMapType())) {
1847 m_body += generateVariantMapSetLookup(
1848 registerVariable(baseReg), index, property.storedType(),
1849 derefContentPointer(variableIn));
1850 generateWriteBack(baseReg);
1851 break;
1852 }
1853 const QString baseContentPointer = resolveValueTypeContentPointer(
1854 originalScope, base, object,
1855 u"TypeError: Value is %1 and could not be converted to an object"_s);
1856
1857 if (original.contains(m_typeResolver->variantMapType())) {
1858 m_body += generateVariantMapSetLookup(
1859 u"(*static_cast<const QVariantMap *>("_s
1860 + baseContentPointer + u"))"_s, index, property.storedType(),
1861 derefContentPointer(variableIn));
1862 generateWriteBack(baseReg);
1863 break;
1864 }
1865
1866 const QString lookup = u"aotContext->setValueLookup("_s + indexString
1867 + u", "_s + baseContentPointer
1868 + u", "_s + variableIn + u')';
1869
1870 // We use the asVariant lookup also for non-shadowable properties if the input can hold
1871 // undefined since that may be a reset. See QQmlJSTypePropagator::generate_StoreProperty().
1872 const QString initialization
1873 = (property.contains(m_typeResolver->varType())
1874 ? u"aotContext->initSetValueLookupAsVariant("_s
1875 : u"aotContext->initSetValueLookup("_s)
1876 + indexString + u", "_s + metaObject(originalScope) + u')';
1877 recordPropertyLookup(m_typeResolver->originalContainedType(base),
1878 originalScope->property(propertyName));
1879 generateLookup(lookup, initialization);
1880 generateWriteBack(baseReg);
1881
1882 break;
1883 }
1884 case QQmlJSScope::AccessSemantics::None:
1885 Q_UNREACHABLE();
1886 break;
1887 }
1888
1889 m_body += u"}\n"_s;
1890}
1891
1892void QQmlJSCodeGenerator::generate_LoadSuperProperty(int property)
1893{
1894 Q_UNUSED(property)
1896}
1897
1898void QQmlJSCodeGenerator::generate_StoreSuperProperty(int property)
1899{
1900 Q_UNUSED(property)
1902}
1903
1904void QQmlJSCodeGenerator::generate_Yield()
1905{
1907}
1908
1909void QQmlJSCodeGenerator::generate_YieldStar()
1910{
1912}
1913
1914void QQmlJSCodeGenerator::generate_Resume(int)
1915{
1917}
1918
1919QString QQmlJSCodeGenerator::initAndCall(
1920 int argc, int argv, const QString &callMethodTemplate, const QString &initMethodTemplate,
1921 QString *outVar)
1922{
1923 QString args;
1924
1925 if (m_state.changedRegisterIndex() == InvalidRegister ||
1926 m_state.accumulatorOut().contains(m_typeResolver->voidType())) {
1927 args = u"nullptr"_s;
1928 } else {
1929 *outVar = u"callResult"_s;
1930 const QQmlJSScope::ConstPtr outType = m_state.accumulatorOut().storedType();
1931 m_body += outType->augmentedInternalName() + u' ' + *outVar;
1932 m_body += u";\n";
1933
1934 args = contentPointer(m_state.accumulatorOut(), *outVar);
1935 }
1936
1937 // We may need to convert the arguments to the function call so that they match what the
1938 // function expects. They are passed as void* after all. We try to convert them where they
1939 // are created, but if they are read as different types in multiple places, we can't.
1940 QString argumentPreparation;
1941 for (int i = 0; i < argc; ++i) {
1942 if (isRegisterAffectedBySideEffects(argv + i))
1943 REJECT<QString>(u"calling method with argument affected by side effects"_s);
1944 const QQmlJSRegisterContent content = registerType(argv + i);
1945 const QQmlJSRegisterContent read = m_state.readRegister(argv + i);
1946 if (read.contains(content.containedType())) {
1947 args += u", "_s + contentPointer(read, registerVariable(argv + i));
1948 } else {
1949 const QString var = u"arg"_s + QString::number(i);
1950 argumentPreparation +=
1951 u" "_s + read.storedType()->augmentedInternalName() + u' ' + var + u" = "_s
1952 + conversion(content, read, consumedRegisterVariable(argv + i)) + u";\n";
1953 args += u", "_s + contentPointer(read, var);
1954 }
1955 }
1956
1957 QString initMethod;
1958
1959 if (m_state.isShadowable()) {
1960 initMethod = initMethodTemplate;
1961 } else {
1962 const QQmlJSMetaMethod method = m_state.accumulatorOut().methodCall();
1963 Q_ASSERT(!method.isConstructor());
1964
1965 initMethod = initMethodTemplate.arg(int(method.methodIndex()));
1966 }
1967
1968 return u"const auto doCall = [&]() {\n"_s
1969 + argumentPreparation
1970 + u" void *args[] = {" + args + u"};\n"_s
1971 + u" return aotContext->"_s + callMethodTemplate.arg(u"args"_s).arg(argc) + u";\n"
1972 + u"};\n"_s
1973 + u"const auto doInit = [&]() {\n"_s
1974 + u" aotContext->"_s + initMethod + u";\n"
1975 + u"};\n"_s;
1976}
1977
1978void QQmlJSCodeGenerator::generateMoveOutVarAfterCall(const QString &outVar)
1979{
1980 if (m_state.accumulatorVariableOut.isEmpty() || outVar.isEmpty())
1981 return;
1982
1983 m_body += m_state.accumulatorVariableOut + u" = "_s + u"std::move(" + outVar + u");\n";
1984 m_body += u"aotContext->setImplicitDestructible("_s
1985 + m_state.accumulatorVariableOut + u");\n"_s;
1986}
1987
1988void QQmlJSCodeGenerator::generate_CallValue(int name, int argc, int argv)
1989{
1990 Q_UNUSED(name)
1991 Q_UNUSED(argc)
1992 Q_UNUSED(argv)
1994}
1995
1996void QQmlJSCodeGenerator::generate_CallWithReceiver(int name, int thisObject, int argc, int argv)
1997{
1998 Q_UNUSED(name)
1999 Q_UNUSED(thisObject)
2000 Q_UNUSED(argc)
2001 Q_UNUSED(argv)
2003}
2004
2005void QQmlJSCodeGenerator::generate_CallProperty(int nameIndex, int baseReg, int argc, int argv)
2006{
2007 Q_UNUSED(nameIndex);
2008 Q_UNUSED(baseReg);
2009 Q_UNUSED(argc);
2010 Q_UNUSED(argv);
2011 REJECT(u"CallProperty"_s);
2012}
2013
2014bool QQmlJSCodeGenerator::inlineStringMethod(const QString &name, int base, int argc, int argv)
2015{
2016 if (name != u"arg"_s || argc != 1)
2017 return false;
2018
2019 const auto arg = [&](const QQmlJSScope::ConstPtr &type) {
2020 return convertStored(registerType(argv).storedType(), type, consumedRegisterVariable(argv));
2021 };
2022
2023 const auto ret = [&](const QString &arg) {
2024 const QString expression = convertStored(
2025 registerType(base).storedType(), m_typeResolver->stringType(),
2026 consumedRegisterVariable(base)) + u".arg("_s + arg + u')';
2027 return conversion(
2028 m_typeResolver->stringType(), m_state.accumulatorOut(), expression);
2029 };
2030
2031 const QQmlJSRegisterContent input = m_state.readRegister(argv);
2032 m_body += m_state.accumulatorVariableOut + u" = "_s;
2033
2034 if (m_typeResolver->isNumeric(input))
2035 m_body += ret(arg(input.containedType()));
2036 else if (input.contains(m_typeResolver->boolType()))
2037 m_body += ret(arg(m_typeResolver->boolType()));
2038 else
2039 m_body += ret(arg(m_typeResolver->stringType()));
2040 m_body += u";\n"_s;
2041 return true;
2042}
2043
2044bool QQmlJSCodeGenerator::inlineTranslateMethod(const QString &name, int argc, int argv)
2045{
2046 addInclude(u"QtCore/qcoreapplication.h"_s);
2047
2048 const auto arg = [&](int i, const QQmlJSScope::ConstPtr &type) {
2049 Q_ASSERT(i < argc);
2050 return convertStored(registerType(argv + i).storedType(), type,
2051 consumedRegisterVariable(argv + i));
2052 };
2053
2054 const auto stringArg = [&](int i) {
2055 return i < argc
2056 ? (arg(i, m_typeResolver->stringType()) + u".toUtf8().constData()"_s)
2057 : u"\"\""_s;
2058 };
2059
2060 const auto intArg = [&](int i) {
2061 return i < argc ? arg(i, m_typeResolver->int32Type()) : u"-1"_s;
2062 };
2063
2064 const auto stringRet = [&](const QString &expression) {
2065 return conversion(
2066 m_typeResolver->stringType(), m_state.accumulatorOut(), expression);
2067 };
2068
2069 const auto capture = [&]() {
2070 m_body += u"aotContext->captureTranslation();\n"_s;
2071 };
2072
2073 if (name == u"QT_TRID_NOOP"_s || name == u"QT_TR_NOOP"_s) {
2074 Q_ASSERT(argc > 0);
2075 m_body += m_state.accumulatorVariableOut + u" = "_s
2076 + stringRet(arg(0, m_typeResolver->stringType())) + u";\n"_s;
2077 return true;
2078 }
2079
2080 if (name == u"QT_TRANSLATE_NOOP"_s) {
2081 Q_ASSERT(argc > 1);
2082 m_body += m_state.accumulatorVariableOut + u" = "_s
2083 + stringRet(arg(1, m_typeResolver->stringType())) + u";\n"_s;
2084 return true;
2085 }
2086
2087 if (name == u"qsTrId"_s) {
2088 capture();
2089 // We inline qtTrId() here because in the !QT_CONFIG(translation) case it's unavailable.
2090 // QCoreApplication::translate() is always available in some primitive form.
2091 // Also, this saves a function call.
2092 m_body += m_state.accumulatorVariableOut + u" = "_s
2093 + stringRet(u"QCoreApplication::translate(nullptr, "_s + stringArg(0) +
2094 u", nullptr, "_s + intArg(1) + u")"_s) + u";\n"_s;
2095 return true;
2096 }
2097
2098 if (name == u"qsTr"_s) {
2099 capture();
2100 m_body += m_state.accumulatorVariableOut + u" = "_s
2101 + stringRet(u"QCoreApplication::translate("_s
2102 + u"aotContext->translationContext().toUtf8().constData(), "_s
2103 + stringArg(0) + u", "_s + stringArg(1) + u", "_s
2104 + intArg(2) + u")"_s) + u";\n"_s;
2105 return true;
2106 }
2107
2108 if (name == u"qsTranslate"_s) {
2109 capture();
2110 m_body += m_state.accumulatorVariableOut + u" = "_s
2111 + stringRet(u"QCoreApplication::translate("_s
2112 + stringArg(0) + u", "_s + stringArg(1) + u", "_s
2113 + stringArg(2) + u", "_s + intArg(3) + u")"_s) + u";\n"_s;
2114 return true;
2115 }
2116
2117 return false;
2118}
2119
2120static QString maxExpression(int argc)
2121{
2122 Q_ASSERT_X(argc >= 2, Q_FUNC_INFO, "max() expects at least two arguments.");
2123
2124 QString expression =
2125 u"[&]() { \nauto tmpMax = (qIsNull(arg2) && qIsNull(arg1) && std::copysign(1.0, arg2) == 1) ? arg2 : ((arg2 > arg1 || std::isnan(arg2)) ? arg2 : arg1);\n"_s;
2126 for (int i = 2; i < argc; i++) {
2127 expression +=
2128 "\ttmpMax = (qIsNull(%1) && qIsNull(tmpMax) && std::copysign(1.0, %1) == 1) ? arg2 : ((%1 > tmpMax || std::isnan(%1)) ? %1 : tmpMax);\n"_L1
2129 .arg("arg"_L1 + QString::number(i + 1));
2130 }
2131 expression += "return tmpMax;\n}()"_L1;
2132
2133 return expression;
2134}
2135
2136static QString minExpression(int argc)
2137{
2138 Q_ASSERT_X(argc >= 2, Q_FUNC_INFO, "min() expects at least two arguments.");
2139
2140 QString expression =
2141 u"[&]() { \nauto tmpMin = (qIsNull(arg2) && qIsNull(arg1) && std::copysign(1.0, arg2) == -1) ? arg2 : ((arg2 < arg1 || std::isnan(arg2)) ? arg2 : arg1);\n"_s;
2142 for (int i = 2; i < argc; i++) {
2143 expression +=
2144 "tmpMin = (qIsNull(%1) && qIsNull(tmpMin) && std::copysign(1.0, %1) == -1) ? arg2 : ((%1 < tmpMin || std::isnan(%1)) ? %1 : tmpMin);\n"_L1
2145 .arg("arg"_L1 + QString::number(i + 1));
2146 }
2147 expression += "return tmpMin;\n}()"_L1;
2148
2149 return expression;
2150}
2151
2152bool QQmlJSCodeGenerator::inlineMathMethod(const QString &name, int argc, int argv)
2153{
2154 addInclude(u"cmath"_s);
2155 addInclude(u"limits"_s);
2156 addInclude(u"QtCore/qalgorithms.h"_s);
2157 addInclude(u"QtCore/qrandom.h"_s);
2158 addInclude(u"QtQml/qjsprimitivevalue.h"_s);
2159
2160 // If the result is not stored, we don't need to generate any code. All the math methods are
2161 // conceptually pure functions.
2162 if (m_state.changedRegisterIndex() != Accumulator)
2163 return true;
2164
2165 m_body += u"{\n"_s;
2166 for (int i = 0; i < argc; ++i) {
2167 m_body += u"const double arg%1 = "_s.arg(i + 1) + convertStored(
2168 registerType(argv + i).storedType(),
2169 m_typeResolver->realType(), consumedRegisterVariable(argv + i))
2170 + u";\n"_s;
2171 }
2172
2173 const QString qNaN = u"std::numeric_limits<double>::quiet_NaN()"_s;
2174 const QString inf = u"std::numeric_limits<double>::infinity()"_s;
2175 m_body += m_state.accumulatorVariableOut + u" = "_s;
2176
2177 QString expression;
2178
2179 if (name == u"abs" && argc == 1) {
2180 expression = u"(qIsNull(arg1) ? 0 : (arg1 < 0.0 ? -arg1 : arg1))"_s;
2181 } else if (name == u"acos"_s && argc == 1) {
2182 expression = u"arg1 > 1.0 ? %1 : std::acos(arg1)"_s.arg(qNaN);
2183 } else if (name == u"acosh"_s && argc == 1) {
2184 expression = u"arg1 < 1.0 ? %1 : std::acosh(arg1)"_s.arg(qNaN);
2185 } else if (name == u"asin"_s && argc == 1) {
2186 expression = u"arg1 > 1.0 ? %1 : std::asin(arg1)"_s.arg(qNaN);
2187 } else if (name == u"asinh"_s && argc == 1) {
2188 expression = u"qIsNull(arg1) ? arg1 : std::asinh(arg1)"_s;
2189 } else if (name == u"atan"_s && argc == 1) {
2190 expression = u"qIsNull(arg1) ? arg1 : std::atan(arg1)"_s;
2191 } else if (name == u"atanh"_s && argc == 1) {
2192 expression = u"qIsNull(arg1) ? arg1 : std::atanh(arg1)"_s;
2193 } else if (name == u"atan2"_s) {
2194 // TODO: complicated
2195 return false;
2196 } else if (name == u"cbrt"_s && argc == 1) {
2197 expression = u"std::cbrt(arg1)"_s;
2198 } else if (name == u"ceil"_s && argc == 1) {
2199 expression = u"(arg1 < 0.0 && arg1 > -1.0) ? std::copysign(0.0, -1.0) : std::ceil(arg1)"_s;
2200 } else if (name == u"clz32"_s && argc == 1) {
2201 expression = u"qint32(qCountLeadingZeroBits(quint32(QJSNumberCoercion::toInteger(arg1))))"_s;
2202 } else if (name == u"cos"_s && argc == 1) {
2203 expression = u"std::cos(arg1)"_s;
2204 } else if (name == u"cosh"_s && argc == 1) {
2205 expression = u"std::cosh(arg1)"_s;
2206 } else if (name == u"exp"_s && argc == 1) {
2207 expression = u"std::isinf(arg1) "
2208 "? (std::copysign(1.0, arg1) == -1 ? 0.0 : %1) "
2209 ": std::exp(arg1)"_s.arg(inf);
2210 } else if (name == u"expm1"_s) {
2211 // TODO: complicated
2212 return false;
2213 } else if (name == u"floor"_s && argc == 1) {
2214 expression = u"std::floor(arg1)"_s;
2215 } else if (name == u"fround"_s && argc == 1) {
2216 expression = u"(std::isnan(arg1) || std::isinf(arg1) || qIsNull(arg1)) "
2217 "? arg1 "
2218 ": double(float(arg1))"_s;
2219 } else if (name == u"hypot"_s) {
2220 // TODO: complicated
2221 return false;
2222 } else if (name == u"imul"_s && argc == 2) {
2223 expression = u"qint32(quint32(QJSNumberCoercion::toInteger(arg1)) "
2224 "* quint32(QJSNumberCoercion::toInteger(arg2)))"_s;
2225 } else if (name == u"log"_s && argc == 1) {
2226 expression = u"arg1 < 0.0 ? %1 : std::log(arg1)"_s.arg(qNaN);
2227 } else if (name == u"log10"_s && argc == 1) {
2228 expression = u"arg1 < 0.0 ? %1 : std::log10(arg1)"_s.arg(qNaN);
2229 } else if (name == u"log1p"_s && argc == 1) {
2230 expression = u"arg1 < -1.0 ? %1 : std::log1p(arg1)"_s.arg(qNaN);
2231 } else if (name == u"log2"_s && argc == 1) {
2232 expression = u"arg1 < -0.0 ? %1 : std::log2(arg1)"_s.arg(qNaN);
2233 } else if (name == u"max"_s && argc >= 2) {
2234 expression = maxExpression(argc);
2235 } else if (name == u"min"_s && argc >= 2) {
2236 expression = minExpression(argc);
2237 } else if (name == u"pow"_s) {
2238 expression = u"QQmlPrivate::jsExponentiate(arg1, arg2)"_s;
2239 } else if (name == u"random"_s && argc == 0) {
2240 expression = u"QRandomGenerator::global()->generateDouble()"_s;
2241 } else if (name == u"round"_s && argc == 1) {
2242 expression = u"std::isfinite(arg1) "
2243 "? ((arg1 < 0.5 && arg1 >= -0.5) "
2244 "? std::copysign(0.0, arg1) "
2245 ": std::floor(arg1 + 0.5)) "
2246 ": arg1"_s;
2247 } else if (name == u"sign"_s && argc == 1) {
2248 expression = u"std::isnan(arg1) "
2249 "? %1 "
2250 ": (qIsNull(arg1) "
2251 "? arg1 "
2252 ": (std::signbit(arg1) ? -1.0 : 1.0))"_s.arg(qNaN);
2253 } else if (name == u"sin"_s && argc == 1) {
2254 expression = u"qIsNull(arg1) ? arg1 : std::sin(arg1)"_s;
2255 } else if (name == u"sinh"_s && argc == 1) {
2256 expression = u"qIsNull(arg1) ? arg1 : std::sinh(arg1)"_s;
2257 } else if (name == u"sqrt"_s && argc == 1) {
2258 expression = u"std::sqrt(arg1)"_s;
2259 } else if (name == u"tan"_s && argc == 1) {
2260 expression = u"qIsNull(arg1) ? arg1 : std::tan(arg1)"_s;
2261 } else if (name == u"tanh"_s && argc == 1) {
2262 expression = u"qIsNull(arg1) ? arg1 : std::tanh(arg1)"_s;
2263 } else if (name == u"trunc"_s && argc == 1) {
2264 expression = u"std::trunc(arg1)"_s;
2265 } else {
2266 return false;
2267 }
2268
2269 m_body += conversion(m_typeResolver->realType(), m_state.accumulatorOut(), expression);
2270
2271 m_body += u";\n"_s;
2272 m_body += u"}\n"_s;
2273 return true;
2274}
2275
2276static QString messageTypeForMethod(const QString &method)
2277{
2278 if (method == u"log" || method == u"debug")
2279 return u"QtDebugMsg"_s;
2280 if (method == u"info")
2281 return u"QtInfoMsg"_s;
2282 if (method == u"warn")
2283 return u"QtWarningMsg"_s;
2284 if (method == u"error")
2285 return u"QtCriticalMsg"_s;
2286 return QString();
2287}
2288
2289bool QQmlJSCodeGenerator::inlineConsoleMethod(const QString &name, int argc, int argv)
2290{
2291 const QString type = messageTypeForMethod(name);
2292 if (type.isEmpty())
2293 return false;
2294
2295 addInclude(u"QtCore/qloggingcategory.h"_s);
2296
2297 m_body += u"{\n";
2298 m_body += u" bool firstArgIsCategory = false;\n";
2299 const QQmlJSRegisterContent firstArg = argc > 0 ? registerType(argv) : QQmlJSRegisterContent();
2300
2301 // We could check whether the first argument is a QQmlLoggingCategoryBase here, and we should
2302 // because QQmlLoggingCategoryBase is now a builtin.
2303 // TODO: The run time check for firstArg is obsolete.
2304 const bool firstArgIsReference = argc > 0
2305 && firstArg.containedType()->isReferenceType();
2306
2307 if (firstArgIsReference) {
2308 m_body += u" QObject *firstArg = ";
2309 m_body += convertStored(
2310 firstArg.storedType(),
2311 m_typeResolver->genericType(firstArg.storedType()),
2312 registerVariable(argv));
2313 m_body += u";\n";
2314 }
2315
2316 m_body += u" const QLoggingCategory *category = aotContext->resolveLoggingCategory(";
2317 m_body += firstArgIsReference ? u"firstArg"_sv : u"nullptr"_sv;
2318 m_body += u", &firstArgIsCategory);\n";
2319 m_body += u" if (category && category->isEnabled(" + type + u")) {\n";
2320
2321 m_body += u" const QString message = ";
2322
2323 const auto stringConversion = [&](int i) -> QString {
2324 const QQmlJSScope::ConstPtr read = m_state.readRegister(argv + i).storedType();
2325 const QQmlJSScope::ConstPtr actual = registerType(argv + i).storedType();
2326 if (read == m_typeResolver->stringType()) {
2327 return convertStored(actual, read, consumedRegisterVariable(argv + i));
2328 } else if (actual->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
2329 addInclude(u"QtQml/qjslist.h"_s);
2330 return u"(u'[' + QJSList(&"_s + registerVariable(argv + i)
2331 + u", aotContext->engine).toString() + u']')"_s;
2332 } else {
2333 REJECT<QString>(u"converting arguments for console method to string"_s);
2334 }
2335 };
2336
2337 if (argc > 0) {
2338 if (firstArgIsReference) {
2339 const QString firstArgStringConversion = convertStored(
2340 registerType(argv).storedType(),
2341 m_typeResolver->stringType(), registerVariable(argv));
2342 m_body += u"(firstArgIsCategory ? QString() : (" + firstArgStringConversion;
2343 if (argc > 1)
2344 m_body += u".append(QLatin1Char(' ')))).append(";
2345 else
2346 m_body += u"))";
2347 } else {
2348 m_body += stringConversion(0);
2349 if (argc > 1)
2350 m_body += u".append(QLatin1Char(' ')).append(";
2351 }
2352
2353 for (int i = 1; i < argc; ++i) {
2354 if (i > 1)
2355 m_body += u".append(QLatin1Char(' ')).append("_s;
2356 m_body += stringConversion(i) + u')';
2357 }
2358 } else {
2359 m_body += u"QString()";
2360 }
2361 m_body += u";\n ";
2362 generateSetInstructionPointer();
2363 m_body += u" aotContext->writeToConsole(" + type + u", message, category);\n";
2364 m_body += u" }\n";
2365 m_body += u"}\n";
2366 return true;
2367}
2368
2369bool QQmlJSCodeGenerator::inlineArrayMethod(const QString &name, int base, int argc, int argv)
2370{
2371 const auto intType = m_typeResolver->int32Type();
2372 const auto elementType = registerType(base).storedType()->elementType();
2373 const auto boolType = m_typeResolver->boolType();
2374 const auto stringType = m_typeResolver->stringType();
2375 const auto baseType = registerType(base);
2376
2377 const QString baseVar = registerVariable(base);
2378 const QString qjsListMethod = u"QJSList(&"_s + baseVar + u", aotContext->engine)."
2379 + name + u"(";
2380
2381 addInclude(u"QtQml/qjslist.h"_s);
2382
2383 if (name == u"includes" && argc > 0 && argc < 3) {
2384 QString call = qjsListMethod
2385 + convertStored(registerType(argv).storedType(), elementType,
2386 consumedRegisterVariable(argv));
2387 if (argc == 2) {
2388 call += u", " + convertStored(registerType(argv + 1).storedType(), intType,
2389 consumedRegisterVariable(argv + 1));
2390 }
2391 call += u")";
2392
2393 m_body += m_state.accumulatorVariableOut + u" = "_s
2394 + conversion(boolType, m_state.accumulatorOut(), call) + u";\n"_s;
2395 return true;
2396 }
2397
2398 if (name == u"toString" || (name == u"join" && argc < 2)) {
2399 QString call = qjsListMethod;
2400 if (argc == 1) {
2401 call += convertStored(registerType(argv).storedType(), stringType,
2402 consumedRegisterVariable(argv));
2403 }
2404 call += u")";
2405
2406 m_body += m_state.accumulatorVariableOut + u" = "_s
2407 + conversion(stringType, m_state.accumulatorOut(), call) + u";\n"_s;
2408 return true;
2409 }
2410
2411 if (name == u"slice" && argc < 3) {
2412 QString call = qjsListMethod;
2413 for (int i = 0; i < argc; ++i) {
2414 if (i > 0)
2415 call += u", ";
2416 call += convertStored(registerType(argv + i).storedType(), intType,
2417 consumedRegisterVariable(argv + i));
2418 }
2419 call += u")";
2420
2421 m_body += m_state.accumulatorVariableOut + u" = "_s;
2422 if (baseType.storedType()->isListProperty())
2423 m_body += conversion(m_typeResolver->qObjectListType(), m_state.accumulatorOut(), call);
2424 else
2425 m_body += conversion(baseType, m_state.accumulatorOut(), call);
2426 m_body += u";\n"_s;
2427
2428 return true;
2429 }
2430
2431 if ((name == u"indexOf" || name == u"lastIndexOf") && argc > 0 && argc < 3) {
2432 QString call = qjsListMethod
2433 + convertStored(registerType(argv).storedType(), elementType,
2434 consumedRegisterVariable(argv));
2435 if (argc == 2) {
2436 call += u", " + convertStored(registerType(argv + 1).storedType(), intType,
2437 consumedRegisterVariable(argv + 1));
2438 }
2439 call += u")";
2440
2441 m_body += m_state.accumulatorVariableOut + u" = "_s
2442 + conversion(intType, m_state.accumulatorOut(), call) + u";\n"_s;
2443 return true;
2444 }
2445
2446 return false;
2447}
2448
2449void QQmlJSCodeGenerator::generate_CallPropertyLookup(int index, int base, int argc, int argv)
2450{
2451 INJECT_TRACE_INFO(generate_CallPropertyLookup);
2452
2453 const QQmlJSRegisterContent scopeContent = m_state.accumulatorOut().scope();
2454 const QQmlJSScope::ConstPtr scope = scopeContent.containedType();
2455
2456 AccumulatorConverter registers(this);
2457
2458 const QQmlJSRegisterContent baseType = registerType(base);
2459 const QString name = m_jsUnitGenerator->lookupName(index);
2460
2461 if (scope == m_typeResolver->mathObject()) {
2462 if (inlineMathMethod(name, argc, argv))
2463 return;
2464 } else if (scope == m_typeResolver->consoleObject()) {
2465 if (inlineConsoleMethod(name, argc, argv))
2466 return;
2467 } else if (scope == m_typeResolver->stringType()) {
2468 if (inlineStringMethod(name, base, argc, argv))
2469 return;
2470 } else if (baseType.storedType()->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
2471 if (inlineArrayMethod(name, base, argc, argv))
2472 return;
2473 }
2474
2475 if (m_state.accumulatorOut().isJavaScriptReturnValue())
2476 REJECT(u"call to untyped JavaScript function"_s);
2477
2478 m_body += u"{\n"_s;
2479 QString outVar;
2480
2481 if (scope->isReferenceType()) {
2482 const QString inputPointer = resolveQObjectPointer(
2483 scope, baseType, registerVariable(base),
2484 u"Cannot call method '%1' of %2"_s.arg(name));
2485
2486 const QString initMethodTemplate = m_state.isShadowable()
2487 ? u"initCallObjectPropertyLookupAsVariant(%1, %2)"_s
2488 : u"initCallObjectPropertyLookup(%1, %2, %3)"_s;
2489
2490 m_body += initAndCall(
2491 argc, argv,
2492 u"callObjectPropertyLookup(%1, %2, %3, %4)"_s.arg(index).arg(inputPointer),
2493 initMethodTemplate.arg(index).arg(inputPointer), &outVar);
2494 } else {
2495 const QQmlJSScope::ConstPtr originalScope
2496 = m_typeResolver->original(scopeContent).containedType();
2497 const QString inputPointer = resolveValueTypeContentPointer(
2498 originalScope, baseType, registerVariable(base),
2499 u"Cannot call method '%1' of %2"_s.arg(name));
2500
2501 m_body += initAndCall(
2502 argc, argv,
2503 u"callValueLookup(%1, %2, %3, %4)"_s.arg(index).arg(inputPointer),
2504 u"initCallValueLookup(%1, %2, %3)"_s
2505 .arg(index).arg(metaObject(originalScope)),
2506 &outVar);
2507 }
2508
2509 const QString lookup = u"doCall()"_s;
2510 const QString initialization = u"doInit()"_s;
2511 const QString preparation = getLookupPreparation(m_state.accumulatorOut(), outVar, index);
2512 recordMethodLookup(m_typeResolver->originalContainedType(m_state.accumulatorOut().scope()),
2513 originalMethod(m_state.accumulatorOut()));
2514 generateLookup(lookup, initialization, preparation);
2515 generateMoveOutVarAfterCall(outVar);
2516
2517 m_body += u"}\n"_s;
2518
2519 if (scope->isReferenceType())
2520 return;
2521
2522 const QQmlJSMetaMethod method = m_state.accumulatorOut().methodCall();
2523 if (!method.isConst())
2524 generateWriteBack(base);
2525}
2526
2527void QQmlJSCodeGenerator::generate_CallName(int name, int argc, int argv)
2528{
2529 Q_UNUSED(name);
2530 Q_UNUSED(argc);
2531 Q_UNUSED(argv);
2532 REJECT(u"CallName"_s);
2533}
2534
2535void QQmlJSCodeGenerator::generate_CallPossiblyDirectEval(int argc, int argv)
2536{
2537 Q_UNUSED(argc)
2538 Q_UNUSED(argv)
2540}
2541
2542void QQmlJSCodeGenerator::generate_CallGlobalLookup(int index, int argc, int argv)
2543{
2544 Q_UNUSED(index);
2545 Q_UNUSED(argc);
2546 Q_UNUSED(argv);
2547 REJECT(u"CallGlobalLookup"_s);
2548}
2549
2550void QQmlJSCodeGenerator::generate_CallQmlContextPropertyLookup(int index, int argc, int argv)
2551{
2552 INJECT_TRACE_INFO(generate_CallQmlContextPropertyLookup);
2553
2554 if (m_state.accumulatorOut().scope().contains(m_typeResolver->jsGlobalObject())) {
2555 const QString name = m_jsUnitGenerator->stringForIndex(
2556 m_jsUnitGenerator->lookupNameIndex(index));
2557 if (inlineTranslateMethod(name, argc, argv))
2558 return;
2559 }
2560
2561 if (m_state.accumulatorOut().isJavaScriptReturnValue())
2562 REJECT(u"call to untyped JavaScript function"_s);
2563
2564 AccumulatorConverter registers(this);
2565
2566 m_body += u"{\n"_s;
2567 QString outVar;
2568 m_body += initAndCall(
2569 argc, argv, u"callQmlContextPropertyLookup(%1, %2, %3)"_s.arg(index),
2570 u"initCallQmlContextPropertyLookup(%1, %2)"_s.arg(index), &outVar);
2571
2572 const QString lookup = u"doCall()"_s;
2573 const QString initialization = u"doInit()"_s;
2574 const QString preparation = getLookupPreparation(m_state.accumulatorOut(), outVar, index);
2575 recordMethodLookup(m_typeResolver->originalContainedType(m_state.accumulatorOut().scope()),
2576 originalMethod(m_state.accumulatorOut()));
2577 generateLookup(lookup, initialization, preparation);
2578 generateMoveOutVarAfterCall(outVar);
2579
2580 m_body += u"}\n"_s;
2581}
2582
2583void QQmlJSCodeGenerator::generate_CallWithSpread(int func, int thisObject, int argc, int argv)
2584{
2585 Q_UNUSED(func)
2586 Q_UNUSED(thisObject)
2587 Q_UNUSED(argc)
2588 Q_UNUSED(argv)
2590}
2591
2592void QQmlJSCodeGenerator::generate_TailCall(int func, int thisObject, int argc, int argv)
2593{
2594 Q_UNUSED(func)
2595 Q_UNUSED(thisObject)
2596 Q_UNUSED(argc)
2597 Q_UNUSED(argv)
2599}
2600
2601void QQmlJSCodeGenerator::generate_Construct(int func, int argc, int argv)
2602{
2603 INJECT_TRACE_INFO(generate_Construct);
2604 Q_UNUSED(func);
2605
2606 const auto originalResult = originalType(m_state.accumulatorOut());
2607
2608 if (originalResult.contains(m_typeResolver->dateTimeType())) {
2609 m_body += m_state.accumulatorVariableOut + u" = ";
2610 if (argc == 0) {
2611 m_body += conversion(
2612 m_typeResolver->dateTimeType(), m_state.accumulatorOut(),
2613 u"QDateTime::currentDateTime()"_s) + u";\n";
2614 return;
2615 }
2616
2617 if (argc == 1 && m_state.readRegister(argv).contains(m_typeResolver->dateTimeType())) {
2618 m_body += conversion(
2619 registerType(argv), m_state.readRegister(argv), registerVariable(argv))
2620 + u";\n";
2621 return;
2622 }
2623
2624 QString ctorArgs;
2625 constexpr int maxArgc = 7; // year, month, day, hours, minutes, seconds, milliseconds
2626 for (int i = 0; i < std::min(argc, maxArgc); ++i) {
2627 if (i > 0)
2628 ctorArgs += u", ";
2629 ctorArgs += conversion(
2630 registerType(argv + i), m_state.readRegister(argv + i),
2631 registerVariable(argv + i));
2632 }
2633 m_body += conversion(
2634 m_typeResolver->dateTimeType(), m_state.accumulatorOut(),
2635 u"aotContext->constructDateTime("_s + ctorArgs + u')') + u";\n";
2636 return;
2637 }
2638
2639 if (originalResult.contains(m_typeResolver->variantListType())) {
2640 rejectIfBadArray();
2641
2642 if (argc == 1 && m_state.readRegister(argv).contains(m_typeResolver->realType())) {
2643 addInclude(u"QtQml/qjslist.h"_s);
2644
2645 const QString error = u" aotContext->engine->throwError(QJSValue::RangeError, "_s
2646 + u"QLatin1String(\"Invalid array length\"));\n"_s;
2647
2648 const QString indexName = registerVariable(argv);
2649 const auto indexType = registerType(argv).containedType();
2650 if (!m_typeResolver->isNativeArrayIndex(indexType)) {
2651 m_body += u"if (!QJSNumberCoercion::isArrayIndex("_s + indexName + u")) {\n"_s
2652 + error;
2653 generateReturnError();
2654 m_body += u"}\n"_s;
2655 } else if (!m_typeResolver->isUnsignedInteger(indexType)) {
2656 m_body += u"if ("_s + indexName + u" < 0) {\n"_s
2657 + error;
2658 generateReturnError();
2659 m_body += u"}\n"_s;
2660 }
2661
2662 m_body += m_state.accumulatorVariableOut + u" = "_s
2663 + m_state.accumulatorOut().storedType()->internalName() + u"();\n"_s;
2664 m_body += u"QJSList(&"_s + m_state.accumulatorVariableOut
2665 + u", aotContext->engine).resize("_s
2666 + convertStored(
2667 registerType(argv).storedType(), m_typeResolver->sizeType(),
2668 consumedRegisterVariable(argv))
2669 + u");\n"_s;
2670 } else if (!m_logger->currentFunctionHasCompileError()) {
2671 generateArrayInitializer(argc, argv);
2672 }
2673 return;
2674 }
2675
2676 const QQmlJSScope::ConstPtr originalContained = originalResult.containedType();
2677 if (originalContained->isValueType() && originalResult.isMethodCall()) {
2678 const QQmlJSMetaMethod ctor = originalResult.methodCall();
2679 if (ctor.isJavaScriptFunction())
2680 REJECT(u"calling JavaScript constructor "_s + ctor.methodName());
2681
2682 QList<QQmlJSRegisterContent> argumentTypes;
2683 QStringList arguments;
2684 for (int i = 0; i < argc; ++i) {
2685 argumentTypes.append(registerType(argv + i));
2686 arguments.append(consumedRegisterVariable(argv + i));
2687 }
2688
2689 const QQmlJSScope::ConstPtr extension = originalContained->extensionType().scope;
2690 const QString result = generateCallConstructor(
2691 ctor, argumentTypes, arguments, metaType(originalContained),
2692 metaObject(extension ? extension : originalContained));
2693
2694 m_body += m_state.accumulatorVariableOut + u" = "_s
2695 + conversion(m_pool->storedIn(originalResult, m_typeResolver->varType()),
2696 m_state.accumulatorOut(), result)
2697 + u";\n"_s;
2698
2699 return;
2700 }
2701
2702
2703 REJECT(u"Construct"_s);
2704}
2705
2706void QQmlJSCodeGenerator::generate_ConstructWithSpread(int func, int argc, int argv)
2707{
2708 Q_UNUSED(func)
2709 Q_UNUSED(argc)
2710 Q_UNUSED(argv)
2712}
2713
2714void QQmlJSCodeGenerator::generate_SetUnwindHandler(int offset)
2715{
2716 Q_UNUSED(offset)
2717 REJECT(u"SetUnwindHandler"_s);
2718}
2719
2720void QQmlJSCodeGenerator::generate_UnwindDispatch()
2721{
2722 REJECT(u"UnwindDispatch"_s);
2723}
2724
2725void QQmlJSCodeGenerator::generate_UnwindToLabel(int level, int offset)
2726{
2727 Q_UNUSED(level)
2728 Q_UNUSED(offset)
2730}
2731
2732void QQmlJSCodeGenerator::generate_DeadTemporalZoneCheck(int name)
2733{
2734 Q_UNUSED(name)
2735 INJECT_TRACE_INFO(generate_DeadTemporalZoneCheck);
2736 // Nothing to do here. If we have statically asserted the dtz check in the type propagator
2737 // the value cannot be empty. Otherwise we can't get here.
2738}
2739
2740void QQmlJSCodeGenerator::generate_ThrowException()
2741{
2742 INJECT_TRACE_INFO(generate_ThrowException);
2743
2744 generateSetInstructionPointer();
2745 m_body += u"aotContext->engine->throwError("_s + conversion(
2746 m_state.accumulatorIn(),
2747 m_typeResolver->jsValueType(),
2748 m_state.accumulatorVariableIn) + u");\n"_s;
2749 generateReturnError();
2750 m_skipUntilNextLabel = true;
2751 resetState();
2752}
2753
2754void QQmlJSCodeGenerator::generate_GetException()
2755{
2757}
2758
2759void QQmlJSCodeGenerator::generate_SetException()
2760{
2762}
2763
2764void QQmlJSCodeGenerator::generate_CreateCallContext()
2765{
2766 INJECT_TRACE_INFO(generate_CreateCallContext);
2767
2768 m_body += u"{\n"_s;
2769}
2770
2771void QQmlJSCodeGenerator::generate_PushCatchContext(int index, int nameIndex)
2772{
2773 Q_UNUSED(index)
2774 Q_UNUSED(nameIndex)
2775 REJECT(u"PushCatchContext"_s);
2776}
2777
2778void QQmlJSCodeGenerator::generate_PushWithContext()
2779{
2781}
2782
2783void QQmlJSCodeGenerator::generate_PushBlockContext(int index)
2784{
2785 Q_UNUSED(index)
2787}
2788
2789void QQmlJSCodeGenerator::generate_CloneBlockContext()
2790{
2792}
2793
2794void QQmlJSCodeGenerator::generate_PushScriptContext(int index)
2795{
2796 Q_UNUSED(index)
2798}
2799
2800void QQmlJSCodeGenerator::generate_PopScriptContext()
2801{
2803}
2804
2805void QQmlJSCodeGenerator::generate_PopContext()
2806{
2807 INJECT_TRACE_INFO(generate_PopContext);
2808
2809 // Add an empty block before the closing brace, in case there was a bare label before it.
2810 m_body += u"{}\n}\n"_s;
2811}
2812
2813void QQmlJSCodeGenerator::generate_GetIterator(int iterator)
2814{
2815 INJECT_TRACE_INFO(generate_GetIterator);
2816
2817 addInclude(u"QtQml/qjslist.h"_s);
2818 const QQmlJSRegisterContent listType = m_state.accumulatorIn();
2819 if (!listType.isList())
2820 REJECT(u"iterator on non-list type"_s);
2821
2822 const QQmlJSRegisterContent iteratorType = m_state.accumulatorOut();
2823 if (!iteratorType.isProperty())
2824 REJECT(u"using non-iterator as iterator"_s);
2825
2826 const QString identifier = QString::number(iteratorType.baseLookupIndex());
2827 QString baseName = m_state.accumulatorVariableOut.mid(2); // remove "s."
2828 const QString iteratorName = baseName + u"Iterator" + identifier;
2829 const QString listName = baseName + u"List" + identifier;
2830
2831 m_body += u"QJSListFor"_s
2832 + (iterator == int(QQmlJS::AST::ForEachType::In) ? u"In"_s : u"Of"_s)
2833 + u"Iterator "_s + iteratorName + u";\n";
2834 m_body += m_state.accumulatorVariableOut + u" = &" + iteratorName + u";\n";
2835
2836 m_body += m_state.accumulatorVariableOut + u"->init(";
2837 if (iterator == int(QQmlJS::AST::ForEachType::In)) {
2838 if (!iteratorType.isStoredIn(m_typeResolver->forInIteratorPtr()))
2839 REJECT(u"using non-iterator as iterator"_s);
2840 m_body += u"QJSList(&" + m_state.accumulatorVariableIn + u", aotContext->engine)";
2841 }
2842 m_body += u");\n";
2843
2844 if (iterator == int(QQmlJS::AST::ForEachType::Of)) {
2845 if (!iteratorType.isStoredIn(m_typeResolver->forOfIteratorPtr()))
2846 REJECT(u"using non-iterator as iterator"_s);
2847 m_body += u"const auto &" // Rely on life time extension for const refs
2848 + listName + u" = " + consumedAccumulatorVariableIn();
2849 }
2850}
2851
2852void QQmlJSCodeGenerator::generate_IteratorNext(int value, int offset)
2853{
2854 INJECT_TRACE_INFO(generate_IteratorNext);
2855
2856 Q_ASSERT(value == m_state.changedRegisterIndex());
2857 const QQmlJSRegisterContent iteratorContent = m_state.accumulatorIn();
2858 if (!iteratorContent.isProperty())
2859 REJECT(u"using non-iterator as iterator"_s);
2860
2861 const QQmlJSScope::ConstPtr iteratorType = iteratorContent.storedType();
2862 const QString listName = m_state.accumulatorVariableIn
2863 + u"List" + QString::number(iteratorContent.baseLookupIndex());
2864 QString qjsList;
2865 if (iteratorType == m_typeResolver->forOfIteratorPtr())
2866 qjsList = u"QJSList(&" + listName + u", aotContext->engine)";
2867 else if (iteratorType != m_typeResolver->forInIteratorPtr())
2868 REJECT(u"using non-iterator as iterator"_s);
2869
2870 m_body += u"if (" + m_state.accumulatorVariableIn + u"->hasNext(" + qjsList + u")) {\n ";
2871
2872 // We know that this works because we can do ->next() below.
2873 QQmlJSRegisterContent iteratorValue = m_typeResolver->extractNonVoidFromOptionalType(
2874 m_typeResolver->original(m_state.changedRegister()));
2875 iteratorValue = m_pool->storedIn(iteratorValue, iteratorValue.containedType());
2876
2877 m_body += changedRegisterVariable() + u" = "
2878 + conversion(
2879 iteratorValue, m_state.changedRegister(),
2880 m_state.accumulatorVariableIn + u"->next(" + qjsList + u')')
2881 + u";\n";
2882 m_body += u"} else {\n ";
2883 m_body += changedRegisterVariable() + u" = "
2884 + conversion(m_typeResolver->voidType(), m_state.changedRegister(), QString());
2885 m_body += u";\n ";
2886 generateJumpCodeWithTypeConversions(offset);
2887 m_body += u"\n}"_s;
2888}
2889
2890void QQmlJSCodeGenerator::generate_IteratorNextForYieldStar(int iterator, int object, int offset)
2891{
2892 Q_UNUSED(iterator)
2893 Q_UNUSED(object)
2894 Q_UNUSED(offset)
2896}
2897
2898void QQmlJSCodeGenerator::generate_IteratorClose()
2899{
2901}
2902
2903void QQmlJSCodeGenerator::generate_DestructureRestElement()
2904{
2906}
2907
2908void QQmlJSCodeGenerator::generate_DeleteProperty(int base, int index)
2909{
2910 Q_UNUSED(base)
2911 Q_UNUSED(index)
2913}
2914
2915void QQmlJSCodeGenerator::generate_DeleteName(int name)
2916{
2917 Q_UNUSED(name)
2919}
2920
2921void QQmlJSCodeGenerator::generate_TypeofName(int name)
2922{
2923 Q_UNUSED(name);
2924 REJECT(u"TypeofName"_s);
2925}
2926
2927void QQmlJSCodeGenerator::generate_TypeofValue()
2928{
2929 REJECT(u"TypeofValue"_s);
2930}
2931
2932void QQmlJSCodeGenerator::generate_DeclareVar(int varName, int isDeletable)
2933{
2934 Q_UNUSED(varName)
2935 Q_UNUSED(isDeletable)
2937}
2938
2939void QQmlJSCodeGenerator::generate_DefineArray(int argc, int args)
2940{
2941 INJECT_TRACE_INFO(generate_DefineArray);
2942
2943 rejectIfBadArray();
2944 if (!m_logger->currentFunctionHasCompileError())
2945 generateArrayInitializer(argc, args);
2946}
2947
2948void QQmlJSCodeGenerator::generate_DefineObjectLiteral(int internalClassId, int argc, int args)
2949{
2950 INJECT_TRACE_INFO(generate_DefineObjectLiteral);
2951
2952 const QQmlJSScope::ConstPtr stored = m_state.accumulatorOut().storedType();
2953 if (stored->accessSemantics() != QQmlJSScope::AccessSemantics::Value)
2954 REJECT(u"storing an object literal in a non-value type"_s);
2955
2956 const QQmlJSScope::ConstPtr contained = m_state.accumulatorOut().containedType();
2957
2958 const int classSize = m_jsUnitGenerator->jsClassSize(internalClassId);
2959
2960 // This is not implemented because we cannot statically determine the type of the value and we
2961 // don't want to rely on QVariant::convert() since that may give different results than
2962 // the JavaScript coercion. We might still make it work by querying the QMetaProperty
2963 // for its type at run time and runtime coercing to that, but we don't know whether that
2964 // still pays off.
2965 if (argc > classSize)
2966 REJECT(u"non-literal keys of object literals"_s);
2967
2968 Q_ASSERT(argc == classSize);
2969
2970 const auto createVariantMap = [&]() {
2971 QString result;
2972 result += u"QVariantMap {\n";
2973 const QQmlJSScope::ConstPtr propType = m_typeResolver->varType();
2974 for (int i = 0; i < classSize; ++i) {
2975 result += u"{ "_s
2976 + QQmlJSUtils::toLiteral(m_jsUnitGenerator->jsClassMember(internalClassId, i))
2977 + u", "_s;
2978 const int currentArg = args + i;
2979 const QQmlJSScope::ConstPtr argType = registerType(currentArg).storedType();
2980 const QString consumedArg = consumedRegisterVariable(currentArg);
2981 result += convertStored(argType, propType, consumedArg) + u" },\n";
2982 }
2983
2984
2985 result += u"}";
2986 return result;
2987 };
2988
2989 if (contained == m_typeResolver->varType() || contained == m_typeResolver->variantMapType()) {
2990 m_body += m_state.accumulatorVariableOut + u" = "_s + createVariantMap() + u";\n"_s;
2991 return;
2992 }
2993
2994 if (contained == m_typeResolver->jsValueType()) {
2995 m_body += m_state.accumulatorVariableOut + u" = aotContext->engine->toScriptValue("_s
2996 + createVariantMap() + u");\n"_s;
2997 return;
2998 }
2999
3000 m_body += m_state.accumulatorVariableOut + u" = "_s + stored->augmentedInternalName();
3001 const bool isVariantOrPrimitive = (stored == m_typeResolver->varType())
3002 || (stored == m_typeResolver->jsPrimitiveType());
3003
3004 if (m_state.accumulatorOut().contains(stored)) {
3005 m_body += u"()";
3006 } else if (isVariantOrPrimitive) {
3007 m_body += u'(' + metaType(m_state.accumulatorOut().containedType()) + u')';
3008 } else {
3009 REJECT(u"storing an object literal in an unsupported container %1"_s
3010 .arg(stored->internalName()));
3011 }
3012 m_body += u";\n";
3013
3014 if (argc == 0)
3015 return;
3016
3017 bool isExtension = false;
3018 if (!m_typeResolver->canPopulate(contained, m_typeResolver->variantMapType(), &isExtension))
3019 REJECT(u"storing an object literal in a non-structured value type"_s);
3020
3021 const QQmlJSScope::ConstPtr accessor = isExtension
3022 ? contained->extensionType().scope
3023 : contained;
3024
3025 m_body += u"{\n";
3026 m_body += u" const QMetaObject *meta = ";
3027 if (!isExtension && isVariantOrPrimitive)
3028 m_body += m_state.accumulatorVariableOut + u".metaType().metaObject()";
3029 else
3030 m_body += metaObject(accessor);
3031 m_body += u";\n";
3032
3033 for (int i = 0; i < classSize; ++i) {
3034 m_body += u" {\n";
3035 const QString propName = m_jsUnitGenerator->jsClassMember(internalClassId, i);
3036 const int currentArg = args + i;
3037 const QQmlJSRegisterContent propType = m_state.readRegister(currentArg);
3038 const QQmlJSRegisterContent argType = registerType(currentArg);
3039 const QQmlJSMetaProperty property = contained->property(propName);
3040 const QString consumedArg = consumedRegisterVariable(currentArg);
3041 QString argument = conversion(argType, propType, consumedArg);
3042
3043 if (argument == consumedArg) {
3044 argument = registerVariable(currentArg);
3045 } else {
3046 m_body += u" "_s + propType.storedType()->augmentedInternalName()
3047 + u" arg = "_s + argument + u";\n";
3048 argument = u"arg"_s;
3049 }
3050
3051 int index = property.index();
3052 if (index == -1)
3053 continue;
3054
3055 const QString indexString = QString::number(index);
3056 m_body += u" void *argv[] = { %1, nullptr };\n"_s
3057 .arg(contentPointer(propType, argument));
3058 m_body += u" meta->property("_s + indexString;
3059 m_body += u").enclosingMetaObject()->d.static_metacall(reinterpret_cast<QObject *>(";
3060 m_body += contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut);
3061 m_body += u"), QMetaObject::WriteProperty, " + indexString + u", argv);\n";
3062 m_body += u" }\n";
3063 }
3064
3065 m_body += u"}\n";
3066
3067}
3068
3069void QQmlJSCodeGenerator::generate_CreateClass(int classIndex, int heritage, int computedNames)
3070{
3071 Q_UNUSED(classIndex)
3072 Q_UNUSED(heritage)
3073 Q_UNUSED(computedNames)
3075}
3076
3077void QQmlJSCodeGenerator::generate_CreateMappedArgumentsObject()
3078{
3080}
3081
3082void QQmlJSCodeGenerator::generate_CreateUnmappedArgumentsObject()
3083{
3085}
3086
3087void QQmlJSCodeGenerator::generate_CreateRestParameter(int argIndex)
3088{
3089 Q_UNUSED(argIndex)
3091}
3092
3093void QQmlJSCodeGenerator::generate_ConvertThisToObject()
3094{
3095 INJECT_TRACE_INFO(generate_ConvertThisToObject);
3096
3097 m_body += changedRegisterVariable() + u" = "_s
3098 + conversion(m_typeResolver->qObjectType(), m_state.changedRegister(),
3099 u"aotContext->thisObject()"_s)
3100 + u";\n"_s;
3101}
3102
3103void QQmlJSCodeGenerator::generate_LoadSuperConstructor()
3104{
3106}
3107
3108void QQmlJSCodeGenerator::generate_ToObject()
3109{
3111}
3112
3113void QQmlJSCodeGenerator::generate_Jump(int offset)
3114{
3115 INJECT_TRACE_INFO(generate_Jump);
3116
3117 generateJumpCodeWithTypeConversions(offset);
3118 m_skipUntilNextLabel = true;
3119 resetState();
3120}
3121
3122void QQmlJSCodeGenerator::generate_JumpTrue(int offset)
3123{
3124 INJECT_TRACE_INFO(generate_JumpTrue);
3125
3126 m_body += u"if ("_s;
3127 m_body += convertStored(m_state.accumulatorIn().storedType(), m_typeResolver->boolType(),
3128 m_state.accumulatorVariableIn);
3129 m_body += u") "_s;
3130 generateJumpCodeWithTypeConversions(offset);
3131}
3132
3133void QQmlJSCodeGenerator::generate_JumpFalse(int offset)
3134{
3135 INJECT_TRACE_INFO(generate_JumpFalse);
3136
3137 m_body += u"if (!"_s;
3138 m_body += convertStored(m_state.accumulatorIn().storedType(), m_typeResolver->boolType(),
3139 m_state.accumulatorVariableIn);
3140 m_body += u") "_s;
3141 generateJumpCodeWithTypeConversions(offset);
3142}
3143
3144void QQmlJSCodeGenerator::generate_JumpNoException(int offset)
3145{
3146 INJECT_TRACE_INFO(generate_JumpNoException);
3147
3148 m_body += u"if (!context->engine->hasException()) "_s;
3149 generateJumpCodeWithTypeConversions(offset);
3150}
3151
3152void QQmlJSCodeGenerator::generate_JumpNotUndefined(int offset)
3153{
3154 Q_UNUSED(offset)
3156}
3157
3158void QQmlJSCodeGenerator::generate_CheckException()
3159{
3160 INJECT_TRACE_INFO(generate_CheckException);
3161
3162 generateExceptionCheck();
3163}
3164
3165void QQmlJSCodeGenerator::generate_CmpEqNull()
3166{
3167 INJECT_TRACE_INFO(generate_CmpEqNull);
3168 generateEqualityOperation(literalType(m_typeResolver->nullType()), QString(), u"equals"_s, false);
3169}
3170
3171void QQmlJSCodeGenerator::generate_CmpNeNull()
3172{
3173 INJECT_TRACE_INFO(generate_CmlNeNull);
3174 generateEqualityOperation(literalType(m_typeResolver->nullType()), QString(), u"equals"_s, true);
3175}
3176
3177QString QQmlJSCodeGenerator::getLookupPreparation(
3178 QQmlJSRegisterContent content, const QString &var, int lookup)
3179{
3180 if (content.contains(content.storedType()))
3181 return QString();
3182
3183 if (content.storedType()->isOpaqueType()) {
3184 // Can't do meta-type lookups if the type is potentially unregistered,
3185 // which can be the case for mehods
3186 REJECT<QString>("Can't prepare lookup involving non-registered types"_L1);
3187 }
3188
3189 if (content.isStoredIn(m_typeResolver->varType())) {
3190 return var + u" = QVariant(aotContext->lookupResultMetaType("_s
3191 + QString::number(lookup) + u"))"_s;
3192 }
3193
3194 if (content.isStoredIn(m_typeResolver->jsPrimitiveType())) {
3195 return var + u" = QJSPrimitiveValue(aotContext->lookupResultMetaType("_s
3196 + QString::number(lookup) + u"))"_s;
3197 }
3198
3199 // TODO: We could make sure they're compatible, for example QObject pointers.
3200 return QString();
3201}
3202
3203QString QQmlJSCodeGenerator::contentPointer(QQmlJSRegisterContent content, const QString &var)
3204{
3205 const QQmlJSScope::ConstPtr stored = content.storedType();
3206 if (content.contains(stored))
3207 return u'&' + var;
3208
3209 if (content.isStoredIn(m_typeResolver->varType())
3210 || content.isStoredIn(m_typeResolver->jsPrimitiveType())) {
3211 return var + u".data()"_s;
3212 }
3213
3214 if (stored->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
3215 return u'&' + var;
3216
3217 if (m_typeResolver->isNumeric(content.storedType())
3218 && content.containedType()->scopeType() == QQmlSA::ScopeType::EnumScope) {
3219 return u'&' + var;
3220 }
3221
3222 if (stored->isListProperty() && content.containedType()->isListProperty())
3223 return u'&' + var;
3224
3225 REJECT<QString>(
3226 u"content pointer of unsupported wrapper type "_s + content.descriptiveName());
3227}
3228
3229QString QQmlJSCodeGenerator::contentType(QQmlJSRegisterContent content, const QString &var)
3230{
3231 const QQmlJSScope::ConstPtr stored = content.storedType();
3232 const QQmlJSScope::ConstPtr contained = content.containedType();
3233 if (contained == stored)
3234 return metaTypeFromType(stored);
3235
3236 if (stored == m_typeResolver->varType() || stored == m_typeResolver->jsPrimitiveType())
3237 return var + u".metaType()"_s; // We expect the container to be initialized
3238
3239 if (stored->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
3240 return metaType(contained);
3241
3242 const QQmlJSScope::ConstPtr nonComposite = QQmlJSScope::nonCompositeBaseType(contained);
3243 if (m_typeResolver->isNumeric(stored) && nonComposite->scopeType() == QQmlSA::ScopeType::EnumScope)
3244 return metaTypeFromType(nonComposite->baseType());
3245
3246 if (stored->isListProperty() && contained->isListProperty())
3247 return metaType(contained);
3248
3249 REJECT<QString>(
3250 u"content type of unsupported wrapper type "_s + content.descriptiveName());
3251}
3252
3253void QQmlJSCodeGenerator::generate_CmpEqInt(int lhsConst)
3254{
3255 INJECT_TRACE_INFO(generate_CmpEqInt);
3256
3257 generateEqualityOperation(
3258 literalType(m_typeResolver->int32Type()), QString::number(lhsConst), u"equals"_s, false);
3259}
3260
3261void QQmlJSCodeGenerator::generate_CmpNeInt(int lhsConst)
3262{
3263 INJECT_TRACE_INFO(generate_CmpNeInt);
3264
3265 generateEqualityOperation(
3266 literalType(m_typeResolver->int32Type()), QString::number(lhsConst), u"equals"_s, true);
3267}
3268
3269void QQmlJSCodeGenerator::generate_CmpEq(int lhs)
3270{
3271 INJECT_TRACE_INFO(generate_CmpEq);
3272 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"equals"_s, false);
3273}
3274
3275void QQmlJSCodeGenerator::generate_CmpNe(int lhs)
3276{
3277 INJECT_TRACE_INFO(generate_CmpNe);
3278 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"equals"_s, true);
3279}
3280
3281void QQmlJSCodeGenerator::generate_CmpGt(int lhs)
3282{
3283 INJECT_TRACE_INFO(generate_CmpGt);
3284 generateCompareOperation(lhs, u">"_s);
3285}
3286
3287void QQmlJSCodeGenerator::generate_CmpGe(int lhs)
3288{
3289 INJECT_TRACE_INFO(generate_CmpGe);
3290 generateCompareOperation(lhs, u">="_s);
3291}
3292
3293void QQmlJSCodeGenerator::generate_CmpLt(int lhs)
3294{
3295 INJECT_TRACE_INFO(generate_CmpLt);
3296 generateCompareOperation(lhs, u"<"_s);
3297}
3298
3299void QQmlJSCodeGenerator::generate_CmpLe(int lhs)
3300{
3301 INJECT_TRACE_INFO(generate_CmpLe);
3302 generateCompareOperation(lhs, u"<="_s);
3303}
3304
3305void QQmlJSCodeGenerator::generate_CmpStrictEqual(int lhs)
3306{
3307 INJECT_TRACE_INFO(generate_CmpStrictEqual);
3308 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"strictlyEquals"_s, false);
3309}
3310
3311void QQmlJSCodeGenerator::generate_CmpStrictNotEqual(int lhs)
3312{
3313 INJECT_TRACE_INFO(generate_CmpStrictNotEqual);
3314 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"strictlyEquals"_s, true);
3315}
3316
3317void QQmlJSCodeGenerator::generate_CmpIn(int lhs)
3318{
3319 Q_UNUSED(lhs)
3320 REJECT(u"CmpIn"_s);
3321}
3322
3323void QQmlJSCodeGenerator::generate_CmpInstanceOf(int lhs)
3324{
3325 Q_UNUSED(lhs)
3327}
3328
3329void QQmlJSCodeGenerator::generate_As(int lhs)
3330{
3331 INJECT_TRACE_INFO(generate_As);
3332
3333 const QString input = registerVariable(lhs);
3334 const QQmlJSRegisterContent inputContent = m_state.readRegister(lhs);
3335 const QQmlJSRegisterContent outputContent = m_state.accumulatorOut();
3336
3337 // If the originalType output is a conversion, we're supposed to check for the contained
3338 // type and if it doesn't match, set the result to null or undefined.
3339 const QQmlJSRegisterContent originalContent = originalType(outputContent);
3340 QQmlJSScope::ConstPtr target;
3341 if (originalContent.containedType()->isReferenceType())
3342 target = originalContent.containedType();
3343 else if (originalContent.isConversion())
3344 target = m_typeResolver->extractNonVoidFromOptionalType(originalContent).containedType();
3345 else if (originalContent.variant() == QQmlJSRegisterContent::Cast)
3346 target = originalContent.containedType();
3347
3348 if (!target)
3349 REJECT(u"type assertion to unknown type"_s);
3350
3351 const bool isTrivial = m_typeResolver->inherits(
3352 m_typeResolver->originalContainedType(inputContent), target);
3353
3354 m_body += m_state.accumulatorVariableOut + u" = "_s;
3355
3356 if (!isTrivial && target->isReferenceType()) {
3357 const QQmlJSScope::ConstPtr genericContained = m_typeResolver->genericType(target);
3358 const QString inputConversion = inputContent.storedType()->isReferenceType()
3359 ? input
3360 : convertStored(inputContent.storedType(), genericContained, input);
3361
3362 if (target->isComposite()
3363 && m_state.accumulatorIn().isStoredIn(m_typeResolver->metaObjectType())) {
3364 m_body += conversion(
3365 genericContained, outputContent,
3366 m_state.accumulatorVariableIn + u"->cast("_s + inputConversion + u')');
3367 } else {
3368 m_body += conversion(
3369 genericContained, outputContent,
3370 u'(' + metaObject(target) + u")->cast("_s + inputConversion + u')');
3371 }
3372 m_body += u";\n"_s;
3373 return;
3374 }
3375
3376 if (inputContent.isStoredIn(m_typeResolver->varType())
3377 || inputContent.isStoredIn(m_typeResolver->jsPrimitiveType())) {
3378
3379 const auto source = m_typeResolver->extractNonVoidFromOptionalType(
3380 originalType(inputContent)).containedType();
3381
3382 if (source && source == target) {
3383 m_body += input + u".metaType() == "_s + metaType(target)
3384 + u" ? " + conversion(inputContent, outputContent, input)
3385 + u" : " + conversion(
3386 literalType(m_typeResolver->voidType()), outputContent, QString());
3387 m_body += u";\n"_s;
3388 return;
3389 }
3390 }
3391
3392 if (isTrivial) {
3393 // No actual conversion necessary. The 'as' is a no-op
3394 m_body += conversion(inputContent, m_state.accumulatorOut(), input) + u";\n"_s;
3395 return;
3396 }
3397
3398 REJECT(u"non-trivial value type assertion"_s);
3399}
3400
3401void QQmlJSCodeGenerator::generate_UNot()
3402{
3403 INJECT_TRACE_INFO(generate_UNot);
3404 generateUnaryOperation(u"!"_s);
3405}
3406
3407void QQmlJSCodeGenerator::generate_UPlus()
3408{
3409 INJECT_TRACE_INFO(generate_UPlus);
3410 generateUnaryOperation(u"+"_s);
3411}
3412
3413void QQmlJSCodeGenerator::generate_UMinus()
3414{
3415 INJECT_TRACE_INFO(generate_UMinus);
3416 generateUnaryOperation(u"-"_s);
3417}
3418
3419void QQmlJSCodeGenerator::generate_UCompl()
3420{
3421 INJECT_TRACE_INFO(generate_UCompl);
3422 generateUnaryOperation(u"~"_s);
3423}
3424
3425void QQmlJSCodeGenerator::generate_Increment()
3426{
3427 INJECT_TRACE_INFO(generate_Increment);
3428 generateInPlaceOperation(u"++"_s);
3429}
3430
3431void QQmlJSCodeGenerator::generate_Decrement()
3432{
3433 INJECT_TRACE_INFO(generate_Decrement);
3434 generateInPlaceOperation(u"--"_s);
3435}
3436
3437void QQmlJSCodeGenerator::generate_Add(int lhs)
3438{
3439 INJECT_TRACE_INFO(generate_Add);
3440 generateArithmeticOperation(lhs, u"+"_s);
3441}
3442
3443void QQmlJSCodeGenerator::generate_BitAnd(int lhs)
3444{
3445 INJECT_TRACE_INFO(generate_BitAnd);
3446 generateArithmeticOperation(lhs, u"&"_s);
3447}
3448
3449void QQmlJSCodeGenerator::generate_BitOr(int lhs)
3450{
3451 INJECT_TRACE_INFO(generate_BitOr);
3452 generateArithmeticOperation(lhs, u"|"_s);
3453}
3454
3455void QQmlJSCodeGenerator::generate_BitXor(int lhs)
3456{
3457 INJECT_TRACE_INFO(generate_BitXor);
3458 generateArithmeticOperation(lhs, u"^"_s);
3459}
3460
3461void QQmlJSCodeGenerator::generate_UShr(int lhs)
3462{
3463 INJECT_TRACE_INFO(generate_BitUShr);
3464 generateShiftOperation(lhs, u">>"_s);
3465}
3466
3467void QQmlJSCodeGenerator::generate_Shr(int lhs)
3468{
3469 INJECT_TRACE_INFO(generate_Shr);
3470 generateShiftOperation(lhs, u">>"_s);
3471}
3472
3473void QQmlJSCodeGenerator::generate_Shl(int lhs)
3474{
3475 INJECT_TRACE_INFO(generate_Shl);
3476 generateShiftOperation(lhs, u"<<"_s);
3477}
3478
3479void QQmlJSCodeGenerator::generate_BitAndConst(int rhs)
3480{
3481 INJECT_TRACE_INFO(generate_BitAndConst);
3482 generateArithmeticConstOperation(rhs, u"&"_s);
3483}
3484
3485void QQmlJSCodeGenerator::generate_BitOrConst(int rhs)
3486{
3487 INJECT_TRACE_INFO(generate_BitOrConst);
3488 generateArithmeticConstOperation(rhs, u"|"_s);
3489}
3490
3491void QQmlJSCodeGenerator::generate_BitXorConst(int rhs)
3492{
3493 INJECT_TRACE_INFO(generate_BitXorConst);
3494 generateArithmeticConstOperation(rhs, u"^"_s);
3495}
3496
3497void QQmlJSCodeGenerator::generate_UShrConst(int rhs)
3498{
3499 INJECT_TRACE_INFO(generate_UShrConst);
3500 generateArithmeticConstOperation(rhs & 0x1f, u">>"_s);
3501}
3502
3503void QQmlJSCodeGenerator::generate_ShrConst(int rhs)
3504{
3505 INJECT_TRACE_INFO(generate_ShrConst);
3506 generateArithmeticConstOperation(rhs & 0x1f, u">>"_s);
3507}
3508
3509void QQmlJSCodeGenerator::generate_ShlConst(int rhs)
3510{
3511 INJECT_TRACE_INFO(generate_ShlConst);
3512 generateArithmeticConstOperation(rhs & 0x1f, u"<<"_s);
3513}
3514
3515void QQmlJSCodeGenerator::generate_Exp(int lhs)
3516{
3517 INJECT_TRACE_INFO(generate_Exp);
3518
3519 const QString lhsString = conversion(
3520 registerType(lhs), m_state.readRegister(lhs), consumedRegisterVariable(lhs));
3521 const QString rhsString = conversion(
3522 m_state.accumulatorIn(), m_state.readAccumulator(),
3523 consumedAccumulatorVariableIn());
3524
3525 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !lhsString.isEmpty());
3526 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !rhsString.isEmpty());
3527
3528 const QQmlJSRegisterContent originalOut = originalType(m_state.accumulatorOut());
3529 m_body += m_state.accumulatorVariableOut + u" = "_s;
3530 m_body += conversion(
3531 originalOut, m_state.accumulatorOut(),
3532 u"QQmlPrivate::jsExponentiate("_s + lhsString + u", "_s + rhsString + u')');
3533 m_body += u";\n"_s;
3534}
3535
3536void QQmlJSCodeGenerator::generate_Mul(int lhs)
3537{
3538 INJECT_TRACE_INFO(generate_Mul);
3539 generateArithmeticOperation(lhs, u"*"_s);
3540}
3541
3542void QQmlJSCodeGenerator::generate_Div(int lhs)
3543{
3544 INJECT_TRACE_INFO(generate_Div);
3545 GeneratePragmaWarningBlock warningBlock(this);
3546 warningBlock.silenceDivideByZero();
3547 generateArithmeticOperation(lhs, u"/"_s);
3548}
3549
3550void QQmlJSCodeGenerator::generate_Mod(int lhs)
3551{
3552 INJECT_TRACE_INFO(generate_Mod);
3553
3554 const auto lhsVar = convertStored(
3555 registerType(lhs).storedType(), m_typeResolver->jsPrimitiveType(),
3556 consumedRegisterVariable(lhs));
3557 const auto rhsVar = convertStored(
3558 m_state.accumulatorIn().storedType(), m_typeResolver->jsPrimitiveType(),
3559 consumedAccumulatorVariableIn());
3560 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !lhsVar.isEmpty());
3561 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !rhsVar.isEmpty());
3562
3563 m_body += m_state.accumulatorVariableOut;
3564 m_body += u" = "_s;
3565 m_body += conversion(m_typeResolver->jsPrimitiveType(), m_state.accumulatorOut(),
3566 u'(' + lhsVar + u" % "_s + rhsVar + u')');
3567 m_body += u";\n"_s;
3568}
3569
3570void QQmlJSCodeGenerator::generate_Sub(int lhs)
3571{
3572 INJECT_TRACE_INFO(generate_Sub);
3573 generateArithmeticOperation(lhs, u"-"_s);
3574}
3575
3576void QQmlJSCodeGenerator::generate_InitializeBlockDeadTemporalZone(int firstReg, int count)
3577{
3578 Q_UNUSED(firstReg)
3579 Q_UNUSED(count)
3580 // Ignore. We reject uninitialized values anyway.
3581}
3582
3583void QQmlJSCodeGenerator::generate_ThrowOnNullOrUndefined()
3584{
3586}
3587
3588void QQmlJSCodeGenerator::generate_GetTemplateObject(int index)
3589{
3590 Q_UNUSED(index)
3592}
3593
3594QV4::Moth::ByteCodeHandler::Verdict QQmlJSCodeGenerator::startInstruction(
3595 QV4::Moth::Instr::Type type)
3596{
3597 m_state.State::operator=(nextStateFromAnnotations(m_state, m_annotations));
3598 const auto accumulatorIn = m_state.registers.find(Accumulator);
3599 if (accumulatorIn != m_state.registers.end()
3600 && isTypeStorable(m_typeResolver, accumulatorIn.value().content.storedType())) {
3601 QQmlJSRegisterContent content = accumulatorIn.value().content;
3602 m_state.accumulatorVariableIn = m_registerVariables.value(content).variableName;
3603 Q_ASSERT(!m_state.accumulatorVariableIn.isEmpty());
3604 } else {
3605 m_state.accumulatorVariableIn.clear();
3606 }
3607
3608 auto labelIt = m_labels.constFind(currentInstructionOffset());
3609 if (labelIt != m_labels.constEnd()) {
3610 m_body += *labelIt + u":;\n"_s;
3611 m_skipUntilNextLabel = false;
3612 } else if (m_skipUntilNextLabel && !instructionManipulatesContext(type)) {
3613 return SkipInstruction;
3614 }
3615
3616 if (m_state.changedRegisterIndex() == Accumulator)
3617 m_state.accumulatorVariableOut = changedRegisterVariable();
3618 else
3619 m_state.accumulatorVariableOut.clear();
3620
3621 // If the accumulator type is valid, we want an accumulator variable.
3622 // If not, we don't want one.
3623 Q_ASSERT(m_state.changedRegisterIndex() == Accumulator
3624 || m_state.accumulatorVariableOut.isEmpty());
3625 Q_ASSERT(m_state.changedRegisterIndex() != Accumulator
3626 || !m_state.accumulatorVariableOut.isEmpty()
3627 || !isTypeStorable(m_typeResolver, m_state.changedRegister().storedType()));
3628
3629 // If the instruction has no side effects and doesn't write any register, it's dead.
3630 // We might still need the label, though, and the source code comment.
3631 if (!m_state.hasInternalSideEffects() && changedRegisterVariable().isEmpty()) {
3632 generateJumpCodeWithTypeConversions(0);
3633 return SkipInstruction;
3634 }
3635
3636 return ProcessInstruction;
3637}
3638
3639void QQmlJSCodeGenerator::endInstruction(QV4::Moth::Instr::Type)
3640{
3641 if (!m_skipUntilNextLabel)
3642 generateJumpCodeWithTypeConversions(0);
3643 m_pool->clearTemporaries();
3644}
3645
3646void QQmlJSCodeGenerator::generateSetInstructionPointer()
3647{
3648 m_body += u"aotContext->setInstructionPointer("_s
3649 + QString::number(nextInstructionOffset()) + u");\n"_s;
3650}
3651
3652void QQmlJSCodeGenerator::generateExceptionCheck()
3653{
3654 m_body += u"if (aotContext->engine->hasError()) {\n"_s;
3655 generateReturnError();
3656 m_body += u"}\n"_s;
3657}
3658
3659void QQmlJSCodeGenerator::generateEqualityOperation(
3660 QQmlJSRegisterContent lhsContent, QQmlJSRegisterContent rhsContent,
3661 const QString &lhsName, const QString &rhsName, const QString &function, bool invert)
3662{
3663 const bool lhsIsOptional = m_typeResolver->isOptionalType(lhsContent);
3664 const bool rhsIsOptional = m_typeResolver->isOptionalType(rhsContent);
3665
3666 const QQmlJSScope::ConstPtr rhsContained = rhsIsOptional
3667 ? m_typeResolver->extractNonVoidFromOptionalType(rhsContent).containedType()
3668 : rhsContent.containedType();
3669
3670 const QQmlJSScope::ConstPtr lhsContained = lhsIsOptional
3671 ? m_typeResolver->extractNonVoidFromOptionalType(lhsContent).containedType()
3672 : lhsContent.containedType();
3673
3674 const bool isStrict = function == "strictlyEquals"_L1;
3675 const bool strictlyComparableWithVar = isStrict
3676 && QQmlJSUtils::canStrictlyCompareWithVar(m_typeResolver, lhsContained, rhsContained);
3677 auto isComparable = [&]() {
3678 if (m_typeResolver->isPrimitive(lhsContent) && m_typeResolver->isPrimitive(rhsContent))
3679 return true;
3680 if (m_typeResolver->isNumeric(lhsContent) && m_typeResolver->isNumeric(rhsContent))
3681 return true;
3682 if (m_typeResolver->isNumeric(lhsContent) && rhsContent.isEnumeration())
3683 return true;
3684 if (m_typeResolver->isNumeric(rhsContent) && lhsContent.isEnumeration())
3685 return true;
3686 if (strictlyComparableWithVar)
3687 return true;
3688 if (QQmlJSUtils::canCompareWithQObject(m_typeResolver, lhsContained, rhsContained))
3689 return true;
3690 if (QQmlJSUtils::canCompareWithQUrl(m_typeResolver, lhsContained, rhsContained))
3691 return true;
3692 return false;
3693 };
3694
3695 const auto retrieveOriginal = [this](QQmlJSRegisterContent content) {
3696 const auto contained = content.containedType();
3697 const auto originalContent = originalType(content);
3698 const auto containedOriginal = originalContent.containedType();
3699
3700 if (originalContent.isStoredIn(m_typeResolver->genericType(containedOriginal))) {
3701 // The original type doesn't need any wrapping.
3702 return originalContent;
3703 } else if (contained == containedOriginal) {
3704 if (originalContent.isConversion()) {
3705 // The original conversion origins are more accurate
3706 return m_pool->storedIn(originalContent, content.storedType());
3707 }
3708 } else if (m_typeResolver->canHold(contained, containedOriginal)) {
3709 return m_pool->storedIn(originalContent, content.storedType());
3710 }
3711
3712 return content;
3713 };
3714
3715 const QQmlJSScope::ConstPtr lhsType = lhsContent.storedType();
3716 const QQmlJSScope::ConstPtr rhsType = rhsContent.storedType();
3717
3718 if (!isComparable()) {
3719 QQmlJSRegisterContent lhsOriginal = retrieveOriginal(lhsContent);
3720 QQmlJSRegisterContent rhsOriginal = retrieveOriginal(rhsContent);
3721 if (lhsOriginal.containedType() != lhsContent.containedType()
3722 || lhsOriginal.storedType() != lhsType
3723 || rhsOriginal.containedType() != rhsContent.containedType()
3724 || rhsOriginal.storedType() != rhsType) {
3725 // If either side is simply a wrapping of a specific type into a more general one, we
3726 // can compare the original types instead. You can't nest wrappings after all.
3727 generateEqualityOperation(lhsOriginal, rhsOriginal,
3728 conversion(lhsType, lhsOriginal, lhsName),
3729 conversion(rhsType, rhsOriginal, rhsName),
3730 function, invert);
3731 return;
3732 }
3733
3734 REJECT(u"incomparable types %1 and %2"_s.arg(
3735 rhsContent.descriptiveName(), lhsContent.descriptiveName()));
3736 }
3737
3738 if (strictlyComparableWithVar) {
3739 // Determine which side is holding a storable type
3740 if (!lhsName.isEmpty() && rhsName.isEmpty()) {
3741 // lhs register holds var type and rhs is not storable
3742 generateVariantEqualityComparison(rhsContent, lhsName, invert);
3743 return;
3744 }
3745
3746 if (!rhsName.isEmpty() && lhsName.isEmpty()) {
3747 // lhs content is not storable and rhs is var type
3748 generateVariantEqualityComparison(lhsContent, rhsName, invert);
3749 return;
3750 }
3751
3752 if (lhsContent.contains(m_typeResolver->varType())) {
3753 generateVariantEqualityComparison(rhsContent, rhsName, lhsName, invert);
3754 return;
3755 }
3756
3757 if (rhsContent.contains(m_typeResolver->varType())) {
3758 generateVariantEqualityComparison(lhsContent, lhsName, rhsName, invert);
3759 return;
3760 }
3761
3762 // It shouldn't be possible to get here because optional null should be stored in
3763 // QJSPrimitiveValue, not in QVariant. But let's rather be safe than sorry.
3764 REJECT(u"comparison of optional null"_s);
3765 }
3766
3767 const auto comparison = [&]() -> QString {
3768 const auto primitive = m_typeResolver->jsPrimitiveType();
3769 const QString sign = invert ? u" != "_s : u" == "_s;
3770
3771 if (lhsType == rhsType && lhsType != primitive && lhsType != m_typeResolver->varType()) {
3772
3773 // Straight forward comparison of equal types,
3774 // except QJSPrimitiveValue which has two comparison functions.
3775
3776 if (isTypeStorable(m_typeResolver, lhsType))
3777 return lhsName + sign + rhsName;
3778
3779 // null === null and undefined === undefined
3780 return invert ? u"false"_s : u"true"_s;
3781 }
3782
3783 if (QQmlJSUtils::canCompareWithQObject(m_typeResolver, lhsType, rhsType)) {
3784 // Comparison of QObject-derived with nullptr or different QObject-derived.
3785 return (isTypeStorable(m_typeResolver, lhsType) ? lhsName : u"nullptr"_s)
3786 + sign
3787 + (isTypeStorable(m_typeResolver, rhsType) ? rhsName : u"nullptr"_s);
3788 }
3789
3790 if (QQmlJSUtils::canCompareWithQObject(m_typeResolver, lhsContained, rhsContained)) {
3791 // Comparison of optional QObject-derived with nullptr or different QObject-derived.
3792 // Mind that null == undefined but null !== undefined
3793 // Therefore the isStrict dance.
3794
3795 QString result;
3796 if (isStrict) {
3797 if (lhsIsOptional) {
3798 if (rhsIsOptional) {
3799 // If both are invalid we're fine
3800 result += u"(!"_s
3801 + lhsName + u".isValid() && !"_s
3802 + rhsName + u".isValid()) || "_s;
3803 }
3804
3805 result += u'(' + lhsName + u".isValid() && "_s;
3806 } else {
3807 result += u'(';
3808 }
3809
3810 if (rhsIsOptional) {
3811 result += rhsName + u".isValid() && "_s;
3812 }
3813 } else {
3814 result += u'(';
3815 }
3816
3817 // We do not implement comparison with explicit undefined, yet. Only with null.
3818 Q_ASSERT(lhsType != m_typeResolver->voidType());
3819 Q_ASSERT(rhsType != m_typeResolver->voidType());
3820
3821 const auto resolvedName = [&](const QString name) -> QString {
3822 // If isStrict we check validity already before.
3823 const QString content = u"*static_cast<QObject **>("_s + name + u".data())"_s;
3824 return isStrict
3825 ? content
3826 : u'(' + name + u".isValid() ? "_s + content + u" : nullptr)"_s;
3827 };
3828
3829 const QString lhsResolved = lhsIsOptional ? resolvedName(lhsName) : lhsName;
3830 const QString rhsResolved = rhsIsOptional ? resolvedName(rhsName) : rhsName;
3831
3832 return (invert ? u"!("_s : u"("_s) + result
3833 + (isTypeStorable(m_typeResolver, lhsType) ? lhsResolved : u"nullptr"_s)
3834 + u" == "_s
3835 + (isTypeStorable(m_typeResolver, rhsType) ? rhsResolved : u"nullptr"_s)
3836 + u"))"_s;
3837 }
3838
3839 if ((m_typeResolver->isUnsignedInteger(rhsType)
3840 && m_typeResolver->isUnsignedInteger(lhsType))
3841 || (m_typeResolver->isSignedInteger(rhsType)
3842 && m_typeResolver->isSignedInteger(lhsType))) {
3843 // Both integers of same signedness: Let the C++ compiler perform the type promotion
3844 return lhsName + sign + rhsName;
3845 }
3846
3847 if (rhsType == m_typeResolver->boolType() && m_typeResolver->isIntegral(lhsType)) {
3848 // Integral and bool: We can promote the bool to the integral type
3849 return lhsName + sign + convertStored(rhsType, lhsType, rhsName);
3850 }
3851
3852 if (lhsType == m_typeResolver->boolType() && m_typeResolver->isIntegral(rhsType)) {
3853 // Integral and bool: We can promote the bool to the integral type
3854 return convertStored(lhsType, rhsType, lhsName) + sign + rhsName;
3855 }
3856
3857 if (m_typeResolver->isNumeric(lhsType) && m_typeResolver->isNumeric(rhsType)) {
3858 // Both numbers: promote them to double
3859 return convertStored(lhsType, m_typeResolver->realType(), lhsName)
3860 + sign
3861 + convertStored(rhsType, m_typeResolver->realType(), rhsName);
3862 }
3863
3864 // If none of the above matches, we have to use QJSPrimitiveValue
3865 return (invert ? u"!"_s : QString())
3866 + convertStored(lhsType, primitive, lhsName)
3867 + u'.' + function + u'(' + convertStored(rhsType, primitive, rhsName) + u')';
3868 };
3869
3870 m_body += m_state.accumulatorVariableOut + u" = "_s;
3871 m_body += conversion(m_typeResolver->boolType(), m_state.accumulatorOut(), comparison());
3872 m_body += u";\n"_s;
3873}
3874
3875void QQmlJSCodeGenerator::generateCompareOperation(int lhs, const QString &cppOperator)
3876{
3877 m_body += m_state.accumulatorVariableOut + u" = "_s;
3878
3879 const auto lhsType = registerType(lhs);
3880 const QQmlJSScope::ConstPtr compareType =
3881 m_typeResolver->isNumeric(lhsType) && m_typeResolver->isNumeric(m_state.accumulatorIn())
3882 ? m_typeResolver->merge(lhsType.storedType(), m_state.accumulatorIn().storedType())
3883 : m_typeResolver->jsPrimitiveType();
3884
3885 m_body += conversion(
3886 m_typeResolver->boolType(), m_state.accumulatorOut(),
3887 convertStored(registerType(lhs).storedType(), compareType,
3888 consumedRegisterVariable(lhs))
3889 + u' ' + cppOperator + u' '
3890 + convertStored(m_state.accumulatorIn().storedType(), compareType,
3891 consumedAccumulatorVariableIn()));
3892 m_body += u";\n"_s;
3893}
3894
3895void QQmlJSCodeGenerator::generateArithmeticOperation(int lhs, const QString &cppOperator)
3896{
3897 generateArithmeticOperation(
3898 conversion(registerType(lhs), m_state.readRegister(lhs),
3899 consumedRegisterVariable(lhs)),
3900 conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3901 consumedAccumulatorVariableIn()),
3902 cppOperator);
3903}
3904
3905void QQmlJSCodeGenerator::generateShiftOperation(int lhs, const QString &cppOperator)
3906{
3907 generateArithmeticOperation(
3908 conversion(registerType(lhs), m_state.readRegister(lhs),
3909 consumedRegisterVariable(lhs)),
3910 u'(' + conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3911 consumedAccumulatorVariableIn()) + u" & 0x1f)"_s,
3912 cppOperator);
3913}
3914
3915void QQmlJSCodeGenerator::generateArithmeticOperation(
3916 const QString &lhs, const QString &rhs, const QString &cppOperator)
3917{
3918 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !lhs.isEmpty());
3919 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !rhs.isEmpty());
3920
3921 const QQmlJSRegisterContent originalOut = originalType(m_state.accumulatorOut());
3922 m_body += m_state.accumulatorVariableOut;
3923 m_body += u" = "_s;
3924 const QString explicitCast
3925 = originalOut.isStoredIn(m_typeResolver->stringType())
3926 ? originalOut.storedType()->internalName()
3927 : QString();
3928 m_body += conversion(
3929 originalOut, m_state.accumulatorOut(),
3930 explicitCast + u'(' + lhs + u' ' + cppOperator + u' ' + rhs + u')');
3931 m_body += u";\n"_s;
3932}
3933
3934void QQmlJSCodeGenerator::generateArithmeticConstOperation(int rhsConst, const QString &cppOperator)
3935{
3936 generateArithmeticOperation(
3937 conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3938 consumedAccumulatorVariableIn()),
3939 conversion(literalType(m_typeResolver->int32Type()),
3940 m_state.readAccumulator(), QString::number(rhsConst)),
3941 cppOperator);
3942}
3943
3944void QQmlJSCodeGenerator::generateUnaryOperation(const QString &cppOperator)
3945{
3946 const auto var = conversion(m_state.accumulatorIn(),
3947 originalType(m_state.readAccumulator()),
3948 consumedAccumulatorVariableIn());
3949
3950 if (var == m_state.accumulatorVariableOut) {
3951 m_body += m_state.accumulatorVariableOut + u" = "_s + cppOperator + var + u";\n"_s;
3952 return;
3953 }
3954
3955 const auto originalResult = originalType(m_state.accumulatorOut());
3956 if (m_state.accumulatorOut() == originalResult) {
3957 m_body += m_state.accumulatorVariableOut + u" = "_s + var + u";\n"_s;
3958 m_body += m_state.accumulatorVariableOut + u" = "_s
3959 + cppOperator + m_state.accumulatorVariableOut + u";\n"_s;
3960 return;
3961 }
3962
3963 m_body += m_state.accumulatorVariableOut + u" = "_s + conversion(
3964 originalResult, m_state.accumulatorOut(), cppOperator + var) + u";\n"_s;
3965}
3966
3967void QQmlJSCodeGenerator::generateInPlaceOperation(const QString &cppOperator)
3968{
3969 {
3970 // If actually in place, we cannot consume the variable.
3971 const QString var = conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3972 m_state.accumulatorVariableIn);
3973 if (var == m_state.accumulatorVariableOut) {
3974 m_body += cppOperator + var + u";\n"_s;
3975 return;
3976 }
3977 }
3978
3979 const QString var = conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3980 consumedAccumulatorVariableIn());
3981
3982 const auto originalResult = originalType(m_state.accumulatorOut());
3983 if (m_state.accumulatorOut() == originalResult) {
3984 m_body += m_state.accumulatorVariableOut + u" = "_s + var + u";\n"_s;
3985 m_body += cppOperator + m_state.accumulatorVariableOut + u";\n"_s;
3986 return;
3987 }
3988
3989 m_body += u"{\n"_s;
3990 m_body += u"auto converted = "_s + var + u";\n"_s;
3991 m_body += m_state.accumulatorVariableOut + u" = "_s + conversion(
3992 originalResult, m_state.accumulatorOut(), u'('
3993 + cppOperator + u"converted)"_s) + u";\n"_s;
3994 m_body += u"}\n"_s;
3995}
3996
3997void QQmlJSCodeGenerator::generateLookup(const QString &lookup, const QString &initialization,
3998 const QString &resultPreparation)
3999{
4000 m_body += u"#ifndef QT_NO_DEBUG\n"_s;
4001 generateSetInstructionPointer();
4002 m_body += u"#endif\n"_s;
4003
4004 if (!resultPreparation.isEmpty())
4005 m_body += resultPreparation + u";\n"_s;
4006 m_body += u"while (!"_s + lookup + u") {\n"_s;
4007
4008 m_body += u"#ifdef QT_NO_DEBUG\n"_s;
4009 generateSetInstructionPointer();
4010 m_body += u"#endif\n"_s;
4011
4012 m_body += initialization + u";\n"_s;
4013 generateExceptionCheck();
4014 if (!resultPreparation.isEmpty())
4015 m_body += resultPreparation + u";\n"_s;
4016 m_body += u"}\n"_s;
4017}
4018
4019void QQmlJSCodeGenerator::generateJumpCodeWithTypeConversions(int relativeOffset)
4020{
4021 QString conversionCode;
4022 const int absoluteOffset = nextInstructionOffset() + relativeOffset;
4023 const auto annotation = m_annotations.find(absoluteOffset);
4024 if (static_cast<InstructionAnnotations::const_iterator>(annotation) != m_annotations.constEnd()) {
4025 const auto &conversions = annotation->second.typeConversions;
4026
4027 for (auto regIt = conversions.constBegin(), regEnd = conversions.constEnd();
4028 regIt != regEnd; ++regIt) {
4029 const QQmlJSRegisterContent targetType = regIt.value().content;
4030 if (!targetType.isValid() || !isTypeStorable(m_typeResolver, targetType.storedType()))
4031 continue;
4032
4033 const int registerIndex = regIt.key();
4034 const auto variable = m_registerVariables.constFind(targetType);
4035
4036 if (variable == m_registerVariables.constEnd())
4037 continue;
4038
4039 QQmlJSRegisterContent currentType;
4040 QString currentVariable;
4041 if (registerIndex == m_state.changedRegisterIndex()) {
4042 currentVariable = changedRegisterVariable();
4043 if (variable->variableName == currentVariable)
4044 continue;
4045
4046 currentType = m_state.changedRegister();
4047
4048 // TODO: We can std::move the changed register in some cases here but it needs be
4049 // done carefully (QTBUG-141920).
4050 } else {
4051 const auto it = m_state.registers.find(registerIndex);
4052 if (it == m_state.registers.end()
4053 || variable->variableName == registerVariable(registerIndex)) {
4054 continue;
4055 }
4056
4057 currentType = it.value().content;
4058 currentVariable = consumedRegisterVariable(registerIndex);
4059 }
4060
4061 // Actually == here. We want the jump code also for equal types
4062 if (currentType == targetType)
4063 continue;
4064
4065 conversionCode += variable->variableName;
4066 conversionCode += u" = "_s;
4067 conversionCode += conversion(currentType, targetType, currentVariable);
4068 conversionCode += u";\n"_s;
4069 }
4070 }
4071
4072 if (relativeOffset) {
4073 auto labelIt = m_labels.find(absoluteOffset);
4074 if (labelIt == m_labels.end())
4075 labelIt = m_labels.insert(absoluteOffset, u"label_%1"_s.arg(m_labels.size()));
4076 conversionCode += u" goto "_s + *labelIt + u";\n"_s;
4077 }
4078
4079 m_body += u"{\n"_s + conversionCode + u"}\n"_s;
4080}
4081
4082QString QQmlJSCodeGenerator::registerVariable(int index) const
4083{
4084 QQmlJSRegisterContent content = registerType(index);
4085 const auto it = m_registerVariables.constFind(content);
4086 if (it != m_registerVariables.constEnd())
4087 return it->variableName;
4088
4089 return QString();
4090}
4091
4092QString QQmlJSCodeGenerator::lookupVariable(int lookupIndex) const
4093{
4094 for (auto it = m_registerVariables.constBegin(), end = m_registerVariables.constEnd(); it != end; ++it) {
4095 if (it.key().resultLookupIndex() == lookupIndex)
4096 return it->variableName;
4097 }
4098 return QString();
4099}
4100
4101QString QQmlJSCodeGenerator::consumedRegisterVariable(int index) const
4102{
4103 const QString var = registerVariable(index);
4104 if (var.isEmpty() || !shouldMoveRegister(index))
4105 return var;
4106 return u"std::move(" + var + u")";
4107}
4108
4109QString QQmlJSCodeGenerator::consumedAccumulatorVariableIn() const
4110{
4111 return shouldMoveRegister(Accumulator)
4112 ? u"std::move(" + m_state.accumulatorVariableIn + u")"
4113 : m_state.accumulatorVariableIn;
4114}
4115
4116QString QQmlJSCodeGenerator::changedRegisterVariable() const
4117{
4118 QQmlJSRegisterContent changedRegister = m_state.changedRegister();
4119
4120 const QQmlJSScope::ConstPtr storedType = changedRegister.storedType();
4121 if (storedType.isNull())
4122 return QString();
4123
4124 return m_registerVariables.value(changedRegister).variableName;
4125}
4126
4127QQmlJSRegisterContent QQmlJSCodeGenerator::registerType(int index) const
4128{
4129 auto it = m_state.registers.find(index);
4130 if (it != m_state.registers.end())
4131 return it.value().content;
4132
4133 return QQmlJSRegisterContent();
4134}
4135
4136QQmlJSRegisterContent QQmlJSCodeGenerator::lookupType(int lookupIndex) const
4137{
4138 auto it = m_state.lookups.find(lookupIndex);
4139 if (it != m_state.lookups.end())
4140 return it.value().content;
4141
4142 return QQmlJSRegisterContent();
4143}
4144
4145bool QQmlJSCodeGenerator::shouldMoveRegister(int index) const
4146{
4147 return m_state.canMoveReadRegister(index)
4148 && !m_typeResolver->isTriviallyCopyable(m_state.readRegister(index).storedType());
4149}
4150
4151QString QQmlJSCodeGenerator::conversion(
4152 QQmlJSRegisterContent from, QQmlJSRegisterContent to, const QString &variable)
4153{
4154 const QQmlJSScope::ConstPtr contained = to.containedType();
4155
4156 // If from is QJSPrimitiveValue and to contains a primitive we coerce using QJSPrimitiveValue
4157 if (from.isStoredIn(m_typeResolver->jsPrimitiveType()) && m_typeResolver->isPrimitive(to)) {
4158
4159 QString primitive = [&]() -> QString {
4160 if (contained == m_typeResolver->jsPrimitiveType())
4161 return variable;
4162
4163 const QString conversion = variable + u".to<QJSPrimitiveValue::%1>()"_s;
4164 if (contained == m_typeResolver->boolType())
4165 return conversion.arg(u"Boolean"_s);
4166 if (m_typeResolver->isIntegral(to))
4167 return conversion.arg(u"Integer"_s);
4168 if (m_typeResolver->isNumeric(to))
4169 return conversion.arg(u"Double"_s);
4170 if (contained == m_typeResolver->stringType())
4171 return conversion.arg(u"String"_s);
4172 REJECT<QString>(
4173 u"Conversion of QJSPrimitiveValue to "_s + contained->internalName());
4174 }();
4175
4176 if (primitive.isEmpty())
4177 return primitive;
4178
4179 return convertStored(m_typeResolver->jsPrimitiveType(), to.storedType(), primitive);
4180 }
4181
4182 if (to.isStoredIn(contained)
4183 || m_typeResolver->isNumeric(to.storedType())
4184 || to.storedType()->isReferenceType()
4185 || from.contains(contained)) {
4186 // If:
4187 // * the output is not actually wrapped at all, or
4188 // * the output is stored in a numeric type (as there are no internals to a number), or
4189 // * the output is a QObject pointer, or
4190 // * we merely wrap the value into a new container,
4191 // we can convert by stored type.
4192 return convertStored(from.storedType(), to.storedType(), variable);
4193 } else {
4194 return convertContained(from, to, variable);
4195 }
4196}
4197
4198QString QQmlJSCodeGenerator::convertStored(
4199 const QQmlJSScope::ConstPtr &from, const QQmlJSScope::ConstPtr &to, const QString &variable)
4200{
4201 // TODO: most values can be moved, which is much more efficient with the common types.
4202 // add a move(from, to, variable) function that implements the moves.
4203 Q_ASSERT(!to->isComposite()); // We cannot directly convert to composites.
4204
4205 const auto jsValueType = m_typeResolver->jsValueType();
4206 const auto varType = m_typeResolver->varType();
4207 const auto jsPrimitiveType = m_typeResolver->jsPrimitiveType();
4208 const auto boolType = m_typeResolver->boolType();
4209
4210 auto zeroBoolOrInt = [&](const QQmlJSScope::ConstPtr &to) {
4211 if (to == boolType)
4212 return u"false"_s;
4213 if (m_typeResolver->isSignedInteger(to))
4214 return u"0"_s;
4215 if (m_typeResolver->isUnsignedInteger(to))
4216 return u"0u"_s;
4217 return QString();
4218 };
4219
4220 if (from == m_typeResolver->voidType()) {
4221 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
4222 return u"static_cast<"_s + to->internalName() + u" *>(nullptr)"_s;
4223 const QString zero = zeroBoolOrInt(to);
4224 if (!zero.isEmpty())
4225 return zero;
4226 if (to == m_typeResolver->floatType())
4227 return u"std::numeric_limits<float>::quiet_NaN()"_s;
4228 if (to == m_typeResolver->realType())
4229 return u"std::numeric_limits<double>::quiet_NaN()"_s;
4230 if (to == m_typeResolver->stringType())
4231 return QQmlJSUtils::toLiteral(u"undefined"_s);
4232 if (to == m_typeResolver->varType())
4233 return u"QVariant()"_s;
4234 if (to == m_typeResolver->jsValueType())
4235 return u"QJSValue();"_s;
4236 if (to == m_typeResolver->jsPrimitiveType())
4237 return u"QJSPrimitiveValue()"_s;
4238 if (from == to)
4239 return QString();
4240 }
4241
4242 if (from == m_typeResolver->nullType()) {
4243 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
4244 return u"static_cast<"_s + to->internalName() + u" *>(nullptr)"_s;
4245 if (to == jsValueType)
4246 return u"QJSValue(QJSValue::NullValue)"_s;
4247 if (to == jsPrimitiveType)
4248 return u"QJSPrimitiveValue(QJSPrimitiveNull())"_s;
4249 if (to == varType)
4250 return u"QVariant::fromValue<std::nullptr_t>(nullptr)"_s;
4251 const QString zero = zeroBoolOrInt(to);
4252 if (!zero.isEmpty())
4253 return zero;
4254 if (to == m_typeResolver->floatType())
4255 return u"0.0f"_s;
4256 if (to == m_typeResolver->realType())
4257 return u"0.0"_s;
4258 if (to == m_typeResolver->stringType())
4259 return QQmlJSUtils::toLiteral(u"null"_s);
4260 if (from == to)
4261 return QString();
4262 REJECT<QString>(u"Conversion from null to %1"_s.arg(to->internalName()));
4263 }
4264
4265 if (from == to)
4266 return variable;
4267
4268 if (from->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
4269 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
4270 // Compare internalName here. The same C++ type can be exposed muliple times in
4271 // different QML types. However, the C++ names have to be unique. We can always
4272 // static_cast to those.
4273
4274 for (QQmlJSScope::ConstPtr base = from; base; base = base->baseType()) {
4275 // We still have to cast as other execution paths may result in different types.
4276 if (base->internalName() == to->internalName())
4277 return u"static_cast<"_s + to->internalName() + u" *>("_s + variable + u')';
4278 }
4279 for (QQmlJSScope::ConstPtr base = to; base; base = base->baseType()) {
4280 if (base->internalName() == from->internalName())
4281 return u"static_cast<"_s + to->internalName() + u" *>("_s + variable + u')';
4282 }
4283 } else if (to == m_typeResolver->boolType()) {
4284 return u'(' + variable + u" != nullptr)"_s;
4285 }
4286 }
4287
4288 auto isJsValue = [&](const QQmlJSScope::ConstPtr &candidate) {
4289 return candidate == jsValueType || candidate->isScript();
4290 };
4291
4292 if (isJsValue(from) && isJsValue(to))
4293 return variable;
4294
4295 const auto isBoolOrNumber = [&](const QQmlJSScope::ConstPtr &type) {
4296 return m_typeResolver->isNumeric(type)
4297 || type == m_typeResolver->boolType()
4298 || type->scopeType() == QQmlSA::ScopeType::EnumScope;
4299 };
4300
4301 if (from == m_typeResolver->realType() || from == m_typeResolver->floatType()) {
4302 if (to == m_typeResolver->int64Type() || to == m_typeResolver->uint64Type()) {
4303 return to->internalName() + u"(QJSNumberCoercion::roundTowards0("_s
4304 + variable + u"))"_s;
4305 }
4306
4307 if (m_typeResolver->isSignedInteger(to))
4308 return u"QJSNumberCoercion::toInteger("_s + variable + u')';
4309 if (m_typeResolver->isUnsignedInteger(to))
4310 return u"uint(QJSNumberCoercion::toInteger("_s + variable + u"))"_s;
4311 if (to == m_typeResolver->boolType())
4312 return u"[](double moved){ return moved && !std::isnan(moved); }("_s + variable + u')';
4313 }
4314
4315 if (isBoolOrNumber(from) && isBoolOrNumber(to))
4316 return to->internalName() + u'(' + variable + u')';
4317
4318
4319 if (from == jsPrimitiveType) {
4320 if (to == boolType)
4321 return variable + u".toBoolean()"_s;
4322 if (to == m_typeResolver->int64Type() || to == m_typeResolver->uint64Type())
4323 return u"%1(%2.toDouble())"_s.arg(to->internalName(), variable);
4324 if (m_typeResolver->isIntegral(to))
4325 return u"%1(%2.toInteger())"_s.arg(to->internalName(), variable);
4326 if (m_typeResolver->isNumeric(to))
4327 return u"%1(%2.toDouble())"_s.arg(to->internalName(), variable);
4328 if (to == m_typeResolver->stringType())
4329 return variable + u".toString()"_s;
4330 if (to == jsValueType)
4331 return u"QJSValue(QJSPrimitiveValue("_s + variable + u"))"_s;
4332 if (to == varType)
4333 return variable + u".toVariant()"_s;
4334 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
4335 return u"static_cast<"_s + to->internalName() + u" *>(nullptr)"_s;
4336 }
4337
4338 if (isJsValue(from)) {
4339 if (to == jsPrimitiveType)
4340 return variable + u".toPrimitive()"_s;
4341 if (to == varType)
4342 return variable + u".toVariant(QJSValue::RetainJSObjects)"_s;
4343 return u"qjsvalue_cast<"_s + castTargetName(to) + u">("_s + variable + u')';
4344 }
4345
4346 if (to == jsPrimitiveType) {
4347 // null and undefined have been handled above already
4348 Q_ASSERT(from != m_typeResolver->nullType());
4349 Q_ASSERT(from != m_typeResolver->voidType());
4350
4351 if (from == m_typeResolver->boolType()
4352 || from == m_typeResolver->int32Type()
4353 || from == m_typeResolver->realType()
4354 || from == m_typeResolver->stringType()) {
4355 return u"QJSPrimitiveValue("_s + variable + u')';
4356 } else if (from == m_typeResolver->int16Type()
4357 || from == m_typeResolver->int8Type()
4358 || from == m_typeResolver->uint16Type()
4359 || from == m_typeResolver->uint8Type()) {
4360 return u"QJSPrimitiveValue(int("_s + variable + u"))"_s;
4361 } else if (m_typeResolver->isNumeric(from)) {
4362 return u"QJSPrimitiveValue(double("_s + variable + u"))"_s;
4363 }
4364 }
4365
4366 if (to == jsValueType)
4367 return u"aotContext->engine->toScriptValue("_s + variable + u')';
4368
4369 if (from == varType) {
4370 if (to == m_typeResolver->listPropertyType())
4371 return u"QQmlListReference("_s + variable + u", aotContext->qmlEngine())"_s;
4372 return u"aotContext->engine->fromVariant<"_s + castTargetName(to) + u">("_s
4373 + variable + u')';
4374 }
4375
4376 if (to == varType)
4377 return u"QVariant::fromValue("_s + variable + u')';
4378
4379 if (from == m_typeResolver->urlType() && to == m_typeResolver->stringType())
4380 return variable + u".toString()"_s;
4381
4382 if (from == m_typeResolver->stringType() && to == m_typeResolver->urlType())
4383 return u"QUrl("_s + variable + u')';
4384
4385 if (from == m_typeResolver->byteArrayType() && to == m_typeResolver->stringType())
4386 return u"QString::fromUtf8("_s + variable + u')';
4387
4388 if (from == m_typeResolver->stringType() && to == m_typeResolver->byteArrayType())
4389 return variable + u".toUtf8()"_s;
4390
4391 for (const auto &originType : {
4392 m_typeResolver->dateTimeType(),
4393 m_typeResolver->dateType(),
4394 m_typeResolver->timeType()}) {
4395 if (from == originType) {
4396 for (const auto &targetType : {
4397 m_typeResolver->dateTimeType(),
4398 m_typeResolver->dateType(),
4399 m_typeResolver->timeType(),
4400 m_typeResolver->stringType(),
4401 m_typeResolver->realType()}) {
4402 if (to == targetType) {
4403 return u"aotContext->engine->coerceValue<%1, %2>(%3)"_s.arg(
4404 originType->internalName(), targetType->internalName(), variable);
4405 }
4406 }
4407 break;
4408 }
4409 }
4410
4411 const auto retrieveFromPrimitive = [&](
4412 const QQmlJSScope::ConstPtr &type, const QString &expression) -> QString
4413 {
4414 if (type == m_typeResolver->boolType())
4415 return expression + u".toBoolean()"_s;
4416 if (m_typeResolver->isSignedInteger(type))
4417 return expression + u".toInteger()"_s;
4418 if (m_typeResolver->isUnsignedInteger(type))
4419 return u"uint("_s + expression + u".toInteger())"_s;
4420 if (type == m_typeResolver->realType())
4421 return expression + u".toDouble()"_s;
4422 if (type == m_typeResolver->floatType())
4423 return u"float("_s + expression + u".toDouble())"_s;
4424 if (type == m_typeResolver->stringType())
4425 return expression + u".toString()"_s;
4426 return QString();
4427 };
4428
4429 if (!retrieveFromPrimitive(from, u"x"_s).isEmpty()) {
4430 const QString retrieve = retrieveFromPrimitive(
4431 to, convertStored(from, m_typeResolver->jsPrimitiveType(), variable));
4432 if (!retrieve.isEmpty())
4433 return retrieve;
4434 }
4435
4436 if (from->isReferenceType() && to == m_typeResolver->stringType()) {
4437 return u"aotContext->engine->coerceValue<"_s + castTargetName(from) + u", "
4438 + castTargetName(to) + u">("_s + variable + u')';
4439 }
4440
4441 // Any value type is a non-null JS 'object' and therefore coerces to true.
4442 if (to == m_typeResolver->boolType()) {
4443 // All the interesting cases are already handled above:
4444 Q_ASSERT(from != m_typeResolver->nullType());
4445 Q_ASSERT(from != m_typeResolver->voidType());
4446 Q_ASSERT(retrieveFromPrimitive(from, u"x"_s).isEmpty());
4447 Q_ASSERT(!isBoolOrNumber(from));
4448
4449 return u"true"_s;
4450 }
4451
4452 if (m_typeResolver->areEquivalentLists(from, to))
4453 return variable;
4454
4455 if (from->isListProperty()
4456 && to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
4457 && to->elementType()->isReferenceType()
4458 && !to->isListProperty()) {
4459 return variable + u".toList<"_s + to->internalName() + u">()"_s;
4460 }
4461
4462 bool isExtension = false;
4463 if (m_typeResolver->canPopulate(to, from, &isExtension)) {
4464 REJECT<QString>(
4465 u"populating "_s + to->internalName() + u" from "_s + from->internalName());
4466 } else if (const auto ctor = m_typeResolver->selectConstructor(to, from, &isExtension);
4467 ctor.isValid()) {
4468 const auto argumentTypes = ctor.parameters();
4469 const QString argument = convertStored(from, argumentTypes[0].type(), variable);
4470 if (isExtension) {
4471 // We typically use private inheritance for the foreign/extension trick.
4472 // Therefore we need to jump through some hoops to extract the actual value here.
4473 return u"[](auto &&arg) { "
4474 "%1 wrapper(std::forward<decltype(arg)>(arg)); "
4475 "return %2(reinterpret_cast<%2 &>(wrapper)); "
4476 "}(%3)"_s.arg(
4477 to->extensionType().scope->internalName(), to->internalName(), argument);
4478 }
4479
4480 return u"%1(%2)"_s.arg(to->internalName(), argument);
4481 }
4482
4483 if (to == m_typeResolver->stringType()
4484 && from->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
4485 addInclude(u"QtQml/qjslist.h"_s);
4486
4487 // Extend the life time of whatever variable is across the call to toString().
4488 // variable may be an rvalue.
4489 return u"[&](auto &&l){ return QJSList(&l, aotContext->engine).toString(); }("_s
4490 + variable + u')';
4491 }
4492
4493 // TODO: add more conversions
4494
4495 REJECT<QString>(
4496 u"conversion from "_s + from->internalName() + u" to "_s + to->internalName());
4497}
4498
4499QString QQmlJSCodeGenerator::convertContained(QQmlJSRegisterContent from, QQmlJSRegisterContent to, const QString &variable)
4500{
4501 const QQmlJSScope::ConstPtr containedFrom = from.containedType();
4502 const QQmlJSScope::ConstPtr containedTo = to.containedType();
4503
4504 // Those should be handled before, by convertStored().
4505 Q_ASSERT(!to.storedType()->isReferenceType());
4506 Q_ASSERT(!to.isStoredIn(containedTo));
4507 Q_ASSERT(containedFrom != containedTo);
4508
4509 if (!to.isStoredIn(m_typeResolver->varType())
4510 && !to.isStoredIn(m_typeResolver->jsPrimitiveType())) {
4511 REJECT<QString>(u"internal conversion into unsupported wrapper type."_s);
4512 }
4513
4514 bool isExtension = false;
4515 if (m_typeResolver->canPopulate(containedTo, containedFrom, &isExtension)) {
4516 REJECT<QString>(u"populating "_s + containedTo->internalName()
4517 + u" from "_s + containedFrom->internalName());
4518 } else if (const auto ctor = m_typeResolver->selectConstructor(
4519 containedTo, containedFrom, &isExtension); ctor.isValid()) {
4520 return generateCallConstructor(
4521 ctor, {from}, {variable}, metaType(containedTo),
4522 metaObject(isExtension ? containedTo->extensionType().scope : containedTo));
4523 }
4524
4525 const auto originalFrom = originalType(from);
4526 const auto containedOriginalFrom = originalFrom.containedType();
4527 if (containedFrom != containedOriginalFrom
4528 && m_typeResolver->canHold(containedFrom, containedOriginalFrom)) {
4529 // If from is simply a wrapping of a specific type into a more general one, we can convert
4530 // the original type instead. You can't nest wrappings after all.
4531 return conversion(m_pool->storedIn(originalFrom, from.storedType()), to, variable);
4532 }
4533
4534 if (m_typeResolver->isPrimitive(containedFrom) && m_typeResolver->isPrimitive(containedTo)) {
4535 const QQmlJSRegisterContent intermediate
4536 = m_pool->storedIn(from, m_typeResolver->jsPrimitiveType());
4537 return conversion(intermediate, to, conversion(from, intermediate, variable));
4538 }
4539
4540 REJECT<QString>(
4541 u"internal conversion with incompatible or ambiguous types: %1 -> %2"_s
4542 .arg(from.descriptiveName(), to.descriptiveName()));
4543}
4544
4545void QQmlJSCodeGenerator::reject(const QString &thing)
4546{
4547 addError(u"Cannot generate efficient code for %1"_s.arg(thing));
4548}
4549
4550
4551void QQmlJSCodeGenerator::skip(const QString &thing)
4552{
4553 addSkip(u"Skipped code generation for %1"_s.arg(thing));
4554}
4555
4556QQmlJSCodeGenerator::AccumulatorConverter::AccumulatorConverter(QQmlJSCodeGenerator *generator)
4557 : accumulatorOut(generator->m_state.accumulatorOut())
4558 , accumulatorVariableIn(generator->m_state.accumulatorVariableIn)
4559 , accumulatorVariableOut(generator->m_state.accumulatorVariableOut)
4560 , generator(generator)
4561{
4562 if (accumulatorVariableOut.isEmpty())
4563 return;
4564
4565 const QQmlJSTypeResolver *resolver = generator->m_typeResolver;
4566 const QQmlJSScope::ConstPtr origContained = resolver->originalContainedType(accumulatorOut);
4567 const QQmlJSRegisterContent storage = accumulatorOut.storage();
4568 const QQmlJSScope::ConstPtr stored = storage.containedType();
4569 const QQmlJSScope::ConstPtr origStored = resolver->original(storage).containedType();
4570
4571
4572 // If the stored type differs or if we store in QVariant and the contained type differs,
4573 // then we have to use a temporary ...
4574 if (origStored != stored
4575 || (origContained != accumulatorOut.containedType() && stored == resolver->varType())) {
4576
4577 const bool storable = isTypeStorable(resolver, origStored);
4578 generator->m_state.accumulatorVariableOut = storable ? u"retrieved"_s : QString();
4579 generator->m_state.setRegister(Accumulator, generator->originalType(accumulatorOut));
4580 generator->m_body += u"{\n"_s;
4581 if (storable) {
4582 generator->m_body += origStored->augmentedInternalName() + u' '
4583 + generator->m_state.accumulatorVariableOut + u";\n";
4584 }
4585 } else if (generator->m_state.accumulatorVariableIn == generator->m_state.accumulatorVariableOut
4586 && generator->m_state.readsRegister(Accumulator)
4587 && generator->m_state.accumulatorOut().isStoredIn(resolver->varType())) {
4588 // If both m_state.accumulatorIn and m_state.accumulatorOut are QVariant, we will need to
4589 // prepare the output QVariant, and afterwards use the input variant. Therefore we need to
4590 // move the input out of the way first.
4591 generator->m_state.accumulatorVariableIn
4592 = generator->m_state.accumulatorVariableIn + u"_moved"_s;
4593 generator->m_body += u"{\n"_s;
4594 generator->m_body += u"QVariant "_s + generator->m_state.accumulatorVariableIn
4595 + u" = std::move("_s + generator->m_state.accumulatorVariableOut + u");\n"_s;
4596 }
4597}
4598
4599QQmlJSCodeGenerator::AccumulatorConverter::~AccumulatorConverter()
4600{
4601 if (accumulatorVariableOut != generator->m_state.accumulatorVariableOut) {
4602 generator->m_body += accumulatorVariableOut + u" = "_s + generator->conversion(
4603 generator->m_state.accumulatorOut(), accumulatorOut,
4604 u"std::move("_s + generator->m_state.accumulatorVariableOut + u')') + u";\n"_s;
4605 generator->m_body += u"}\n"_s;
4606 generator->m_state.setRegister(Accumulator, accumulatorOut);
4607 generator->m_state.accumulatorVariableOut = accumulatorVariableOut;
4608 } else if (accumulatorVariableIn != generator->m_state.accumulatorVariableIn) {
4609 generator->m_body += u"}\n"_s;
4610 generator->m_state.accumulatorVariableIn = accumulatorVariableIn;
4611 }
4612}
4613
4614
4615QT_END_NAMESPACE
4616
4617QQmlJSCodeGenerator::GeneratePragmaWarningBlock::GeneratePragmaWarningBlock(QQmlJSCodeGenerator *generator)
4618 : m_generator(generator)
4619{
4620 m_generator->m_body += u"QT_WARNING_PUSH\n"_s;
4621}
4622
4623QQmlJSCodeGenerator::GeneratePragmaWarningBlock::~GeneratePragmaWarningBlock()
4624{
4625 m_generator->m_body += u"QT_WARNING_POP\n"_s;
4626}
4627
4628void QQmlJSCodeGenerator::GeneratePragmaWarningBlock::silenceDivideByZero()
4629{
4630 // currently only needed with MSVC, if we need it on more compilers, we
4631 // should add a proper macro in Qt itself
4632 m_generator->m_body += u"QT_WARNING_DISABLE_MSVC(4723) // potential divide by 0\n"_s;
4633}
4634
4635void QQmlJSCodeGenerator::recordPropertyLookup(const QQmlJSScope::ConstPtr &base,
4636 const QQmlJSMetaProperty &property)
4637{
4638 if (!m_noAotValidation) {
4639 if (!m_lookupSignaturesRecorder.recordPropertyLookup(base, property))
4640 REJECT(m_lookupSignaturesRecorder.rejectMessage());
4641 }
4642}
4643
4644void QQmlJSCodeGenerator::recordMethodLookup(const QQmlJSScope::ConstPtr &base,
4645 const QQmlJSMetaMethod &method)
4646{
4647 if (!m_noAotValidation) {
4648 if (!m_lookupSignaturesRecorder.recordMethodLookup(base, method))
4649 REJECT(m_lookupSignaturesRecorder.rejectMessage());
4650 }
4651}
4652
4653void QQmlJSCodeGenerator::recordEnumKeyLookup(const QQmlJSScope::ConstPtr &base,
4654 const QQmlJSMetaEnum &metaEnum,
4655 const QString &keyName)
4656{
4657 if (!m_noAotValidation) {
4658 if (!m_lookupSignaturesRecorder.recordEnumKeyLookup(base, metaEnum, keyName))
4659 REJECT(m_lookupSignaturesRecorder.rejectMessage());
4660 }
4661}
\inmodule QtQmlCompiler
AccessSemantics
Definition qqmlsa.h:50
Combined button and popup list for selecting options.
static QString toNumericString(double value)
static QString messageTypeForMethod(const QString &method)
static QString derefContentPointer(const QString &contentPointer)
static bool canTypeBeAffectedBySideEffects(const QQmlJSTypeResolver *typeResolver, const QQmlJSRegisterContent &baseType)
#define BYTECODE_UNIMPLEMENTED()
#define INJECT_TRACE_INFO(function)
#define REJECT
static QString registerName(int registerIndex, int offset)
static QString minExpression(int argc)
static QString maxExpression(int argc)
static bool isTypeStorable(const QQmlJSTypeResolver *resolver, const QQmlJSScope::ConstPtr &type)