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 const auto t = QMetaType::fromName(\""_s
75 + QString::fromUtf8(QMetaObject::normalizedType(type->augmentedInternalName().toUtf8()))
76 + u"\"); 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 const QQmlJSMetaMethod::RelativeFunctionIndex relativeMethodIndex =
1966 method.isJavaScriptFunction() ? method.jsFunctionIndex() : method.methodIndex();
1967 initMethod = initMethodTemplate.arg(int(relativeMethodIndex));
1968 }
1969
1970 return u"const auto doCall = [&]() {\n"_s
1971 + argumentPreparation
1972 + u" void *args[] = {" + args + u"};\n"_s
1973 + u" return aotContext->"_s + callMethodTemplate.arg(u"args"_s).arg(argc) + u";\n"
1974 + u"};\n"_s
1975 + u"const auto doInit = [&]() {\n"_s
1976 + u" aotContext->"_s + initMethod + u";\n"
1977 + u"};\n"_s;
1978}
1979
1980void QQmlJSCodeGenerator::generateMoveOutVarAfterCall(const QString &outVar)
1981{
1982 if (m_state.accumulatorVariableOut.isEmpty() || outVar.isEmpty())
1983 return;
1984
1985 m_body += m_state.accumulatorVariableOut + u" = "_s + u"std::move(" + outVar + u");\n";
1986 m_body += u"aotContext->setImplicitDestructible("_s
1987 + m_state.accumulatorVariableOut + u");\n"_s;
1988}
1989
1990void QQmlJSCodeGenerator::generate_CallValue(int name, int argc, int argv)
1991{
1992 Q_UNUSED(name)
1993 Q_UNUSED(argc)
1994 Q_UNUSED(argv)
1996}
1997
1998void QQmlJSCodeGenerator::generate_CallWithReceiver(int name, int thisObject, int argc, int argv)
1999{
2000 Q_UNUSED(name)
2001 Q_UNUSED(thisObject)
2002 Q_UNUSED(argc)
2003 Q_UNUSED(argv)
2005}
2006
2007void QQmlJSCodeGenerator::generate_CallProperty(int nameIndex, int baseReg, int argc, int argv)
2008{
2009 Q_UNUSED(nameIndex);
2010 Q_UNUSED(baseReg);
2011 Q_UNUSED(argc);
2012 Q_UNUSED(argv);
2013 REJECT(u"CallProperty"_s);
2014}
2015
2016bool QQmlJSCodeGenerator::inlineStringMethod(const QString &name, int base, int argc, int argv)
2017{
2018 if (name != u"arg"_s || argc != 1)
2019 return false;
2020
2021 const auto arg = [&](const QQmlJSScope::ConstPtr &type) {
2022 return convertStored(registerType(argv).storedType(), type, consumedRegisterVariable(argv));
2023 };
2024
2025 const auto ret = [&](const QString &arg) {
2026 const QString expression = convertStored(
2027 registerType(base).storedType(), m_typeResolver->stringType(),
2028 consumedRegisterVariable(base)) + u".arg("_s + arg + u')';
2029 return conversion(
2030 m_typeResolver->stringType(), m_state.accumulatorOut(), expression);
2031 };
2032
2033 const QQmlJSRegisterContent input = m_state.readRegister(argv);
2034 m_body += m_state.accumulatorVariableOut + u" = "_s;
2035
2036 if (m_typeResolver->isNumeric(input))
2037 m_body += ret(arg(input.containedType()));
2038 else if (input.contains(m_typeResolver->boolType()))
2039 m_body += ret(arg(m_typeResolver->boolType()));
2040 else
2041 m_body += ret(arg(m_typeResolver->stringType()));
2042 m_body += u";\n"_s;
2043 return true;
2044}
2045
2046bool QQmlJSCodeGenerator::inlineTranslateMethod(const QString &name, int argc, int argv)
2047{
2048 addInclude(u"QtCore/qcoreapplication.h"_s);
2049
2050 const auto arg = [&](int i, const QQmlJSScope::ConstPtr &type) {
2051 Q_ASSERT(i < argc);
2052 return convertStored(registerType(argv + i).storedType(), type,
2053 consumedRegisterVariable(argv + i));
2054 };
2055
2056 const auto stringArg = [&](int i) {
2057 return i < argc
2058 ? (arg(i, m_typeResolver->stringType()) + u".toUtf8().constData()"_s)
2059 : u"\"\""_s;
2060 };
2061
2062 const auto intArg = [&](int i) {
2063 return i < argc ? arg(i, m_typeResolver->int32Type()) : u"-1"_s;
2064 };
2065
2066 const auto stringRet = [&](const QString &expression) {
2067 return conversion(
2068 m_typeResolver->stringType(), m_state.accumulatorOut(), expression);
2069 };
2070
2071 const auto capture = [&]() {
2072 m_body += u"aotContext->captureTranslation();\n"_s;
2073 };
2074
2075 if (name == u"QT_TRID_NOOP"_s || name == u"QT_TR_NOOP"_s) {
2076 Q_ASSERT(argc > 0);
2077 m_body += m_state.accumulatorVariableOut + u" = "_s
2078 + stringRet(arg(0, m_typeResolver->stringType())) + u";\n"_s;
2079 return true;
2080 }
2081
2082 if (name == u"QT_TRANSLATE_NOOP"_s) {
2083 Q_ASSERT(argc > 1);
2084 m_body += m_state.accumulatorVariableOut + u" = "_s
2085 + stringRet(arg(1, m_typeResolver->stringType())) + u";\n"_s;
2086 return true;
2087 }
2088
2089 if (name == u"qsTrId"_s) {
2090 capture();
2091 // We inline qtTrId() here because in the !QT_CONFIG(translation) case it's unavailable.
2092 // QCoreApplication::translate() is always available in some primitive form.
2093 // Also, this saves a function call.
2094 m_body += m_state.accumulatorVariableOut + u" = "_s
2095 + stringRet(u"QCoreApplication::translate(nullptr, "_s + stringArg(0) +
2096 u", nullptr, "_s + intArg(1) + u")"_s) + u";\n"_s;
2097 return true;
2098 }
2099
2100 if (name == u"qsTr"_s) {
2101 capture();
2102 m_body += m_state.accumulatorVariableOut + u" = "_s
2103 + stringRet(u"QCoreApplication::translate("_s
2104 + u"aotContext->translationContext().toUtf8().constData(), "_s
2105 + stringArg(0) + u", "_s + stringArg(1) + u", "_s
2106 + intArg(2) + u")"_s) + u";\n"_s;
2107 return true;
2108 }
2109
2110 if (name == u"qsTranslate"_s) {
2111 capture();
2112 m_body += m_state.accumulatorVariableOut + u" = "_s
2113 + stringRet(u"QCoreApplication::translate("_s
2114 + stringArg(0) + u", "_s + stringArg(1) + u", "_s
2115 + stringArg(2) + u", "_s + intArg(3) + u")"_s) + u";\n"_s;
2116 return true;
2117 }
2118
2119 return false;
2120}
2121
2122static QString maxExpression(int argc)
2123{
2124 Q_ASSERT_X(argc >= 2, Q_FUNC_INFO, "max() expects at least two arguments.");
2125
2126 QString expression =
2127 u"[&]() { \nauto tmpMax = (qIsNull(arg2) && qIsNull(arg1) && std::copysign(1.0, arg2) == 1) ? arg2 : ((arg2 > arg1 || std::isnan(arg2)) ? arg2 : arg1);\n"_s;
2128 for (int i = 2; i < argc; i++) {
2129 expression +=
2130 "\ttmpMax = (qIsNull(%1) && qIsNull(tmpMax) && std::copysign(1.0, %1) == 1) ? arg2 : ((%1 > tmpMax || std::isnan(%1)) ? %1 : tmpMax);\n"_L1
2131 .arg("arg"_L1 + QString::number(i + 1));
2132 }
2133 expression += "return tmpMax;\n}()"_L1;
2134
2135 return expression;
2136}
2137
2138static QString minExpression(int argc)
2139{
2140 Q_ASSERT_X(argc >= 2, Q_FUNC_INFO, "min() expects at least two arguments.");
2141
2142 QString expression =
2143 u"[&]() { \nauto tmpMin = (qIsNull(arg2) && qIsNull(arg1) && std::copysign(1.0, arg2) == -1) ? arg2 : ((arg2 < arg1 || std::isnan(arg2)) ? arg2 : arg1);\n"_s;
2144 for (int i = 2; i < argc; i++) {
2145 expression +=
2146 "tmpMin = (qIsNull(%1) && qIsNull(tmpMin) && std::copysign(1.0, %1) == -1) ? arg2 : ((%1 < tmpMin || std::isnan(%1)) ? %1 : tmpMin);\n"_L1
2147 .arg("arg"_L1 + QString::number(i + 1));
2148 }
2149 expression += "return tmpMin;\n}()"_L1;
2150
2151 return expression;
2152}
2153
2154bool QQmlJSCodeGenerator::inlineMathMethod(const QString &name, int argc, int argv)
2155{
2156 addInclude(u"cmath"_s);
2157 addInclude(u"limits"_s);
2158 addInclude(u"QtCore/qalgorithms.h"_s);
2159 addInclude(u"QtCore/qrandom.h"_s);
2160 addInclude(u"QtQml/qjsprimitivevalue.h"_s);
2161
2162 // If the result is not stored, we don't need to generate any code. All the math methods are
2163 // conceptually pure functions.
2164 if (m_state.changedRegisterIndex() != Accumulator)
2165 return true;
2166
2167 m_body += u"{\n"_s;
2168 for (int i = 0; i < argc; ++i) {
2169 m_body += u"const double arg%1 = "_s.arg(i + 1) + convertStored(
2170 registerType(argv + i).storedType(),
2171 m_typeResolver->realType(), consumedRegisterVariable(argv + i))
2172 + u";\n"_s;
2173 }
2174
2175 const QString qNaN = u"std::numeric_limits<double>::quiet_NaN()"_s;
2176 const QString inf = u"std::numeric_limits<double>::infinity()"_s;
2177 m_body += m_state.accumulatorVariableOut + u" = "_s;
2178
2179 QString expression;
2180
2181 if (name == u"abs" && argc == 1) {
2182 expression = u"(qIsNull(arg1) ? 0 : (arg1 < 0.0 ? -arg1 : arg1))"_s;
2183 } else if (name == u"acos"_s && argc == 1) {
2184 expression = u"arg1 > 1.0 ? %1 : std::acos(arg1)"_s.arg(qNaN);
2185 } else if (name == u"acosh"_s && argc == 1) {
2186 expression = u"arg1 < 1.0 ? %1 : std::acosh(arg1)"_s.arg(qNaN);
2187 } else if (name == u"asin"_s && argc == 1) {
2188 expression = u"arg1 > 1.0 ? %1 : std::asin(arg1)"_s.arg(qNaN);
2189 } else if (name == u"asinh"_s && argc == 1) {
2190 expression = u"qIsNull(arg1) ? arg1 : std::asinh(arg1)"_s;
2191 } else if (name == u"atan"_s && argc == 1) {
2192 expression = u"qIsNull(arg1) ? arg1 : std::atan(arg1)"_s;
2193 } else if (name == u"atanh"_s && argc == 1) {
2194 expression = u"qIsNull(arg1) ? arg1 : std::atanh(arg1)"_s;
2195 } else if (name == u"atan2"_s) {
2196 // TODO: complicated
2197 return false;
2198 } else if (name == u"cbrt"_s && argc == 1) {
2199 expression = u"std::cbrt(arg1)"_s;
2200 } else if (name == u"ceil"_s && argc == 1) {
2201 expression = u"(arg1 < 0.0 && arg1 > -1.0) ? std::copysign(0.0, -1.0) : std::ceil(arg1)"_s;
2202 } else if (name == u"clz32"_s && argc == 1) {
2203 expression = u"qint32(qCountLeadingZeroBits(quint32(QJSNumberCoercion::toInteger(arg1))))"_s;
2204 } else if (name == u"cos"_s && argc == 1) {
2205 expression = u"std::cos(arg1)"_s;
2206 } else if (name == u"cosh"_s && argc == 1) {
2207 expression = u"std::cosh(arg1)"_s;
2208 } else if (name == u"exp"_s && argc == 1) {
2209 expression = u"std::isinf(arg1) "
2210 "? (std::copysign(1.0, arg1) == -1 ? 0.0 : %1) "
2211 ": std::exp(arg1)"_s.arg(inf);
2212 } else if (name == u"expm1"_s) {
2213 // TODO: complicated
2214 return false;
2215 } else if (name == u"floor"_s && argc == 1) {
2216 expression = u"std::floor(arg1)"_s;
2217 } else if (name == u"fround"_s && argc == 1) {
2218 expression = u"(std::isnan(arg1) || std::isinf(arg1) || qIsNull(arg1)) "
2219 "? arg1 "
2220 ": double(float(arg1))"_s;
2221 } else if (name == u"hypot"_s) {
2222 // TODO: complicated
2223 return false;
2224 } else if (name == u"imul"_s && argc == 2) {
2225 expression = u"qint32(quint32(QJSNumberCoercion::toInteger(arg1)) "
2226 "* quint32(QJSNumberCoercion::toInteger(arg2)))"_s;
2227 } else if (name == u"log"_s && argc == 1) {
2228 expression = u"arg1 < 0.0 ? %1 : std::log(arg1)"_s.arg(qNaN);
2229 } else if (name == u"log10"_s && argc == 1) {
2230 expression = u"arg1 < 0.0 ? %1 : std::log10(arg1)"_s.arg(qNaN);
2231 } else if (name == u"log1p"_s && argc == 1) {
2232 expression = u"arg1 < -1.0 ? %1 : std::log1p(arg1)"_s.arg(qNaN);
2233 } else if (name == u"log2"_s && argc == 1) {
2234 expression = u"arg1 < -0.0 ? %1 : std::log2(arg1)"_s.arg(qNaN);
2235 } else if (name == u"max"_s && argc >= 2) {
2236 expression = maxExpression(argc);
2237 } else if (name == u"min"_s && argc >= 2) {
2238 expression = minExpression(argc);
2239 } else if (name == u"pow"_s) {
2240 expression = u"QQmlPrivate::jsExponentiate(arg1, arg2)"_s;
2241 } else if (name == u"random"_s && argc == 0) {
2242 expression = u"QRandomGenerator::global()->generateDouble()"_s;
2243 } else if (name == u"round"_s && argc == 1) {
2244 expression = u"std::isfinite(arg1) "
2245 "? ((arg1 < 0.5 && arg1 >= -0.5) "
2246 "? std::copysign(0.0, arg1) "
2247 ": std::floor(arg1 + 0.5)) "
2248 ": arg1"_s;
2249 } else if (name == u"sign"_s && argc == 1) {
2250 expression = u"std::isnan(arg1) "
2251 "? %1 "
2252 ": (qIsNull(arg1) "
2253 "? arg1 "
2254 ": (std::signbit(arg1) ? -1.0 : 1.0))"_s.arg(qNaN);
2255 } else if (name == u"sin"_s && argc == 1) {
2256 expression = u"qIsNull(arg1) ? arg1 : std::sin(arg1)"_s;
2257 } else if (name == u"sinh"_s && argc == 1) {
2258 expression = u"qIsNull(arg1) ? arg1 : std::sinh(arg1)"_s;
2259 } else if (name == u"sqrt"_s && argc == 1) {
2260 expression = u"std::sqrt(arg1)"_s;
2261 } else if (name == u"tan"_s && argc == 1) {
2262 expression = u"qIsNull(arg1) ? arg1 : std::tan(arg1)"_s;
2263 } else if (name == u"tanh"_s && argc == 1) {
2264 expression = u"qIsNull(arg1) ? arg1 : std::tanh(arg1)"_s;
2265 } else if (name == u"trunc"_s && argc == 1) {
2266 expression = u"std::trunc(arg1)"_s;
2267 } else {
2268 return false;
2269 }
2270
2271 m_body += conversion(m_typeResolver->realType(), m_state.accumulatorOut(), expression);
2272
2273 m_body += u";\n"_s;
2274 m_body += u"}\n"_s;
2275 return true;
2276}
2277
2278static QString messageTypeForMethod(const QString &method)
2279{
2280 if (method == u"log" || method == u"debug")
2281 return u"QtDebugMsg"_s;
2282 if (method == u"info")
2283 return u"QtInfoMsg"_s;
2284 if (method == u"warn")
2285 return u"QtWarningMsg"_s;
2286 if (method == u"error")
2287 return u"QtCriticalMsg"_s;
2288 return QString();
2289}
2290
2291bool QQmlJSCodeGenerator::inlineConsoleMethod(const QString &name, int argc, int argv)
2292{
2293 const QString type = messageTypeForMethod(name);
2294 if (type.isEmpty())
2295 return false;
2296
2297 addInclude(u"QtCore/qloggingcategory.h"_s);
2298
2299 m_body += u"{\n";
2300 m_body += u" bool firstArgIsCategory = false;\n";
2301 const QQmlJSRegisterContent firstArg = argc > 0 ? registerType(argv) : QQmlJSRegisterContent();
2302
2303 // We could check whether the first argument is a QQmlLoggingCategoryBase here, and we should
2304 // because QQmlLoggingCategoryBase is now a builtin.
2305 // TODO: The run time check for firstArg is obsolete.
2306 const bool firstArgIsReference = argc > 0
2307 && firstArg.containedType()->isReferenceType();
2308
2309 if (firstArgIsReference) {
2310 m_body += u" QObject *firstArg = ";
2311 m_body += convertStored(
2312 firstArg.storedType(),
2313 m_typeResolver->genericType(firstArg.storedType()),
2314 registerVariable(argv));
2315 m_body += u";\n";
2316 }
2317
2318 m_body += u" const QLoggingCategory *category = aotContext->resolveLoggingCategory(";
2319 m_body += firstArgIsReference ? u"firstArg"_sv : u"nullptr"_sv;
2320 m_body += u", &firstArgIsCategory);\n";
2321 m_body += u" if (category && category->isEnabled(" + type + u")) {\n";
2322
2323 m_body += u" const QString message = ";
2324
2325 const auto stringConversion = [&](int i) -> QString {
2326 const QQmlJSScope::ConstPtr read = m_state.readRegister(argv + i).storedType();
2327 const QQmlJSScope::ConstPtr actual = registerType(argv + i).storedType();
2328 if (read == m_typeResolver->stringType()) {
2329 return convertStored(actual, read, consumedRegisterVariable(argv + i));
2330 } else if (actual->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
2331 addInclude(u"QtQml/qjslist.h"_s);
2332 return u"(u'[' + QJSList(&"_s + registerVariable(argv + i)
2333 + u", aotContext->engine).toString() + u']')"_s;
2334 } else {
2335 REJECT<QString>(u"converting arguments for console method to string"_s);
2336 }
2337 };
2338
2339 if (argc > 0) {
2340 if (firstArgIsReference) {
2341 const QString firstArgStringConversion = convertStored(
2342 registerType(argv).storedType(),
2343 m_typeResolver->stringType(), registerVariable(argv));
2344 m_body += u"(firstArgIsCategory ? QString() : (" + firstArgStringConversion;
2345 if (argc > 1)
2346 m_body += u".append(QLatin1Char(' ')))).append(";
2347 else
2348 m_body += u"))";
2349 } else {
2350 m_body += stringConversion(0);
2351 if (argc > 1)
2352 m_body += u".append(QLatin1Char(' ')).append(";
2353 }
2354
2355 for (int i = 1; i < argc; ++i) {
2356 if (i > 1)
2357 m_body += u".append(QLatin1Char(' ')).append("_s;
2358 m_body += stringConversion(i) + u')';
2359 }
2360 } else {
2361 m_body += u"QString()";
2362 }
2363 m_body += u";\n ";
2364 generateSetInstructionPointer();
2365 m_body += u" aotContext->writeToConsole(" + type + u", message, category);\n";
2366 m_body += u" }\n";
2367 m_body += u"}\n";
2368 return true;
2369}
2370
2371bool QQmlJSCodeGenerator::inlineArrayMethod(const QString &name, int base, int argc, int argv)
2372{
2373 const auto intType = m_typeResolver->int32Type();
2374 const auto elementType = registerType(base).storedType()->elementType();
2375 const auto boolType = m_typeResolver->boolType();
2376 const auto stringType = m_typeResolver->stringType();
2377 const auto baseType = registerType(base);
2378
2379 const QString baseVar = registerVariable(base);
2380 const QString qjsListMethod = u"QJSList(&"_s + baseVar + u", aotContext->engine)."
2381 + name + u"(";
2382
2383 addInclude(u"QtQml/qjslist.h"_s);
2384
2385 if (name == u"includes" && argc > 0 && argc < 3) {
2386 QString call = qjsListMethod
2387 + convertStored(registerType(argv).storedType(), elementType,
2388 consumedRegisterVariable(argv));
2389 if (argc == 2) {
2390 call += u", " + convertStored(registerType(argv + 1).storedType(), intType,
2391 consumedRegisterVariable(argv + 1));
2392 }
2393 call += u")";
2394
2395 m_body += m_state.accumulatorVariableOut + u" = "_s
2396 + conversion(boolType, m_state.accumulatorOut(), call) + u";\n"_s;
2397 return true;
2398 }
2399
2400 if (name == u"toString" || (name == u"join" && argc < 2)) {
2401 QString call = qjsListMethod;
2402 if (argc == 1) {
2403 call += convertStored(registerType(argv).storedType(), stringType,
2404 consumedRegisterVariable(argv));
2405 }
2406 call += u")";
2407
2408 m_body += m_state.accumulatorVariableOut + u" = "_s
2409 + conversion(stringType, m_state.accumulatorOut(), call) + u";\n"_s;
2410 return true;
2411 }
2412
2413 if (name == u"slice" && argc < 3) {
2414 QString call = qjsListMethod;
2415 for (int i = 0; i < argc; ++i) {
2416 if (i > 0)
2417 call += u", ";
2418 call += convertStored(registerType(argv + i).storedType(), intType,
2419 consumedRegisterVariable(argv + i));
2420 }
2421 call += u")";
2422
2423 m_body += m_state.accumulatorVariableOut + u" = "_s;
2424 if (baseType.storedType()->isListProperty())
2425 m_body += conversion(m_typeResolver->qObjectListType(), m_state.accumulatorOut(), call);
2426 else
2427 m_body += conversion(baseType, m_state.accumulatorOut(), call);
2428 m_body += u";\n"_s;
2429
2430 return true;
2431 }
2432
2433 if ((name == u"indexOf" || name == u"lastIndexOf") && argc > 0 && argc < 3) {
2434 QString call = qjsListMethod
2435 + convertStored(registerType(argv).storedType(), elementType,
2436 consumedRegisterVariable(argv));
2437 if (argc == 2) {
2438 call += u", " + convertStored(registerType(argv + 1).storedType(), intType,
2439 consumedRegisterVariable(argv + 1));
2440 }
2441 call += u")";
2442
2443 m_body += m_state.accumulatorVariableOut + u" = "_s
2444 + conversion(intType, m_state.accumulatorOut(), call) + u";\n"_s;
2445 return true;
2446 }
2447
2448 return false;
2449}
2450
2451void QQmlJSCodeGenerator::generate_CallPropertyLookup(int index, int base, int argc, int argv)
2452{
2453 INJECT_TRACE_INFO(generate_CallPropertyLookup);
2454
2455 const QQmlJSRegisterContent scopeContent = m_state.accumulatorOut().scope();
2456 const QQmlJSScope::ConstPtr scope = scopeContent.containedType();
2457
2458 AccumulatorConverter registers(this);
2459
2460 const QQmlJSRegisterContent baseType = registerType(base);
2461 const QString name = m_jsUnitGenerator->lookupName(index);
2462
2463 if (scope == m_typeResolver->mathObject()) {
2464 if (inlineMathMethod(name, argc, argv))
2465 return;
2466 } else if (scope == m_typeResolver->consoleObject()) {
2467 if (inlineConsoleMethod(name, argc, argv))
2468 return;
2469 } else if (scope == m_typeResolver->stringType()) {
2470 if (inlineStringMethod(name, base, argc, argv))
2471 return;
2472 } else if (baseType.storedType()->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
2473 if (inlineArrayMethod(name, base, argc, argv))
2474 return;
2475 }
2476
2477 if (m_state.accumulatorOut().isJavaScriptReturnValue())
2478 REJECT(u"call to untyped JavaScript function"_s);
2479
2480 m_body += u"{\n"_s;
2481 QString outVar;
2482
2483 if (scope->isReferenceType()) {
2484 const QString inputPointer = resolveQObjectPointer(
2485 scope, baseType, registerVariable(base),
2486 u"Cannot call method '%1' of %2"_s.arg(name));
2487
2488 const QString initMethodTemplate = m_state.isShadowable()
2489 ? u"initCallObjectPropertyLookupAsVariant(%1, %2)"_s
2490 : u"initCallObjectPropertyLookup(%1, %2, %3)"_s;
2491
2492 m_body += initAndCall(
2493 argc, argv,
2494 u"callObjectPropertyLookup(%1, %2, %3, %4)"_s.arg(index).arg(inputPointer),
2495 initMethodTemplate.arg(index).arg(inputPointer), &outVar);
2496 } else {
2497 const QQmlJSScope::ConstPtr originalScope
2498 = m_typeResolver->original(scopeContent).containedType();
2499 const QString inputPointer = resolveValueTypeContentPointer(
2500 originalScope, baseType, registerVariable(base),
2501 u"Cannot call method '%1' of %2"_s.arg(name));
2502
2503 m_body += initAndCall(
2504 argc, argv,
2505 u"callValueLookup(%1, %2, %3, %4)"_s.arg(index).arg(inputPointer),
2506 u"initCallValueLookup(%1, %2, %3)"_s
2507 .arg(index).arg(metaObject(originalScope)),
2508 &outVar);
2509 }
2510
2511 const QString lookup = u"doCall()"_s;
2512 const QString initialization = u"doInit()"_s;
2513 const QString preparation = getLookupPreparation(m_state.accumulatorOut(), outVar, index);
2514 recordMethodLookup(m_typeResolver->originalContainedType(m_state.accumulatorOut().scope()),
2515 originalMethod(m_state.accumulatorOut()));
2516 generateLookup(lookup, initialization, preparation);
2517 generateMoveOutVarAfterCall(outVar);
2518
2519 m_body += u"}\n"_s;
2520
2521 if (scope->isReferenceType())
2522 return;
2523
2524 const QQmlJSMetaMethod method = m_state.accumulatorOut().methodCall();
2525 if (!method.isConst())
2526 generateWriteBack(base);
2527}
2528
2529void QQmlJSCodeGenerator::generate_CallName(int name, int argc, int argv)
2530{
2531 Q_UNUSED(name);
2532 Q_UNUSED(argc);
2533 Q_UNUSED(argv);
2534 REJECT(u"CallName"_s);
2535}
2536
2537void QQmlJSCodeGenerator::generate_CallPossiblyDirectEval(int argc, int argv)
2538{
2539 Q_UNUSED(argc)
2540 Q_UNUSED(argv)
2542}
2543
2544void QQmlJSCodeGenerator::generate_CallGlobalLookup(int index, int argc, int argv)
2545{
2546 Q_UNUSED(index);
2547 Q_UNUSED(argc);
2548 Q_UNUSED(argv);
2549 REJECT(u"CallGlobalLookup"_s);
2550}
2551
2552void QQmlJSCodeGenerator::generate_CallQmlContextPropertyLookup(int index, int argc, int argv)
2553{
2554 INJECT_TRACE_INFO(generate_CallQmlContextPropertyLookup);
2555
2556 if (m_state.accumulatorOut().scope().contains(m_typeResolver->jsGlobalObject())) {
2557 const QString name = m_jsUnitGenerator->stringForIndex(
2558 m_jsUnitGenerator->lookupNameIndex(index));
2559 if (inlineTranslateMethod(name, argc, argv))
2560 return;
2561 }
2562
2563 if (m_state.accumulatorOut().isJavaScriptReturnValue())
2564 REJECT(u"call to untyped JavaScript function"_s);
2565
2566 AccumulatorConverter registers(this);
2567
2568 m_body += u"{\n"_s;
2569 QString outVar;
2570 m_body += initAndCall(
2571 argc, argv, u"callQmlContextPropertyLookup(%1, %2, %3)"_s.arg(index),
2572 u"initCallQmlContextPropertyLookup(%1, %2)"_s.arg(index), &outVar);
2573
2574 const QString lookup = u"doCall()"_s;
2575 const QString initialization = u"doInit()"_s;
2576 const QString preparation = getLookupPreparation(m_state.accumulatorOut(), outVar, index);
2577 recordMethodLookup(m_typeResolver->originalContainedType(m_state.accumulatorOut().scope()),
2578 originalMethod(m_state.accumulatorOut()));
2579 generateLookup(lookup, initialization, preparation);
2580 generateMoveOutVarAfterCall(outVar);
2581
2582 m_body += u"}\n"_s;
2583}
2584
2585void QQmlJSCodeGenerator::generate_CallWithSpread(int func, int thisObject, int argc, int argv)
2586{
2587 Q_UNUSED(func)
2588 Q_UNUSED(thisObject)
2589 Q_UNUSED(argc)
2590 Q_UNUSED(argv)
2592}
2593
2594void QQmlJSCodeGenerator::generate_TailCall(int func, int thisObject, int argc, int argv)
2595{
2596 Q_UNUSED(func)
2597 Q_UNUSED(thisObject)
2598 Q_UNUSED(argc)
2599 Q_UNUSED(argv)
2601}
2602
2603void QQmlJSCodeGenerator::generate_Construct(int func, int argc, int argv)
2604{
2605 INJECT_TRACE_INFO(generate_Construct);
2606 Q_UNUSED(func);
2607
2608 const auto originalResult = originalType(m_state.accumulatorOut());
2609
2610 if (originalResult.contains(m_typeResolver->dateTimeType())) {
2611 m_body += m_state.accumulatorVariableOut + u" = ";
2612 if (argc == 0) {
2613 m_body += conversion(
2614 m_typeResolver->dateTimeType(), m_state.accumulatorOut(),
2615 u"QDateTime::currentDateTime()"_s) + u";\n";
2616 return;
2617 }
2618
2619 if (argc == 1 && m_state.readRegister(argv).contains(m_typeResolver->dateTimeType())) {
2620 m_body += conversion(
2621 registerType(argv), m_state.readRegister(argv), registerVariable(argv))
2622 + u";\n";
2623 return;
2624 }
2625
2626 QString ctorArgs;
2627 constexpr int maxArgc = 7; // year, month, day, hours, minutes, seconds, milliseconds
2628 for (int i = 0; i < std::min(argc, maxArgc); ++i) {
2629 if (i > 0)
2630 ctorArgs += u", ";
2631 ctorArgs += conversion(
2632 registerType(argv + i), m_state.readRegister(argv + i),
2633 registerVariable(argv + i));
2634 }
2635 m_body += conversion(
2636 m_typeResolver->dateTimeType(), m_state.accumulatorOut(),
2637 u"aotContext->constructDateTime("_s + ctorArgs + u')') + u";\n";
2638 return;
2639 }
2640
2641 if (originalResult.contains(m_typeResolver->variantListType())) {
2642 rejectIfBadArray();
2643
2644 if (argc == 1 && m_state.readRegister(argv).contains(m_typeResolver->realType())) {
2645 addInclude(u"QtQml/qjslist.h"_s);
2646
2647 const QString error = u" aotContext->engine->throwError(QJSValue::RangeError, "_s
2648 + u"QLatin1String(\"Invalid array length\"));\n"_s;
2649
2650 const QString indexName = registerVariable(argv);
2651 const auto indexType = registerType(argv).containedType();
2652 if (!m_typeResolver->isNativeArrayIndex(indexType)) {
2653 m_body += u"if (!QJSNumberCoercion::isArrayIndex("_s + indexName + u")) {\n"_s
2654 + error;
2655 generateReturnError();
2656 m_body += u"}\n"_s;
2657 } else if (!m_typeResolver->isUnsignedInteger(indexType)) {
2658 m_body += u"if ("_s + indexName + u" < 0) {\n"_s
2659 + error;
2660 generateReturnError();
2661 m_body += u"}\n"_s;
2662 }
2663
2664 m_body += m_state.accumulatorVariableOut + u" = "_s
2665 + m_state.accumulatorOut().storedType()->internalName() + u"();\n"_s;
2666 m_body += u"QJSList(&"_s + m_state.accumulatorVariableOut
2667 + u", aotContext->engine).resize("_s
2668 + convertStored(
2669 registerType(argv).storedType(), m_typeResolver->sizeType(),
2670 consumedRegisterVariable(argv))
2671 + u");\n"_s;
2672 } else if (!m_logger->currentFunctionHasCompileError()) {
2673 generateArrayInitializer(argc, argv);
2674 }
2675 return;
2676 }
2677
2678 const QQmlJSScope::ConstPtr originalContained = originalResult.containedType();
2679 if (originalContained->isValueType() && originalResult.isMethodCall()) {
2680 const QQmlJSMetaMethod ctor = originalResult.methodCall();
2681 if (ctor.isJavaScriptFunction())
2682 REJECT(u"calling JavaScript constructor "_s + ctor.methodName());
2683
2684 QList<QQmlJSRegisterContent> argumentTypes;
2685 QStringList arguments;
2686 for (int i = 0; i < argc; ++i) {
2687 argumentTypes.append(registerType(argv + i));
2688 arguments.append(consumedRegisterVariable(argv + i));
2689 }
2690
2691 const QQmlJSScope::ConstPtr extension = originalContained->extensionType().scope;
2692 const QString result = generateCallConstructor(
2693 ctor, argumentTypes, arguments, metaType(originalContained),
2694 metaObject(extension ? extension : originalContained));
2695
2696 m_body += m_state.accumulatorVariableOut + u" = "_s
2697 + conversion(m_pool->storedIn(originalResult, m_typeResolver->varType()),
2698 m_state.accumulatorOut(), result)
2699 + u";\n"_s;
2700
2701 return;
2702 }
2703
2704
2705 REJECT(u"Construct"_s);
2706}
2707
2708void QQmlJSCodeGenerator::generate_ConstructWithSpread(int func, int argc, int argv)
2709{
2710 Q_UNUSED(func)
2711 Q_UNUSED(argc)
2712 Q_UNUSED(argv)
2714}
2715
2716void QQmlJSCodeGenerator::generate_SetUnwindHandler(int offset)
2717{
2718 Q_UNUSED(offset)
2719 REJECT(u"SetUnwindHandler"_s);
2720}
2721
2722void QQmlJSCodeGenerator::generate_UnwindDispatch()
2723{
2724 REJECT(u"UnwindDispatch"_s);
2725}
2726
2727void QQmlJSCodeGenerator::generate_UnwindToLabel(int level, int offset)
2728{
2729 Q_UNUSED(level)
2730 Q_UNUSED(offset)
2732}
2733
2734void QQmlJSCodeGenerator::generate_DeadTemporalZoneCheck(int name)
2735{
2736 Q_UNUSED(name)
2737 INJECT_TRACE_INFO(generate_DeadTemporalZoneCheck);
2738 // Nothing to do here. If we have statically asserted the dtz check in the type propagator
2739 // the value cannot be empty. Otherwise we can't get here.
2740}
2741
2742void QQmlJSCodeGenerator::generate_ThrowException()
2743{
2744 INJECT_TRACE_INFO(generate_ThrowException);
2745
2746 generateSetInstructionPointer();
2747 m_body += u"aotContext->engine->throwError("_s + conversion(
2748 m_state.accumulatorIn(),
2749 m_typeResolver->jsValueType(),
2750 m_state.accumulatorVariableIn) + u");\n"_s;
2751 generateReturnError();
2752 m_skipUntilNextLabel = true;
2753 resetState();
2754}
2755
2756void QQmlJSCodeGenerator::generate_GetException()
2757{
2759}
2760
2761void QQmlJSCodeGenerator::generate_SetException()
2762{
2764}
2765
2766void QQmlJSCodeGenerator::generate_CreateCallContext()
2767{
2768 INJECT_TRACE_INFO(generate_CreateCallContext);
2769
2770 m_body += u"{\n"_s;
2771}
2772
2773void QQmlJSCodeGenerator::generate_PushCatchContext(int index, int nameIndex)
2774{
2775 Q_UNUSED(index)
2776 Q_UNUSED(nameIndex)
2777 REJECT(u"PushCatchContext"_s);
2778}
2779
2780void QQmlJSCodeGenerator::generate_PushWithContext()
2781{
2783}
2784
2785void QQmlJSCodeGenerator::generate_PushBlockContext(int index)
2786{
2787 Q_UNUSED(index)
2789}
2790
2791void QQmlJSCodeGenerator::generate_CloneBlockContext()
2792{
2794}
2795
2796void QQmlJSCodeGenerator::generate_PushScriptContext(int index)
2797{
2798 Q_UNUSED(index)
2800}
2801
2802void QQmlJSCodeGenerator::generate_PopScriptContext()
2803{
2805}
2806
2807void QQmlJSCodeGenerator::generate_PopContext()
2808{
2809 INJECT_TRACE_INFO(generate_PopContext);
2810
2811 // Add an empty block before the closing brace, in case there was a bare label before it.
2812 m_body += u"{}\n}\n"_s;
2813}
2814
2815void QQmlJSCodeGenerator::generate_GetIterator(int iterator)
2816{
2817 INJECT_TRACE_INFO(generate_GetIterator);
2818
2819 addInclude(u"QtQml/qjslist.h"_s);
2820 const QQmlJSRegisterContent listType = m_state.accumulatorIn();
2821 if (!listType.isList())
2822 REJECT(u"iterator on non-list type"_s);
2823
2824 const QQmlJSRegisterContent iteratorType = m_state.accumulatorOut();
2825 if (!iteratorType.isProperty())
2826 REJECT(u"using non-iterator as iterator"_s);
2827
2828 const QString identifier = QString::number(iteratorType.baseLookupIndex());
2829 QString baseName = m_state.accumulatorVariableOut.mid(2); // remove "s."
2830 const QString iteratorName = baseName + u"Iterator" + identifier;
2831 const QString listName = baseName + u"List" + identifier;
2832
2833 m_body += u"QJSListFor"_s
2834 + (iterator == int(QQmlJS::AST::ForEachType::In) ? u"In"_s : u"Of"_s)
2835 + u"Iterator "_s + iteratorName + u";\n";
2836 m_body += m_state.accumulatorVariableOut + u" = &" + iteratorName + u";\n";
2837
2838 m_body += m_state.accumulatorVariableOut + u"->init(";
2839 if (iterator == int(QQmlJS::AST::ForEachType::In)) {
2840 if (!iteratorType.isStoredIn(m_typeResolver->forInIteratorPtr()))
2841 REJECT(u"using non-iterator as iterator"_s);
2842 m_body += u"QJSList(&" + m_state.accumulatorVariableIn + u", aotContext->engine)";
2843 }
2844 m_body += u");\n";
2845
2846 if (iterator == int(QQmlJS::AST::ForEachType::Of)) {
2847 if (!iteratorType.isStoredIn(m_typeResolver->forOfIteratorPtr()))
2848 REJECT(u"using non-iterator as iterator"_s);
2849 m_body += u"const auto &" // Rely on life time extension for const refs
2850 + listName + u" = " + consumedAccumulatorVariableIn();
2851 }
2852}
2853
2854void QQmlJSCodeGenerator::generate_IteratorNext(int value, int offset)
2855{
2856 INJECT_TRACE_INFO(generate_IteratorNext);
2857
2858 Q_ASSERT(value == m_state.changedRegisterIndex());
2859 const QQmlJSRegisterContent iteratorContent = m_state.accumulatorIn();
2860 if (!iteratorContent.isProperty())
2861 REJECT(u"using non-iterator as iterator"_s);
2862
2863 const QQmlJSScope::ConstPtr iteratorType = iteratorContent.storedType();
2864 const QString listName = m_state.accumulatorVariableIn
2865 + u"List" + QString::number(iteratorContent.baseLookupIndex());
2866 QString qjsList;
2867 if (iteratorType == m_typeResolver->forOfIteratorPtr())
2868 qjsList = u"QJSList(&" + listName + u", aotContext->engine)";
2869 else if (iteratorType != m_typeResolver->forInIteratorPtr())
2870 REJECT(u"using non-iterator as iterator"_s);
2871
2872 m_body += u"if (" + m_state.accumulatorVariableIn + u"->hasNext(" + qjsList + u")) {\n ";
2873
2874 // We know that this works because we can do ->next() below.
2875 QQmlJSRegisterContent iteratorValue = m_typeResolver->extractNonVoidFromOptionalType(
2876 m_typeResolver->original(m_state.changedRegister()));
2877 iteratorValue = m_pool->storedIn(iteratorValue, iteratorValue.containedType());
2878
2879 m_body += changedRegisterVariable() + u" = "
2880 + conversion(
2881 iteratorValue, m_state.changedRegister(),
2882 m_state.accumulatorVariableIn + u"->next(" + qjsList + u')')
2883 + u";\n";
2884 m_body += u"} else {\n ";
2885 m_body += changedRegisterVariable() + u" = "
2886 + conversion(m_typeResolver->voidType(), m_state.changedRegister(), QString());
2887 m_body += u";\n ";
2888 generateJumpCodeWithTypeConversions(offset);
2889 m_body += u"\n}"_s;
2890}
2891
2892void QQmlJSCodeGenerator::generate_IteratorNextForYieldStar(int iterator, int object, int offset)
2893{
2894 Q_UNUSED(iterator)
2895 Q_UNUSED(object)
2896 Q_UNUSED(offset)
2898}
2899
2900void QQmlJSCodeGenerator::generate_IteratorClose()
2901{
2903}
2904
2905void QQmlJSCodeGenerator::generate_DestructureRestElement()
2906{
2908}
2909
2910void QQmlJSCodeGenerator::generate_DeleteProperty(int base, int index)
2911{
2912 Q_UNUSED(base)
2913 Q_UNUSED(index)
2915}
2916
2917void QQmlJSCodeGenerator::generate_DeleteName(int name)
2918{
2919 Q_UNUSED(name)
2921}
2922
2923void QQmlJSCodeGenerator::generate_TypeofName(int name)
2924{
2925 Q_UNUSED(name);
2926 REJECT(u"TypeofName"_s);
2927}
2928
2929void QQmlJSCodeGenerator::generate_TypeofValue()
2930{
2931 REJECT(u"TypeofValue"_s);
2932}
2933
2934void QQmlJSCodeGenerator::generate_DeclareVar(int varName, int isDeletable)
2935{
2936 Q_UNUSED(varName)
2937 Q_UNUSED(isDeletable)
2939}
2940
2941void QQmlJSCodeGenerator::generate_DefineArray(int argc, int args)
2942{
2943 INJECT_TRACE_INFO(generate_DefineArray);
2944
2945 rejectIfBadArray();
2946 if (!m_logger->currentFunctionHasCompileError())
2947 generateArrayInitializer(argc, args);
2948}
2949
2950void QQmlJSCodeGenerator::generate_DefineObjectLiteral(int internalClassId, int argc, int args)
2951{
2952 INJECT_TRACE_INFO(generate_DefineObjectLiteral);
2953
2954 const QQmlJSScope::ConstPtr stored = m_state.accumulatorOut().storedType();
2955 if (stored->accessSemantics() != QQmlJSScope::AccessSemantics::Value)
2956 REJECT(u"storing an object literal in a non-value type"_s);
2957
2958 const QQmlJSScope::ConstPtr contained = m_state.accumulatorOut().containedType();
2959
2960 const int classSize = m_jsUnitGenerator->jsClassSize(internalClassId);
2961
2962 // This is not implemented because we cannot statically determine the type of the value and we
2963 // don't want to rely on QVariant::convert() since that may give different results than
2964 // the JavaScript coercion. We might still make it work by querying the QMetaProperty
2965 // for its type at run time and runtime coercing to that, but we don't know whether that
2966 // still pays off.
2967 if (argc > classSize)
2968 REJECT(u"non-literal keys of object literals"_s);
2969
2970 Q_ASSERT(argc == classSize);
2971
2972 const auto createVariantMap = [&]() {
2973 QString result;
2974 result += u"QVariantMap {\n";
2975 const QQmlJSScope::ConstPtr propType = m_typeResolver->varType();
2976 for (int i = 0; i < classSize; ++i) {
2977 result += u"{ "_s
2978 + QQmlJSUtils::toLiteral(m_jsUnitGenerator->jsClassMember(internalClassId, i))
2979 + u", "_s;
2980 const int currentArg = args + i;
2981 const QQmlJSScope::ConstPtr argType = registerType(currentArg).storedType();
2982 const QString consumedArg = consumedRegisterVariable(currentArg);
2983 result += convertStored(argType, propType, consumedArg) + u" },\n";
2984 }
2985
2986
2987 result += u"}";
2988 return result;
2989 };
2990
2991 if (contained == m_typeResolver->varType() || contained == m_typeResolver->variantMapType()) {
2992 m_body += m_state.accumulatorVariableOut + u" = "_s + createVariantMap() + u";\n"_s;
2993 return;
2994 }
2995
2996 if (contained == m_typeResolver->jsValueType()) {
2997 m_body += m_state.accumulatorVariableOut + u" = aotContext->engine->toScriptValue("_s
2998 + createVariantMap() + u");\n"_s;
2999 return;
3000 }
3001
3002 m_body += m_state.accumulatorVariableOut + u" = "_s + stored->augmentedInternalName();
3003 const bool isVariantOrPrimitive = (stored == m_typeResolver->varType())
3004 || (stored == m_typeResolver->jsPrimitiveType());
3005
3006 if (m_state.accumulatorOut().contains(stored)) {
3007 m_body += u"()";
3008 } else if (isVariantOrPrimitive) {
3009 m_body += u'(' + metaType(m_state.accumulatorOut().containedType()) + u')';
3010 } else {
3011 REJECT(u"storing an object literal in an unsupported container %1"_s
3012 .arg(stored->internalName()));
3013 }
3014 m_body += u";\n";
3015
3016 if (argc == 0)
3017 return;
3018
3019 bool isExtension = false;
3020 if (!m_typeResolver->canPopulate(contained, m_typeResolver->variantMapType(), &isExtension))
3021 REJECT(u"storing an object literal in a non-structured value type"_s);
3022
3023 const QQmlJSScope::ConstPtr accessor = isExtension
3024 ? contained->extensionType().scope
3025 : contained;
3026
3027 m_body += u"{\n";
3028 m_body += u" const QMetaObject *meta = ";
3029 if (!isExtension && isVariantOrPrimitive)
3030 m_body += m_state.accumulatorVariableOut + u".metaType().metaObject()";
3031 else
3032 m_body += metaObject(accessor);
3033 m_body += u";\n";
3034
3035 for (int i = 0; i < classSize; ++i) {
3036 m_body += u" {\n";
3037 const QString propName = m_jsUnitGenerator->jsClassMember(internalClassId, i);
3038 const int currentArg = args + i;
3039 const QQmlJSRegisterContent propType = m_state.readRegister(currentArg);
3040 const QQmlJSRegisterContent argType = registerType(currentArg);
3041 const QQmlJSMetaProperty property = contained->property(propName);
3042 const QString consumedArg = consumedRegisterVariable(currentArg);
3043 QString argument = conversion(argType, propType, consumedArg);
3044
3045 if (argument == consumedArg) {
3046 argument = registerVariable(currentArg);
3047 } else {
3048 m_body += u" "_s + propType.storedType()->augmentedInternalName()
3049 + u" arg = "_s + argument + u";\n";
3050 argument = u"arg"_s;
3051 }
3052
3053 int index = property.index();
3054 if (index == -1)
3055 continue;
3056
3057 const QString indexString = QString::number(index);
3058 m_body += u" void *argv[] = { %1, nullptr };\n"_s
3059 .arg(contentPointer(propType, argument));
3060 m_body += u" meta->property("_s + indexString;
3061 m_body += u").enclosingMetaObject()->d.static_metacall(reinterpret_cast<QObject *>(";
3062 m_body += contentPointer(m_state.accumulatorOut(), m_state.accumulatorVariableOut);
3063 m_body += u"), QMetaObject::WriteProperty, " + indexString + u", argv);\n";
3064 m_body += u" }\n";
3065 }
3066
3067 m_body += u"}\n";
3068
3069}
3070
3071void QQmlJSCodeGenerator::generate_CreateClass(int classIndex, int heritage, int computedNames)
3072{
3073 Q_UNUSED(classIndex)
3074 Q_UNUSED(heritage)
3075 Q_UNUSED(computedNames)
3077}
3078
3079void QQmlJSCodeGenerator::generate_CreateMappedArgumentsObject()
3080{
3082}
3083
3084void QQmlJSCodeGenerator::generate_CreateUnmappedArgumentsObject()
3085{
3087}
3088
3089void QQmlJSCodeGenerator::generate_CreateRestParameter(int argIndex)
3090{
3091 Q_UNUSED(argIndex)
3093}
3094
3095void QQmlJSCodeGenerator::generate_ConvertThisToObject()
3096{
3097 INJECT_TRACE_INFO(generate_ConvertThisToObject);
3098
3099 m_body += changedRegisterVariable() + u" = "_s
3100 + conversion(m_typeResolver->qObjectType(), m_state.changedRegister(),
3101 u"aotContext->thisObject()"_s)
3102 + u";\n"_s;
3103}
3104
3105void QQmlJSCodeGenerator::generate_LoadSuperConstructor()
3106{
3108}
3109
3110void QQmlJSCodeGenerator::generate_ToObject()
3111{
3113}
3114
3115void QQmlJSCodeGenerator::generate_Jump(int offset)
3116{
3117 INJECT_TRACE_INFO(generate_Jump);
3118
3119 generateJumpCodeWithTypeConversions(offset);
3120 m_skipUntilNextLabel = true;
3121 resetState();
3122}
3123
3124void QQmlJSCodeGenerator::generate_JumpTrue(int offset)
3125{
3126 INJECT_TRACE_INFO(generate_JumpTrue);
3127
3128 m_body += u"if ("_s;
3129 m_body += convertStored(m_state.accumulatorIn().storedType(), m_typeResolver->boolType(),
3130 m_state.accumulatorVariableIn);
3131 m_body += u") "_s;
3132 generateJumpCodeWithTypeConversions(offset);
3133}
3134
3135void QQmlJSCodeGenerator::generate_JumpFalse(int offset)
3136{
3137 INJECT_TRACE_INFO(generate_JumpFalse);
3138
3139 m_body += u"if (!"_s;
3140 m_body += convertStored(m_state.accumulatorIn().storedType(), m_typeResolver->boolType(),
3141 m_state.accumulatorVariableIn);
3142 m_body += u") "_s;
3143 generateJumpCodeWithTypeConversions(offset);
3144}
3145
3146void QQmlJSCodeGenerator::generate_JumpNoException(int offset)
3147{
3148 INJECT_TRACE_INFO(generate_JumpNoException);
3149
3150 m_body += u"if (!context->engine->hasException()) "_s;
3151 generateJumpCodeWithTypeConversions(offset);
3152}
3153
3154void QQmlJSCodeGenerator::generate_JumpNotUndefined(int offset)
3155{
3156 Q_UNUSED(offset)
3158}
3159
3160void QQmlJSCodeGenerator::generate_CheckException()
3161{
3162 INJECT_TRACE_INFO(generate_CheckException);
3163
3164 generateExceptionCheck();
3165}
3166
3167void QQmlJSCodeGenerator::generate_CmpEqNull()
3168{
3169 INJECT_TRACE_INFO(generate_CmpEqNull);
3170 generateEqualityOperation(literalType(m_typeResolver->nullType()), QString(), u"equals"_s, false);
3171}
3172
3173void QQmlJSCodeGenerator::generate_CmpNeNull()
3174{
3175 INJECT_TRACE_INFO(generate_CmlNeNull);
3176 generateEqualityOperation(literalType(m_typeResolver->nullType()), QString(), u"equals"_s, true);
3177}
3178
3179QString QQmlJSCodeGenerator::getLookupPreparation(
3180 QQmlJSRegisterContent content, const QString &var, int lookup)
3181{
3182 if (content.contains(content.storedType()))
3183 return QString();
3184
3185 if (content.storedType()->isOpaqueType()) {
3186 // Can't do meta-type lookups if the type is potentially unregistered,
3187 // which can be the case for mehods
3188 REJECT<QString>("Can't prepare lookup involving non-registered types"_L1);
3189 }
3190
3191 if (content.isStoredIn(m_typeResolver->varType())) {
3192 return var + u" = QVariant(aotContext->lookupResultMetaType("_s
3193 + QString::number(lookup) + u"))"_s;
3194 }
3195
3196 if (content.isStoredIn(m_typeResolver->jsPrimitiveType())) {
3197 return var + u" = QJSPrimitiveValue(aotContext->lookupResultMetaType("_s
3198 + QString::number(lookup) + u"))"_s;
3199 }
3200
3201 // TODO: We could make sure they're compatible, for example QObject pointers.
3202 return QString();
3203}
3204
3205QString QQmlJSCodeGenerator::contentPointer(QQmlJSRegisterContent content, const QString &var)
3206{
3207 const QQmlJSScope::ConstPtr stored = content.storedType();
3208 if (content.contains(stored))
3209 return u'&' + var;
3210
3211 if (content.isStoredIn(m_typeResolver->varType())
3212 || content.isStoredIn(m_typeResolver->jsPrimitiveType())) {
3213 return var + u".data()"_s;
3214 }
3215
3216 if (stored->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
3217 return u'&' + var;
3218
3219 if (m_typeResolver->isNumeric(content.storedType())
3220 && content.containedType()->scopeType() == QQmlSA::ScopeType::EnumScope) {
3221 return u'&' + var;
3222 }
3223
3224 if (stored->isListProperty() && content.containedType()->isListProperty())
3225 return u'&' + var;
3226
3227 REJECT<QString>(
3228 u"content pointer of unsupported wrapper type "_s + content.descriptiveName());
3229}
3230
3231QString QQmlJSCodeGenerator::contentType(QQmlJSRegisterContent content, const QString &var)
3232{
3233 const QQmlJSScope::ConstPtr stored = content.storedType();
3234 const QQmlJSScope::ConstPtr contained = content.containedType();
3235 if (contained == stored)
3236 return metaTypeFromType(stored);
3237
3238 if (stored == m_typeResolver->varType() || stored == m_typeResolver->jsPrimitiveType())
3239 return var + u".metaType()"_s; // We expect the container to be initialized
3240
3241 if (stored->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
3242 return metaType(contained);
3243
3244 const QQmlJSScope::ConstPtr nonComposite = QQmlJSScope::nonCompositeBaseType(contained);
3245 if (m_typeResolver->isNumeric(stored) && nonComposite->scopeType() == QQmlSA::ScopeType::EnumScope)
3246 return metaTypeFromType(nonComposite->baseType());
3247
3248 if (stored->isListProperty() && contained->isListProperty())
3249 return metaType(contained);
3250
3251 REJECT<QString>(
3252 u"content type of unsupported wrapper type "_s + content.descriptiveName());
3253}
3254
3255void QQmlJSCodeGenerator::generate_CmpEqInt(int lhsConst)
3256{
3257 INJECT_TRACE_INFO(generate_CmpEqInt);
3258
3259 generateEqualityOperation(
3260 literalType(m_typeResolver->int32Type()), QString::number(lhsConst), u"equals"_s, false);
3261}
3262
3263void QQmlJSCodeGenerator::generate_CmpNeInt(int lhsConst)
3264{
3265 INJECT_TRACE_INFO(generate_CmpNeInt);
3266
3267 generateEqualityOperation(
3268 literalType(m_typeResolver->int32Type()), QString::number(lhsConst), u"equals"_s, true);
3269}
3270
3271void QQmlJSCodeGenerator::generate_CmpEq(int lhs)
3272{
3273 INJECT_TRACE_INFO(generate_CmpEq);
3274 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"equals"_s, false);
3275}
3276
3277void QQmlJSCodeGenerator::generate_CmpNe(int lhs)
3278{
3279 INJECT_TRACE_INFO(generate_CmpNe);
3280 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"equals"_s, true);
3281}
3282
3283void QQmlJSCodeGenerator::generate_CmpGt(int lhs)
3284{
3285 INJECT_TRACE_INFO(generate_CmpGt);
3286 generateCompareOperation(lhs, u">"_s);
3287}
3288
3289void QQmlJSCodeGenerator::generate_CmpGe(int lhs)
3290{
3291 INJECT_TRACE_INFO(generate_CmpGe);
3292 generateCompareOperation(lhs, u">="_s);
3293}
3294
3295void QQmlJSCodeGenerator::generate_CmpLt(int lhs)
3296{
3297 INJECT_TRACE_INFO(generate_CmpLt);
3298 generateCompareOperation(lhs, u"<"_s);
3299}
3300
3301void QQmlJSCodeGenerator::generate_CmpLe(int lhs)
3302{
3303 INJECT_TRACE_INFO(generate_CmpLe);
3304 generateCompareOperation(lhs, u"<="_s);
3305}
3306
3307void QQmlJSCodeGenerator::generate_CmpStrictEqual(int lhs)
3308{
3309 INJECT_TRACE_INFO(generate_CmpStrictEqual);
3310 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"strictlyEquals"_s, false);
3311}
3312
3313void QQmlJSCodeGenerator::generate_CmpStrictNotEqual(int lhs)
3314{
3315 INJECT_TRACE_INFO(generate_CmpStrictNotEqual);
3316 generateEqualityOperation(registerType(lhs), registerVariable(lhs), u"strictlyEquals"_s, true);
3317}
3318
3319void QQmlJSCodeGenerator::generate_CmpIn(int lhs)
3320{
3321 Q_UNUSED(lhs)
3322 REJECT(u"CmpIn"_s);
3323}
3324
3325void QQmlJSCodeGenerator::generate_CmpInstanceOf(int lhs)
3326{
3327 Q_UNUSED(lhs)
3329}
3330
3331void QQmlJSCodeGenerator::generate_As(int lhs)
3332{
3333 INJECT_TRACE_INFO(generate_As);
3334
3335 const QString input = registerVariable(lhs);
3336 const QQmlJSRegisterContent inputContent = m_state.readRegister(lhs);
3337 const QQmlJSRegisterContent outputContent = m_state.accumulatorOut();
3338
3339 // If the originalType output is a conversion, we're supposed to check for the contained
3340 // type and if it doesn't match, set the result to null or undefined.
3341 const QQmlJSRegisterContent originalContent = originalType(outputContent);
3342 QQmlJSScope::ConstPtr target;
3343 if (originalContent.containedType()->isReferenceType())
3344 target = originalContent.containedType();
3345 else if (originalContent.isConversion())
3346 target = m_typeResolver->extractNonVoidFromOptionalType(originalContent).containedType();
3347 else if (originalContent.variant() == QQmlJSRegisterContent::Cast)
3348 target = originalContent.containedType();
3349
3350 if (!target)
3351 REJECT(u"type assertion to unknown type"_s);
3352
3353 const bool isTrivial = m_typeResolver->inherits(
3354 m_typeResolver->originalContainedType(inputContent), target);
3355
3356 m_body += m_state.accumulatorVariableOut + u" = "_s;
3357
3358 if (!isTrivial && target->isReferenceType()) {
3359 const QQmlJSScope::ConstPtr genericContained = m_typeResolver->genericType(target);
3360 const QString inputConversion = inputContent.storedType()->isReferenceType()
3361 ? input
3362 : convertStored(inputContent.storedType(), genericContained, input);
3363
3364 if (target->isComposite()
3365 && m_state.accumulatorIn().isStoredIn(m_typeResolver->metaObjectType())) {
3366 m_body += conversion(
3367 genericContained, outputContent,
3368 m_state.accumulatorVariableIn + u"->cast("_s + inputConversion + u')');
3369 } else {
3370 m_body += conversion(
3371 genericContained, outputContent,
3372 u'(' + metaObject(target) + u")->cast("_s + inputConversion + u')');
3373 }
3374 m_body += u";\n"_s;
3375 return;
3376 }
3377
3378 if (inputContent.isStoredIn(m_typeResolver->varType())
3379 || inputContent.isStoredIn(m_typeResolver->jsPrimitiveType())) {
3380
3381 const auto source = m_typeResolver->extractNonVoidFromOptionalType(
3382 originalType(inputContent)).containedType();
3383
3384 if (source && source == target) {
3385 m_body += input + u".metaType() == "_s + metaType(target)
3386 + u" ? " + conversion(inputContent, outputContent, input)
3387 + u" : " + conversion(
3388 literalType(m_typeResolver->voidType()), outputContent, QString());
3389 m_body += u";\n"_s;
3390 return;
3391 }
3392 }
3393
3394 if (isTrivial) {
3395 // No actual conversion necessary. The 'as' is a no-op
3396 m_body += conversion(inputContent, m_state.accumulatorOut(), input) + u";\n"_s;
3397 return;
3398 }
3399
3400 REJECT(u"non-trivial value type assertion"_s);
3401}
3402
3403void QQmlJSCodeGenerator::generate_UNot()
3404{
3405 INJECT_TRACE_INFO(generate_UNot);
3406 generateUnaryOperation(u"!"_s);
3407}
3408
3409void QQmlJSCodeGenerator::generate_UPlus()
3410{
3411 INJECT_TRACE_INFO(generate_UPlus);
3412 generateUnaryOperation(u"+"_s);
3413}
3414
3415void QQmlJSCodeGenerator::generate_UMinus()
3416{
3417 INJECT_TRACE_INFO(generate_UMinus);
3418 generateUnaryOperation(u"-"_s);
3419}
3420
3421void QQmlJSCodeGenerator::generate_UCompl()
3422{
3423 INJECT_TRACE_INFO(generate_UCompl);
3424 generateUnaryOperation(u"~"_s);
3425}
3426
3427void QQmlJSCodeGenerator::generate_Increment()
3428{
3429 INJECT_TRACE_INFO(generate_Increment);
3430 generateInPlaceOperation(u"++"_s);
3431}
3432
3433void QQmlJSCodeGenerator::generate_Decrement()
3434{
3435 INJECT_TRACE_INFO(generate_Decrement);
3436 generateInPlaceOperation(u"--"_s);
3437}
3438
3439void QQmlJSCodeGenerator::generate_Add(int lhs)
3440{
3441 INJECT_TRACE_INFO(generate_Add);
3442 generateArithmeticOperation(lhs, u"+"_s);
3443}
3444
3445void QQmlJSCodeGenerator::generate_BitAnd(int lhs)
3446{
3447 INJECT_TRACE_INFO(generate_BitAnd);
3448 generateArithmeticOperation(lhs, u"&"_s);
3449}
3450
3451void QQmlJSCodeGenerator::generate_BitOr(int lhs)
3452{
3453 INJECT_TRACE_INFO(generate_BitOr);
3454 generateArithmeticOperation(lhs, u"|"_s);
3455}
3456
3457void QQmlJSCodeGenerator::generate_BitXor(int lhs)
3458{
3459 INJECT_TRACE_INFO(generate_BitXor);
3460 generateArithmeticOperation(lhs, u"^"_s);
3461}
3462
3463void QQmlJSCodeGenerator::generate_UShr(int lhs)
3464{
3465 INJECT_TRACE_INFO(generate_BitUShr);
3466 generateShiftOperation(lhs, u">>"_s);
3467}
3468
3469void QQmlJSCodeGenerator::generate_Shr(int lhs)
3470{
3471 INJECT_TRACE_INFO(generate_Shr);
3472 generateShiftOperation(lhs, u">>"_s);
3473}
3474
3475void QQmlJSCodeGenerator::generate_Shl(int lhs)
3476{
3477 INJECT_TRACE_INFO(generate_Shl);
3478 generateShiftOperation(lhs, u"<<"_s);
3479}
3480
3481void QQmlJSCodeGenerator::generate_BitAndConst(int rhs)
3482{
3483 INJECT_TRACE_INFO(generate_BitAndConst);
3484 generateArithmeticConstOperation(rhs, u"&"_s);
3485}
3486
3487void QQmlJSCodeGenerator::generate_BitOrConst(int rhs)
3488{
3489 INJECT_TRACE_INFO(generate_BitOrConst);
3490 generateArithmeticConstOperation(rhs, u"|"_s);
3491}
3492
3493void QQmlJSCodeGenerator::generate_BitXorConst(int rhs)
3494{
3495 INJECT_TRACE_INFO(generate_BitXorConst);
3496 generateArithmeticConstOperation(rhs, u"^"_s);
3497}
3498
3499void QQmlJSCodeGenerator::generate_UShrConst(int rhs)
3500{
3501 INJECT_TRACE_INFO(generate_UShrConst);
3502 generateArithmeticConstOperation(rhs & 0x1f, u">>"_s);
3503}
3504
3505void QQmlJSCodeGenerator::generate_ShrConst(int rhs)
3506{
3507 INJECT_TRACE_INFO(generate_ShrConst);
3508 generateArithmeticConstOperation(rhs & 0x1f, u">>"_s);
3509}
3510
3511void QQmlJSCodeGenerator::generate_ShlConst(int rhs)
3512{
3513 INJECT_TRACE_INFO(generate_ShlConst);
3514 generateArithmeticConstOperation(rhs & 0x1f, u"<<"_s);
3515}
3516
3517void QQmlJSCodeGenerator::generate_Exp(int lhs)
3518{
3519 INJECT_TRACE_INFO(generate_Exp);
3520
3521 const QString lhsString = conversion(
3522 registerType(lhs), m_state.readRegister(lhs), consumedRegisterVariable(lhs));
3523 const QString rhsString = conversion(
3524 m_state.accumulatorIn(), m_state.readAccumulator(),
3525 consumedAccumulatorVariableIn());
3526
3527 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !lhsString.isEmpty());
3528 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !rhsString.isEmpty());
3529
3530 const QQmlJSRegisterContent originalOut = originalType(m_state.accumulatorOut());
3531 m_body += m_state.accumulatorVariableOut + u" = "_s;
3532 m_body += conversion(
3533 originalOut, m_state.accumulatorOut(),
3534 u"QQmlPrivate::jsExponentiate("_s + lhsString + u", "_s + rhsString + u')');
3535 m_body += u";\n"_s;
3536}
3537
3538void QQmlJSCodeGenerator::generate_Mul(int lhs)
3539{
3540 INJECT_TRACE_INFO(generate_Mul);
3541 generateArithmeticOperation(lhs, u"*"_s);
3542}
3543
3544void QQmlJSCodeGenerator::generate_Div(int lhs)
3545{
3546 INJECT_TRACE_INFO(generate_Div);
3547 GeneratePragmaWarningBlock warningBlock(this);
3548 warningBlock.silenceDivideByZero();
3549 generateArithmeticOperation(lhs, u"/"_s);
3550}
3551
3552void QQmlJSCodeGenerator::generate_Mod(int lhs)
3553{
3554 INJECT_TRACE_INFO(generate_Mod);
3555
3556 const auto lhsVar = convertStored(
3557 registerType(lhs).storedType(), m_typeResolver->jsPrimitiveType(),
3558 consumedRegisterVariable(lhs));
3559 const auto rhsVar = convertStored(
3560 m_state.accumulatorIn().storedType(), m_typeResolver->jsPrimitiveType(),
3561 consumedAccumulatorVariableIn());
3562 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !lhsVar.isEmpty());
3563 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !rhsVar.isEmpty());
3564
3565 m_body += m_state.accumulatorVariableOut;
3566 m_body += u" = "_s;
3567 m_body += conversion(m_typeResolver->jsPrimitiveType(), m_state.accumulatorOut(),
3568 u'(' + lhsVar + u" % "_s + rhsVar + u')');
3569 m_body += u";\n"_s;
3570}
3571
3572void QQmlJSCodeGenerator::generate_Sub(int lhs)
3573{
3574 INJECT_TRACE_INFO(generate_Sub);
3575 generateArithmeticOperation(lhs, u"-"_s);
3576}
3577
3578void QQmlJSCodeGenerator::generate_InitializeBlockDeadTemporalZone(int firstReg, int count)
3579{
3580 Q_UNUSED(firstReg)
3581 Q_UNUSED(count)
3582 // Ignore. We reject uninitialized values anyway.
3583}
3584
3585void QQmlJSCodeGenerator::generate_ThrowOnNullOrUndefined()
3586{
3588}
3589
3590void QQmlJSCodeGenerator::generate_GetTemplateObject(int index)
3591{
3592 Q_UNUSED(index)
3594}
3595
3596QV4::Moth::ByteCodeHandler::Verdict QQmlJSCodeGenerator::startInstruction(
3597 QV4::Moth::Instr::Type type)
3598{
3599 m_state.State::operator=(nextStateFromAnnotations(m_state, m_annotations));
3600 const auto accumulatorIn = m_state.registers.find(Accumulator);
3601 if (accumulatorIn != m_state.registers.end()
3602 && isTypeStorable(m_typeResolver, accumulatorIn.value().content.storedType())) {
3603 QQmlJSRegisterContent content = accumulatorIn.value().content;
3604 m_state.accumulatorVariableIn = m_registerVariables.value(content).variableName;
3605 Q_ASSERT(!m_state.accumulatorVariableIn.isEmpty());
3606 } else {
3607 m_state.accumulatorVariableIn.clear();
3608 }
3609
3610 auto labelIt = m_labels.constFind(currentInstructionOffset());
3611 if (labelIt != m_labels.constEnd()) {
3612 m_body += *labelIt + u":;\n"_s;
3613 m_skipUntilNextLabel = false;
3614 } else if (m_skipUntilNextLabel && !instructionManipulatesContext(type)) {
3615 return SkipInstruction;
3616 }
3617
3618 if (m_state.changedRegisterIndex() == Accumulator)
3619 m_state.accumulatorVariableOut = changedRegisterVariable();
3620 else
3621 m_state.accumulatorVariableOut.clear();
3622
3623 // If the accumulator type is valid, we want an accumulator variable.
3624 // If not, we don't want one.
3625 Q_ASSERT(m_state.changedRegisterIndex() == Accumulator
3626 || m_state.accumulatorVariableOut.isEmpty());
3627 Q_ASSERT(m_state.changedRegisterIndex() != Accumulator
3628 || !m_state.accumulatorVariableOut.isEmpty()
3629 || !isTypeStorable(m_typeResolver, m_state.changedRegister().storedType()));
3630
3631 // If the instruction has no side effects and doesn't write any register, it's dead.
3632 // We might still need the label, though, and the source code comment.
3633 if (!m_state.hasInternalSideEffects() && changedRegisterVariable().isEmpty()) {
3634 generateJumpCodeWithTypeConversions(0);
3635 return SkipInstruction;
3636 }
3637
3638 return ProcessInstruction;
3639}
3640
3641void QQmlJSCodeGenerator::endInstruction(QV4::Moth::Instr::Type)
3642{
3643 if (!m_skipUntilNextLabel)
3644 generateJumpCodeWithTypeConversions(0);
3645 m_pool->clearTemporaries();
3646}
3647
3648void QQmlJSCodeGenerator::generateSetInstructionPointer()
3649{
3650 m_body += u"aotContext->setInstructionPointer("_s
3651 + QString::number(nextInstructionOffset()) + u");\n"_s;
3652}
3653
3654void QQmlJSCodeGenerator::generateExceptionCheck()
3655{
3656 m_body += u"if (aotContext->engine->hasError()) {\n"_s;
3657 generateReturnError();
3658 m_body += u"}\n"_s;
3659}
3660
3661void QQmlJSCodeGenerator::generateEqualityOperation(
3662 QQmlJSRegisterContent lhsContent, QQmlJSRegisterContent rhsContent,
3663 const QString &lhsName, const QString &rhsName, const QString &function, bool invert)
3664{
3665 const bool lhsIsOptional = m_typeResolver->isOptionalType(lhsContent);
3666 const bool rhsIsOptional = m_typeResolver->isOptionalType(rhsContent);
3667
3668 const QQmlJSScope::ConstPtr rhsContained = rhsIsOptional
3669 ? m_typeResolver->extractNonVoidFromOptionalType(rhsContent).containedType()
3670 : rhsContent.containedType();
3671
3672 const QQmlJSScope::ConstPtr lhsContained = lhsIsOptional
3673 ? m_typeResolver->extractNonVoidFromOptionalType(lhsContent).containedType()
3674 : lhsContent.containedType();
3675
3676 const bool isStrict = function == "strictlyEquals"_L1;
3677 const bool strictlyComparableWithVar
3678 = isStrict && canStrictlyCompareWithVar(m_typeResolver, lhsContained, rhsContained);
3679 auto isComparable = [&]() {
3680 if (m_typeResolver->isPrimitive(lhsContent) && m_typeResolver->isPrimitive(rhsContent))
3681 return true;
3682 if (m_typeResolver->isNumeric(lhsContent) && m_typeResolver->isNumeric(rhsContent))
3683 return true;
3684 if (m_typeResolver->isNumeric(lhsContent) && rhsContent.isEnumeration())
3685 return true;
3686 if (m_typeResolver->isNumeric(rhsContent) && lhsContent.isEnumeration())
3687 return true;
3688 if (strictlyComparableWithVar)
3689 return true;
3690 if (canCompareWithQObject(m_typeResolver, lhsContained, rhsContained))
3691 return true;
3692 if (canCompareWithQUrl(m_typeResolver, lhsContained, rhsContained))
3693 return true;
3694 return false;
3695 };
3696
3697 const auto retrieveOriginal = [this](QQmlJSRegisterContent content) {
3698 const auto contained = content.containedType();
3699 const auto originalContent = originalType(content);
3700 const auto containedOriginal = originalContent.containedType();
3701
3702 if (originalContent.isStoredIn(m_typeResolver->genericType(containedOriginal))) {
3703 // The original type doesn't need any wrapping.
3704 return originalContent;
3705 } else if (contained == containedOriginal) {
3706 if (originalContent.isConversion()) {
3707 // The original conversion origins are more accurate
3708 return m_pool->storedIn(originalContent, content.storedType());
3709 }
3710 } else if (m_typeResolver->canHold(contained, containedOriginal)) {
3711 return m_pool->storedIn(originalContent, content.storedType());
3712 }
3713
3714 return content;
3715 };
3716
3717 const QQmlJSScope::ConstPtr lhsType = lhsContent.storedType();
3718 const QQmlJSScope::ConstPtr rhsType = rhsContent.storedType();
3719
3720 if (!isComparable()) {
3721 QQmlJSRegisterContent lhsOriginal = retrieveOriginal(lhsContent);
3722 QQmlJSRegisterContent rhsOriginal = retrieveOriginal(rhsContent);
3723 if (lhsOriginal.containedType() != lhsContent.containedType()
3724 || lhsOriginal.storedType() != lhsType
3725 || rhsOriginal.containedType() != rhsContent.containedType()
3726 || rhsOriginal.storedType() != rhsType) {
3727 // If either side is simply a wrapping of a specific type into a more general one, we
3728 // can compare the original types instead. You can't nest wrappings after all.
3729 generateEqualityOperation(lhsOriginal, rhsOriginal,
3730 conversion(lhsType, lhsOriginal, lhsName),
3731 conversion(rhsType, rhsOriginal, rhsName),
3732 function, invert);
3733 return;
3734 }
3735
3736 REJECT(u"incomparable types %1 and %2"_s.arg(
3737 rhsContent.descriptiveName(), lhsContent.descriptiveName()));
3738 }
3739
3740 if (strictlyComparableWithVar) {
3741 // Determine which side is holding a storable type
3742 if (!lhsName.isEmpty() && rhsName.isEmpty()) {
3743 // lhs register holds var type and rhs is not storable
3744 generateVariantEqualityComparison(rhsContent, lhsName, invert);
3745 return;
3746 }
3747
3748 if (!rhsName.isEmpty() && lhsName.isEmpty()) {
3749 // lhs content is not storable and rhs is var type
3750 generateVariantEqualityComparison(lhsContent, rhsName, invert);
3751 return;
3752 }
3753
3754 if (lhsContent.contains(m_typeResolver->varType())) {
3755 generateVariantEqualityComparison(rhsContent, rhsName, lhsName, invert);
3756 return;
3757 }
3758
3759 if (rhsContent.contains(m_typeResolver->varType())) {
3760 generateVariantEqualityComparison(lhsContent, lhsName, rhsName, invert);
3761 return;
3762 }
3763
3764 // It shouldn't be possible to get here because optional null should be stored in
3765 // QJSPrimitiveValue, not in QVariant. But let's rather be safe than sorry.
3766 REJECT(u"comparison of optional null"_s);
3767 }
3768
3769 const auto comparison = [&]() -> QString {
3770 const auto primitive = m_typeResolver->jsPrimitiveType();
3771 const QString sign = invert ? u" != "_s : u" == "_s;
3772
3773 if (lhsType == rhsType && lhsType != primitive && lhsType != m_typeResolver->varType()) {
3774
3775 // Straight forward comparison of equal types,
3776 // except QJSPrimitiveValue which has two comparison functions.
3777
3778 if (isTypeStorable(m_typeResolver, lhsType))
3779 return lhsName + sign + rhsName;
3780
3781 // null === null and undefined === undefined
3782 return invert ? u"false"_s : u"true"_s;
3783 }
3784
3785 if (canCompareWithQObject(m_typeResolver, lhsType, rhsType)) {
3786 // Comparison of QObject-derived with nullptr or different QObject-derived.
3787 return (isTypeStorable(m_typeResolver, lhsType) ? lhsName : u"nullptr"_s)
3788 + sign
3789 + (isTypeStorable(m_typeResolver, rhsType) ? rhsName : u"nullptr"_s);
3790 }
3791
3792 if (canCompareWithQObject(m_typeResolver, lhsContained, rhsContained)) {
3793 // Comparison of optional QObject-derived with nullptr or different QObject-derived.
3794 // Mind that null == undefined but null !== undefined
3795 // Therefore the isStrict dance.
3796
3797 QString result;
3798 if (isStrict) {
3799 if (lhsIsOptional) {
3800 if (rhsIsOptional) {
3801 // If both are invalid we're fine
3802 result += u"(!"_s
3803 + lhsName + u".isValid() && !"_s
3804 + rhsName + u".isValid()) || "_s;
3805 }
3806
3807 result += u'(' + lhsName + u".isValid() && "_s;
3808 } else {
3809 result += u'(';
3810 }
3811
3812 if (rhsIsOptional) {
3813 result += rhsName + u".isValid() && "_s;
3814 }
3815 } else {
3816 result += u'(';
3817 }
3818
3819 // We do not implement comparison with explicit undefined, yet. Only with null.
3820 Q_ASSERT(lhsType != m_typeResolver->voidType());
3821 Q_ASSERT(rhsType != m_typeResolver->voidType());
3822
3823 const auto resolvedName = [&](const QString name) -> QString {
3824 // If isStrict we check validity already before.
3825 const QString content = u"*static_cast<QObject **>("_s + name + u".data())"_s;
3826 return isStrict
3827 ? content
3828 : u'(' + name + u".isValid() ? "_s + content + u" : nullptr)"_s;
3829 };
3830
3831 const QString lhsResolved = lhsIsOptional ? resolvedName(lhsName) : lhsName;
3832 const QString rhsResolved = rhsIsOptional ? resolvedName(rhsName) : rhsName;
3833
3834 return (invert ? u"!("_s : u"("_s) + result
3835 + (isTypeStorable(m_typeResolver, lhsType) ? lhsResolved : u"nullptr"_s)
3836 + u" == "_s
3837 + (isTypeStorable(m_typeResolver, rhsType) ? rhsResolved : u"nullptr"_s)
3838 + u"))"_s;
3839 }
3840
3841 if ((m_typeResolver->isUnsignedInteger(rhsType)
3842 && m_typeResolver->isUnsignedInteger(lhsType))
3843 || (m_typeResolver->isSignedInteger(rhsType)
3844 && m_typeResolver->isSignedInteger(lhsType))) {
3845 // Both integers of same signedness: Let the C++ compiler perform the type promotion
3846 return lhsName + sign + rhsName;
3847 }
3848
3849 if (rhsType == m_typeResolver->boolType() && m_typeResolver->isIntegral(lhsType)) {
3850 // Integral and bool: We can promote the bool to the integral type
3851 return lhsName + sign + convertStored(rhsType, lhsType, rhsName);
3852 }
3853
3854 if (lhsType == m_typeResolver->boolType() && m_typeResolver->isIntegral(rhsType)) {
3855 // Integral and bool: We can promote the bool to the integral type
3856 return convertStored(lhsType, rhsType, lhsName) + sign + rhsName;
3857 }
3858
3859 if (m_typeResolver->isNumeric(lhsType) && m_typeResolver->isNumeric(rhsType)) {
3860 // Both numbers: promote them to double
3861 return convertStored(lhsType, m_typeResolver->realType(), lhsName)
3862 + sign
3863 + convertStored(rhsType, m_typeResolver->realType(), rhsName);
3864 }
3865
3866 // If none of the above matches, we have to use QJSPrimitiveValue
3867 return (invert ? u"!"_s : QString())
3868 + convertStored(lhsType, primitive, lhsName)
3869 + u'.' + function + u'(' + convertStored(rhsType, primitive, rhsName) + u')';
3870 };
3871
3872 m_body += m_state.accumulatorVariableOut + u" = "_s;
3873 m_body += conversion(m_typeResolver->boolType(), m_state.accumulatorOut(), comparison());
3874 m_body += u";\n"_s;
3875}
3876
3877void QQmlJSCodeGenerator::generateCompareOperation(int lhs, const QString &cppOperator)
3878{
3879 m_body += m_state.accumulatorVariableOut + u" = "_s;
3880
3881 const auto lhsType = registerType(lhs);
3882 const QQmlJSScope::ConstPtr compareType =
3883 m_typeResolver->isNumeric(lhsType) && m_typeResolver->isNumeric(m_state.accumulatorIn())
3884 ? m_typeResolver->merge(lhsType.storedType(), m_state.accumulatorIn().storedType())
3885 : m_typeResolver->jsPrimitiveType();
3886
3887 m_body += conversion(
3888 m_typeResolver->boolType(), m_state.accumulatorOut(),
3889 convertStored(registerType(lhs).storedType(), compareType,
3890 consumedRegisterVariable(lhs))
3891 + u' ' + cppOperator + u' '
3892 + convertStored(m_state.accumulatorIn().storedType(), compareType,
3893 consumedAccumulatorVariableIn()));
3894 m_body += u";\n"_s;
3895}
3896
3897void QQmlJSCodeGenerator::generateArithmeticOperation(int lhs, const QString &cppOperator)
3898{
3899 generateArithmeticOperation(
3900 conversion(registerType(lhs), m_state.readRegister(lhs),
3901 consumedRegisterVariable(lhs)),
3902 conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3903 consumedAccumulatorVariableIn()),
3904 cppOperator);
3905}
3906
3907void QQmlJSCodeGenerator::generateShiftOperation(int lhs, const QString &cppOperator)
3908{
3909 generateArithmeticOperation(
3910 conversion(registerType(lhs), m_state.readRegister(lhs),
3911 consumedRegisterVariable(lhs)),
3912 u'(' + conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3913 consumedAccumulatorVariableIn()) + u" & 0x1f)"_s,
3914 cppOperator);
3915}
3916
3917void QQmlJSCodeGenerator::generateArithmeticOperation(
3918 const QString &lhs, const QString &rhs, const QString &cppOperator)
3919{
3920 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !lhs.isEmpty());
3921 Q_ASSERT(m_logger->currentFunctionHasCompileError() || !rhs.isEmpty());
3922
3923 const QQmlJSRegisterContent originalOut = originalType(m_state.accumulatorOut());
3924 m_body += m_state.accumulatorVariableOut;
3925 m_body += u" = "_s;
3926 const QString explicitCast
3927 = originalOut.isStoredIn(m_typeResolver->stringType())
3928 ? originalOut.storedType()->internalName()
3929 : QString();
3930 m_body += conversion(
3931 originalOut, m_state.accumulatorOut(),
3932 explicitCast + u'(' + lhs + u' ' + cppOperator + u' ' + rhs + u')');
3933 m_body += u";\n"_s;
3934}
3935
3936void QQmlJSCodeGenerator::generateArithmeticConstOperation(int rhsConst, const QString &cppOperator)
3937{
3938 generateArithmeticOperation(
3939 conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3940 consumedAccumulatorVariableIn()),
3941 conversion(literalType(m_typeResolver->int32Type()),
3942 m_state.readAccumulator(), QString::number(rhsConst)),
3943 cppOperator);
3944}
3945
3946void QQmlJSCodeGenerator::generateUnaryOperation(const QString &cppOperator)
3947{
3948 const auto var = conversion(m_state.accumulatorIn(),
3949 originalType(m_state.readAccumulator()),
3950 consumedAccumulatorVariableIn());
3951
3952 if (var == m_state.accumulatorVariableOut) {
3953 m_body += m_state.accumulatorVariableOut + u" = "_s + cppOperator + var + u";\n"_s;
3954 return;
3955 }
3956
3957 const auto originalResult = originalType(m_state.accumulatorOut());
3958 if (m_state.accumulatorOut() == originalResult) {
3959 m_body += m_state.accumulatorVariableOut + u" = "_s + var + u";\n"_s;
3960 m_body += m_state.accumulatorVariableOut + u" = "_s
3961 + cppOperator + m_state.accumulatorVariableOut + u";\n"_s;
3962 return;
3963 }
3964
3965 m_body += m_state.accumulatorVariableOut + u" = "_s + conversion(
3966 originalResult, m_state.accumulatorOut(), cppOperator + var) + u";\n"_s;
3967}
3968
3969void QQmlJSCodeGenerator::generateInPlaceOperation(const QString &cppOperator)
3970{
3971 {
3972 // If actually in place, we cannot consume the variable.
3973 const QString var = conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3974 m_state.accumulatorVariableIn);
3975 if (var == m_state.accumulatorVariableOut) {
3976 m_body += cppOperator + var + u";\n"_s;
3977 return;
3978 }
3979 }
3980
3981 const QString var = conversion(m_state.accumulatorIn(), m_state.readAccumulator(),
3982 consumedAccumulatorVariableIn());
3983
3984 const auto originalResult = originalType(m_state.accumulatorOut());
3985 if (m_state.accumulatorOut() == originalResult) {
3986 m_body += m_state.accumulatorVariableOut + u" = "_s + var + u";\n"_s;
3987 m_body += cppOperator + m_state.accumulatorVariableOut + u";\n"_s;
3988 return;
3989 }
3990
3991 m_body += u"{\n"_s;
3992 m_body += u"auto converted = "_s + var + u";\n"_s;
3993 m_body += m_state.accumulatorVariableOut + u" = "_s + conversion(
3994 originalResult, m_state.accumulatorOut(), u'('
3995 + cppOperator + u"converted)"_s) + u";\n"_s;
3996 m_body += u"}\n"_s;
3997}
3998
3999void QQmlJSCodeGenerator::generateLookup(const QString &lookup, const QString &initialization,
4000 const QString &resultPreparation)
4001{
4002 m_body += u"#ifndef QT_NO_DEBUG\n"_s;
4003 generateSetInstructionPointer();
4004 m_body += u"#endif\n"_s;
4005
4006 if (!resultPreparation.isEmpty())
4007 m_body += resultPreparation + u";\n"_s;
4008 m_body += u"while (!"_s + lookup + u") {\n"_s;
4009
4010 m_body += u"#ifdef QT_NO_DEBUG\n"_s;
4011 generateSetInstructionPointer();
4012 m_body += u"#endif\n"_s;
4013
4014 m_body += initialization + u";\n"_s;
4015 generateExceptionCheck();
4016 if (!resultPreparation.isEmpty())
4017 m_body += resultPreparation + u";\n"_s;
4018 m_body += u"}\n"_s;
4019}
4020
4021void QQmlJSCodeGenerator::generateJumpCodeWithTypeConversions(int relativeOffset)
4022{
4023 QString conversionCode;
4024 const int absoluteOffset = nextInstructionOffset() + relativeOffset;
4025 const auto annotation = m_annotations.find(absoluteOffset);
4026 if (static_cast<InstructionAnnotations::const_iterator>(annotation) != m_annotations.constEnd()) {
4027 const auto &conversions = annotation->second.typeConversions;
4028
4029 for (auto regIt = conversions.constBegin(), regEnd = conversions.constEnd();
4030 regIt != regEnd; ++regIt) {
4031 const QQmlJSRegisterContent targetType = regIt.value().content;
4032 if (!targetType.isValid() || !isTypeStorable(m_typeResolver, targetType.storedType()))
4033 continue;
4034
4035 const int registerIndex = regIt.key();
4036 const auto variable = m_registerVariables.constFind(targetType);
4037
4038 if (variable == m_registerVariables.constEnd())
4039 continue;
4040
4041 QQmlJSRegisterContent currentType;
4042 QString currentVariable;
4043 if (registerIndex == m_state.changedRegisterIndex()) {
4044 currentVariable = changedRegisterVariable();
4045 if (variable->variableName == currentVariable)
4046 continue;
4047
4048 currentType = m_state.changedRegister();
4049
4050 // TODO: We can std::move the changed register in some cases here but it needs be
4051 // done carefully (QTBUG-141920).
4052 } else {
4053 const auto it = m_state.registers.find(registerIndex);
4054 if (it == m_state.registers.end()
4055 || variable->variableName == registerVariable(registerIndex)) {
4056 continue;
4057 }
4058
4059 currentType = it.value().content;
4060 currentVariable = consumedRegisterVariable(registerIndex);
4061 }
4062
4063 // Actually == here. We want the jump code also for equal types
4064 if (currentType == targetType)
4065 continue;
4066
4067 conversionCode += variable->variableName;
4068 conversionCode += u" = "_s;
4069 conversionCode += conversion(currentType, targetType, currentVariable);
4070 conversionCode += u";\n"_s;
4071 }
4072 }
4073
4074 if (relativeOffset) {
4075 auto labelIt = m_labels.find(absoluteOffset);
4076 if (labelIt == m_labels.end())
4077 labelIt = m_labels.insert(absoluteOffset, u"label_%1"_s.arg(m_labels.size()));
4078 conversionCode += u" goto "_s + *labelIt + u";\n"_s;
4079 }
4080
4081 m_body += u"{\n"_s + conversionCode + u"}\n"_s;
4082}
4083
4084QString QQmlJSCodeGenerator::registerVariable(int index) const
4085{
4086 QQmlJSRegisterContent content = registerType(index);
4087 const auto it = m_registerVariables.constFind(content);
4088 if (it != m_registerVariables.constEnd())
4089 return it->variableName;
4090
4091 return QString();
4092}
4093
4094QString QQmlJSCodeGenerator::lookupVariable(int lookupIndex) const
4095{
4096 for (auto it = m_registerVariables.constBegin(), end = m_registerVariables.constEnd(); it != end; ++it) {
4097 if (it.key().resultLookupIndex() == lookupIndex)
4098 return it->variableName;
4099 }
4100 return QString();
4101}
4102
4103QString QQmlJSCodeGenerator::consumedRegisterVariable(int index) const
4104{
4105 const QString var = registerVariable(index);
4106 if (var.isEmpty() || !shouldMoveRegister(index))
4107 return var;
4108 return u"std::move(" + var + u")";
4109}
4110
4111QString QQmlJSCodeGenerator::consumedAccumulatorVariableIn() const
4112{
4113 return shouldMoveRegister(Accumulator)
4114 ? u"std::move(" + m_state.accumulatorVariableIn + u")"
4115 : m_state.accumulatorVariableIn;
4116}
4117
4118QString QQmlJSCodeGenerator::changedRegisterVariable() const
4119{
4120 QQmlJSRegisterContent changedRegister = m_state.changedRegister();
4121
4122 const QQmlJSScope::ConstPtr storedType = changedRegister.storedType();
4123 if (storedType.isNull())
4124 return QString();
4125
4126 return m_registerVariables.value(changedRegister).variableName;
4127}
4128
4129QQmlJSRegisterContent QQmlJSCodeGenerator::registerType(int index) const
4130{
4131 auto it = m_state.registers.find(index);
4132 if (it != m_state.registers.end())
4133 return it.value().content;
4134
4135 return QQmlJSRegisterContent();
4136}
4137
4138QQmlJSRegisterContent QQmlJSCodeGenerator::lookupType(int lookupIndex) const
4139{
4140 auto it = m_state.lookups.find(lookupIndex);
4141 if (it != m_state.lookups.end())
4142 return it.value().content;
4143
4144 return QQmlJSRegisterContent();
4145}
4146
4147bool QQmlJSCodeGenerator::shouldMoveRegister(int index) const
4148{
4149 return m_state.canMoveReadRegister(index)
4150 && !m_typeResolver->isTriviallyCopyable(m_state.readRegister(index).storedType());
4151}
4152
4153QString QQmlJSCodeGenerator::conversion(
4154 QQmlJSRegisterContent from, QQmlJSRegisterContent to, const QString &variable)
4155{
4156 const QQmlJSScope::ConstPtr contained = to.containedType();
4157
4158 // If from is QJSPrimitiveValue and to contains a primitive we coerce using QJSPrimitiveValue
4159 if (from.isStoredIn(m_typeResolver->jsPrimitiveType()) && m_typeResolver->isPrimitive(to)) {
4160
4161 QString primitive = [&]() -> QString {
4162 if (contained == m_typeResolver->jsPrimitiveType())
4163 return variable;
4164
4165 const QString conversion = variable + u".to<QJSPrimitiveValue::%1>()"_s;
4166 if (contained == m_typeResolver->boolType())
4167 return conversion.arg(u"Boolean"_s);
4168 if (m_typeResolver->isIntegral(to))
4169 return conversion.arg(u"Integer"_s);
4170 if (m_typeResolver->isNumeric(to))
4171 return conversion.arg(u"Double"_s);
4172 if (contained == m_typeResolver->stringType())
4173 return conversion.arg(u"String"_s);
4174 REJECT<QString>(
4175 u"Conversion of QJSPrimitiveValue to "_s + contained->internalName());
4176 }();
4177
4178 if (primitive.isEmpty())
4179 return primitive;
4180
4181 return convertStored(m_typeResolver->jsPrimitiveType(), to.storedType(), primitive);
4182 }
4183
4184 if (to.isStoredIn(contained)
4185 || m_typeResolver->isNumeric(to.storedType())
4186 || to.storedType()->isReferenceType()
4187 || from.contains(contained)) {
4188 // If:
4189 // * the output is not actually wrapped at all, or
4190 // * the output is stored in a numeric type (as there are no internals to a number), or
4191 // * the output is a QObject pointer, or
4192 // * we merely wrap the value into a new container,
4193 // we can convert by stored type.
4194 return convertStored(from.storedType(), to.storedType(), variable);
4195 } else {
4196 return convertContained(from, to, variable);
4197 }
4198}
4199
4200QString QQmlJSCodeGenerator::convertStored(
4201 const QQmlJSScope::ConstPtr &from, const QQmlJSScope::ConstPtr &to, const QString &variable)
4202{
4203 // TODO: most values can be moved, which is much more efficient with the common types.
4204 // add a move(from, to, variable) function that implements the moves.
4205 Q_ASSERT(!to->isComposite()); // We cannot directly convert to composites.
4206
4207 const auto jsValueType = m_typeResolver->jsValueType();
4208 const auto varType = m_typeResolver->varType();
4209 const auto jsPrimitiveType = m_typeResolver->jsPrimitiveType();
4210 const auto boolType = m_typeResolver->boolType();
4211
4212 auto zeroBoolOrInt = [&](const QQmlJSScope::ConstPtr &to) {
4213 if (to == boolType)
4214 return u"false"_s;
4215 if (m_typeResolver->isSignedInteger(to))
4216 return u"0"_s;
4217 if (m_typeResolver->isUnsignedInteger(to))
4218 return u"0u"_s;
4219 return QString();
4220 };
4221
4222 if (from == m_typeResolver->voidType()) {
4223 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
4224 return u"static_cast<"_s + to->internalName() + u" *>(nullptr)"_s;
4225 const QString zero = zeroBoolOrInt(to);
4226 if (!zero.isEmpty())
4227 return zero;
4228 if (to == m_typeResolver->floatType())
4229 return u"std::numeric_limits<float>::quiet_NaN()"_s;
4230 if (to == m_typeResolver->realType())
4231 return u"std::numeric_limits<double>::quiet_NaN()"_s;
4232 if (to == m_typeResolver->stringType())
4233 return QQmlJSUtils::toLiteral(u"undefined"_s);
4234 if (to == m_typeResolver->varType())
4235 return u"QVariant()"_s;
4236 if (to == m_typeResolver->jsValueType())
4237 return u"QJSValue();"_s;
4238 if (to == m_typeResolver->jsPrimitiveType())
4239 return u"QJSPrimitiveValue()"_s;
4240 if (from == to)
4241 return QString();
4242 }
4243
4244 if (from == m_typeResolver->nullType()) {
4245 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
4246 return u"static_cast<"_s + to->internalName() + u" *>(nullptr)"_s;
4247 if (to == jsValueType)
4248 return u"QJSValue(QJSValue::NullValue)"_s;
4249 if (to == jsPrimitiveType)
4250 return u"QJSPrimitiveValue(QJSPrimitiveNull())"_s;
4251 if (to == varType)
4252 return u"QVariant::fromValue<std::nullptr_t>(nullptr)"_s;
4253 const QString zero = zeroBoolOrInt(to);
4254 if (!zero.isEmpty())
4255 return zero;
4256 if (to == m_typeResolver->floatType())
4257 return u"0.0f"_s;
4258 if (to == m_typeResolver->realType())
4259 return u"0.0"_s;
4260 if (to == m_typeResolver->stringType())
4261 return QQmlJSUtils::toLiteral(u"null"_s);
4262 if (from == to)
4263 return QString();
4264 REJECT<QString>(u"Conversion from null to %1"_s.arg(to->internalName()));
4265 }
4266
4267 if (from == to)
4268 return variable;
4269
4270 if (from->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
4271 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference) {
4272 // Compare internalName here. The same C++ type can be exposed muliple times in
4273 // different QML types. However, the C++ names have to be unique. We can always
4274 // static_cast to those.
4275
4276 for (QQmlJSScope::ConstPtr base = from; base; base = base->baseType()) {
4277 // We still have to cast as other execution paths may result in different types.
4278 if (base->internalName() == to->internalName())
4279 return u"static_cast<"_s + to->internalName() + u" *>("_s + variable + u')';
4280 }
4281 for (QQmlJSScope::ConstPtr base = to; base; base = base->baseType()) {
4282 if (base->internalName() == from->internalName())
4283 return u"static_cast<"_s + to->internalName() + u" *>("_s + variable + u')';
4284 }
4285 } else if (to == m_typeResolver->boolType()) {
4286 return u'(' + variable + u" != nullptr)"_s;
4287 }
4288 }
4289
4290 auto isJsValue = [&](const QQmlJSScope::ConstPtr &candidate) {
4291 return candidate == jsValueType || candidate->isScript();
4292 };
4293
4294 if (isJsValue(from) && isJsValue(to))
4295 return variable;
4296
4297 const auto isBoolOrNumber = [&](const QQmlJSScope::ConstPtr &type) {
4298 return m_typeResolver->isNumeric(type)
4299 || type == m_typeResolver->boolType()
4300 || type->scopeType() == QQmlSA::ScopeType::EnumScope;
4301 };
4302
4303 if (from == m_typeResolver->realType() || from == m_typeResolver->floatType()) {
4304 if (to == m_typeResolver->int64Type() || to == m_typeResolver->uint64Type()) {
4305 return to->internalName() + u"(QJSNumberCoercion::roundTowards0("_s
4306 + variable + u"))"_s;
4307 }
4308
4309 if (m_typeResolver->isSignedInteger(to))
4310 return u"QJSNumberCoercion::toInteger("_s + variable + u')';
4311 if (m_typeResolver->isUnsignedInteger(to))
4312 return u"uint(QJSNumberCoercion::toInteger("_s + variable + u"))"_s;
4313 if (to == m_typeResolver->boolType())
4314 return u"[](double moved){ return moved && !std::isnan(moved); }("_s + variable + u')';
4315 }
4316
4317 if (isBoolOrNumber(from) && isBoolOrNumber(to))
4318 return to->internalName() + u'(' + variable + u')';
4319
4320
4321 if (from == jsPrimitiveType) {
4322 if (to == boolType)
4323 return variable + u".toBoolean()"_s;
4324 if (to == m_typeResolver->int64Type() || to == m_typeResolver->uint64Type())
4325 return u"%1(%2.toDouble())"_s.arg(to->internalName(), variable);
4326 if (m_typeResolver->isIntegral(to))
4327 return u"%1(%2.toInteger())"_s.arg(to->internalName(), variable);
4328 if (m_typeResolver->isNumeric(to))
4329 return u"%1(%2.toDouble())"_s.arg(to->internalName(), variable);
4330 if (to == m_typeResolver->stringType())
4331 return variable + u".toString()"_s;
4332 if (to == jsValueType)
4333 return u"QJSValue(QJSPrimitiveValue("_s + variable + u"))"_s;
4334 if (to == varType)
4335 return variable + u".toVariant()"_s;
4336 if (to->accessSemantics() == QQmlJSScope::AccessSemantics::Reference)
4337 return u"static_cast<"_s + to->internalName() + u" *>(nullptr)"_s;
4338 }
4339
4340 if (isJsValue(from)) {
4341 if (to == jsPrimitiveType)
4342 return variable + u".toPrimitive()"_s;
4343 if (to == varType)
4344 return variable + u".toVariant(QJSValue::RetainJSObjects)"_s;
4345 return u"qjsvalue_cast<"_s + castTargetName(to) + u">("_s + variable + u')';
4346 }
4347
4348 if (to == jsPrimitiveType) {
4349 // null and undefined have been handled above already
4350 Q_ASSERT(from != m_typeResolver->nullType());
4351 Q_ASSERT(from != m_typeResolver->voidType());
4352
4353 if (from == m_typeResolver->boolType()
4354 || from == m_typeResolver->int32Type()
4355 || from == m_typeResolver->realType()
4356 || from == m_typeResolver->stringType()) {
4357 return u"QJSPrimitiveValue("_s + variable + u')';
4358 } else if (from == m_typeResolver->int16Type()
4359 || from == m_typeResolver->int8Type()
4360 || from == m_typeResolver->uint16Type()
4361 || from == m_typeResolver->uint8Type()) {
4362 return u"QJSPrimitiveValue(int("_s + variable + u"))"_s;
4363 } else if (m_typeResolver->isNumeric(from)) {
4364 return u"QJSPrimitiveValue(double("_s + variable + u"))"_s;
4365 }
4366 }
4367
4368 if (to == jsValueType)
4369 return u"aotContext->engine->toScriptValue("_s + variable + u')';
4370
4371 if (from == varType) {
4372 if (to == m_typeResolver->listPropertyType())
4373 return u"QQmlListReference("_s + variable + u", aotContext->qmlEngine())"_s;
4374 return u"aotContext->engine->fromVariant<"_s + castTargetName(to) + u">("_s
4375 + variable + u')';
4376 }
4377
4378 if (to == varType)
4379 return u"QVariant::fromValue("_s + variable + u')';
4380
4381 if (from == m_typeResolver->urlType() && to == m_typeResolver->stringType())
4382 return variable + u".toString()"_s;
4383
4384 if (from == m_typeResolver->stringType() && to == m_typeResolver->urlType())
4385 return u"QUrl("_s + variable + u')';
4386
4387 if (from == m_typeResolver->byteArrayType() && to == m_typeResolver->stringType())
4388 return u"QString::fromUtf8("_s + variable + u')';
4389
4390 if (from == m_typeResolver->stringType() && to == m_typeResolver->byteArrayType())
4391 return variable + u".toUtf8()"_s;
4392
4393 for (const auto &originType : {
4394 m_typeResolver->dateTimeType(),
4395 m_typeResolver->dateType(),
4396 m_typeResolver->timeType()}) {
4397 if (from == originType) {
4398 for (const auto &targetType : {
4399 m_typeResolver->dateTimeType(),
4400 m_typeResolver->dateType(),
4401 m_typeResolver->timeType(),
4402 m_typeResolver->stringType(),
4403 m_typeResolver->realType()}) {
4404 if (to == targetType) {
4405 return u"aotContext->engine->coerceValue<%1, %2>(%3)"_s.arg(
4406 originType->internalName(), targetType->internalName(), variable);
4407 }
4408 }
4409 break;
4410 }
4411 }
4412
4413 const auto retrieveFromPrimitive = [&](
4414 const QQmlJSScope::ConstPtr &type, const QString &expression) -> QString
4415 {
4416 if (type == m_typeResolver->boolType())
4417 return expression + u".toBoolean()"_s;
4418 if (m_typeResolver->isSignedInteger(type))
4419 return expression + u".toInteger()"_s;
4420 if (m_typeResolver->isUnsignedInteger(type))
4421 return u"uint("_s + expression + u".toInteger())"_s;
4422 if (type == m_typeResolver->realType())
4423 return expression + u".toDouble()"_s;
4424 if (type == m_typeResolver->floatType())
4425 return u"float("_s + expression + u".toDouble())"_s;
4426 if (type == m_typeResolver->stringType())
4427 return expression + u".toString()"_s;
4428 return QString();
4429 };
4430
4431 if (!retrieveFromPrimitive(from, u"x"_s).isEmpty()) {
4432 const QString retrieve = retrieveFromPrimitive(
4433 to, convertStored(from, m_typeResolver->jsPrimitiveType(), variable));
4434 if (!retrieve.isEmpty())
4435 return retrieve;
4436 }
4437
4438 if (from->isReferenceType() && to == m_typeResolver->stringType()) {
4439 return u"aotContext->engine->coerceValue<"_s + castTargetName(from) + u", "
4440 + castTargetName(to) + u">("_s + variable + u')';
4441 }
4442
4443 // Any value type is a non-null JS 'object' and therefore coerces to true.
4444 if (to == m_typeResolver->boolType()) {
4445 // All the interesting cases are already handled above:
4446 Q_ASSERT(from != m_typeResolver->nullType());
4447 Q_ASSERT(from != m_typeResolver->voidType());
4448 Q_ASSERT(retrieveFromPrimitive(from, u"x"_s).isEmpty());
4449 Q_ASSERT(!isBoolOrNumber(from));
4450
4451 return u"true"_s;
4452 }
4453
4454 if (m_typeResolver->areEquivalentLists(from, to))
4455 return variable;
4456
4457 if (from->isListProperty()
4458 && to->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence
4459 && to->elementType()->isReferenceType()
4460 && !to->isListProperty()) {
4461 return variable + u".toList<"_s + to->internalName() + u">()"_s;
4462 }
4463
4464 bool isExtension = false;
4465 if (m_typeResolver->canPopulate(to, from, &isExtension)) {
4466 REJECT<QString>(
4467 u"populating "_s + to->internalName() + u" from "_s + from->internalName());
4468 } else if (const auto ctor = m_typeResolver->selectConstructor(to, from, &isExtension);
4469 ctor.isValid()) {
4470 const auto argumentTypes = ctor.parameters();
4471 const QString argument = convertStored(from, argumentTypes[0].type(), variable);
4472 if (isExtension) {
4473 // We typically use private inheritance for the foreign/extension trick.
4474 // Therefore we need to jump through some hoops to extract the actual value here.
4475 return u"[](auto &&arg) { "
4476 "%1 wrapper(std::forward<decltype(arg)>(arg)); "
4477 "return %2(reinterpret_cast<%2 &>(wrapper)); "
4478 "}(%3)"_s.arg(
4479 to->extensionType().scope->internalName(), to->internalName(), argument);
4480 }
4481
4482 return u"%1(%2)"_s.arg(to->internalName(), argument);
4483 }
4484
4485 if (to == m_typeResolver->stringType()
4486 && from->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence) {
4487 addInclude(u"QtQml/qjslist.h"_s);
4488
4489 // Extend the life time of whatever variable is across the call to toString().
4490 // variable may be an rvalue.
4491 return u"[&](auto &&l){ return QJSList(&l, aotContext->engine).toString(); }("_s
4492 + variable + u')';
4493 }
4494
4495 // TODO: add more conversions
4496
4497 REJECT<QString>(
4498 u"conversion from "_s + from->internalName() + u" to "_s + to->internalName());
4499}
4500
4501QString QQmlJSCodeGenerator::convertContained(QQmlJSRegisterContent from, QQmlJSRegisterContent to, const QString &variable)
4502{
4503 const QQmlJSScope::ConstPtr containedFrom = from.containedType();
4504 const QQmlJSScope::ConstPtr containedTo = to.containedType();
4505
4506 // Those should be handled before, by convertStored().
4507 Q_ASSERT(!to.storedType()->isReferenceType());
4508 Q_ASSERT(!to.isStoredIn(containedTo));
4509 Q_ASSERT(containedFrom != containedTo);
4510
4511 if (!to.isStoredIn(m_typeResolver->varType())
4512 && !to.isStoredIn(m_typeResolver->jsPrimitiveType())) {
4513 REJECT<QString>(u"internal conversion into unsupported wrapper type."_s);
4514 }
4515
4516 bool isExtension = false;
4517 if (m_typeResolver->canPopulate(containedTo, containedFrom, &isExtension)) {
4518 REJECT<QString>(u"populating "_s + containedTo->internalName()
4519 + u" from "_s + containedFrom->internalName());
4520 } else if (const auto ctor = m_typeResolver->selectConstructor(
4521 containedTo, containedFrom, &isExtension); ctor.isValid()) {
4522 return generateCallConstructor(
4523 ctor, {from}, {variable}, metaType(containedTo),
4524 metaObject(isExtension ? containedTo->extensionType().scope : containedTo));
4525 }
4526
4527 const auto originalFrom = originalType(from);
4528 const auto containedOriginalFrom = originalFrom.containedType();
4529 if (containedFrom != containedOriginalFrom
4530 && m_typeResolver->canHold(containedFrom, containedOriginalFrom)) {
4531 // If from is simply a wrapping of a specific type into a more general one, we can convert
4532 // the original type instead. You can't nest wrappings after all.
4533 return conversion(m_pool->storedIn(originalFrom, from.storedType()), to, variable);
4534 }
4535
4536 if (m_typeResolver->isPrimitive(containedFrom) && m_typeResolver->isPrimitive(containedTo)) {
4537 const QQmlJSRegisterContent intermediate
4538 = m_pool->storedIn(from, m_typeResolver->jsPrimitiveType());
4539 return conversion(intermediate, to, conversion(from, intermediate, variable));
4540 }
4541
4542 REJECT<QString>(
4543 u"internal conversion with incompatible or ambiguous types: %1 -> %2"_s
4544 .arg(from.descriptiveName(), to.descriptiveName()));
4545}
4546
4547void QQmlJSCodeGenerator::reject(const QString &thing)
4548{
4549 addError(u"Cannot generate efficient code for %1"_s.arg(thing));
4550}
4551
4552
4553void QQmlJSCodeGenerator::skip(const QString &thing)
4554{
4555 addSkip(u"Skipped code generation for %1"_s.arg(thing));
4556}
4557
4558QQmlJSCodeGenerator::AccumulatorConverter::AccumulatorConverter(QQmlJSCodeGenerator *generator)
4559 : accumulatorOut(generator->m_state.accumulatorOut())
4560 , accumulatorVariableIn(generator->m_state.accumulatorVariableIn)
4561 , accumulatorVariableOut(generator->m_state.accumulatorVariableOut)
4562 , generator(generator)
4563{
4564 if (accumulatorVariableOut.isEmpty())
4565 return;
4566
4567 const QQmlJSTypeResolver *resolver = generator->m_typeResolver;
4568 const QQmlJSScope::ConstPtr origContained = resolver->originalContainedType(accumulatorOut);
4569 const QQmlJSRegisterContent storage = accumulatorOut.storage();
4570 const QQmlJSScope::ConstPtr stored = storage.containedType();
4571 const QQmlJSScope::ConstPtr origStored = resolver->original(storage).containedType();
4572
4573
4574 // If the stored type differs or if we store in QVariant and the contained type differs,
4575 // then we have to use a temporary ...
4576 if (origStored != stored
4577 || (origContained != accumulatorOut.containedType() && stored == resolver->varType())) {
4578
4579 const bool storable = isTypeStorable(resolver, origStored);
4580 generator->m_state.accumulatorVariableOut = storable ? u"retrieved"_s : QString();
4581 generator->m_state.setRegister(Accumulator, generator->originalType(accumulatorOut));
4582 generator->m_body += u"{\n"_s;
4583 if (storable) {
4584 generator->m_body += origStored->augmentedInternalName() + u' '
4585 + generator->m_state.accumulatorVariableOut + u";\n";
4586 }
4587 } else if (generator->m_state.accumulatorVariableIn == generator->m_state.accumulatorVariableOut
4588 && generator->m_state.readsRegister(Accumulator)
4589 && generator->m_state.accumulatorOut().isStoredIn(resolver->varType())) {
4590 // If both m_state.accumulatorIn and m_state.accumulatorOut are QVariant, we will need to
4591 // prepare the output QVariant, and afterwards use the input variant. Therefore we need to
4592 // move the input out of the way first.
4593 generator->m_state.accumulatorVariableIn
4594 = generator->m_state.accumulatorVariableIn + u"_moved"_s;
4595 generator->m_body += u"{\n"_s;
4596 generator->m_body += u"QVariant "_s + generator->m_state.accumulatorVariableIn
4597 + u" = std::move("_s + generator->m_state.accumulatorVariableOut + u");\n"_s;
4598 }
4599}
4600
4601QQmlJSCodeGenerator::AccumulatorConverter::~AccumulatorConverter()
4602{
4603 if (accumulatorVariableOut != generator->m_state.accumulatorVariableOut) {
4604 generator->m_body += accumulatorVariableOut + u" = "_s + generator->conversion(
4605 generator->m_state.accumulatorOut(), accumulatorOut,
4606 u"std::move("_s + generator->m_state.accumulatorVariableOut + u')') + u";\n"_s;
4607 generator->m_body += u"}\n"_s;
4608 generator->m_state.setRegister(Accumulator, accumulatorOut);
4609 generator->m_state.accumulatorVariableOut = accumulatorVariableOut;
4610 } else if (accumulatorVariableIn != generator->m_state.accumulatorVariableIn) {
4611 generator->m_body += u"}\n"_s;
4612 generator->m_state.accumulatorVariableIn = accumulatorVariableIn;
4613 }
4614}
4615
4616
4617QT_END_NAMESPACE
4618
4619QQmlJSCodeGenerator::GeneratePragmaWarningBlock::GeneratePragmaWarningBlock(QQmlJSCodeGenerator *generator)
4620 : m_generator(generator)
4621{
4622 m_generator->m_body += u"QT_WARNING_PUSH\n"_s;
4623}
4624
4625QQmlJSCodeGenerator::GeneratePragmaWarningBlock::~GeneratePragmaWarningBlock()
4626{
4627 m_generator->m_body += u"QT_WARNING_POP\n"_s;
4628}
4629
4630void QQmlJSCodeGenerator::GeneratePragmaWarningBlock::silenceDivideByZero()
4631{
4632 // currently only needed with MSVC, if we need it on more compilers, we
4633 // should add a proper macro in Qt itself
4634 m_generator->m_body += u"QT_WARNING_DISABLE_MSVC(4723) // potential divide by 0\n"_s;
4635}
4636
4637void QQmlJSCodeGenerator::recordPropertyLookup(const QQmlJSScope::ConstPtr &base,
4638 const QQmlJSMetaProperty &property)
4639{
4640 if (!m_noAotValidation)
4641 m_lookupSignaturesRecorder.recordPropertyLookup(base, property);
4642}
4643
4644void QQmlJSCodeGenerator::recordMethodLookup(const QQmlJSScope::ConstPtr &base,
4645 const QQmlJSMetaMethod &method)
4646{
4647 if (!m_noAotValidation)
4648 m_lookupSignaturesRecorder.recordMethodLookup(base, method);
4649}
4650
4651void QQmlJSCodeGenerator::recordEnumKeyLookup(const QQmlJSScope::ConstPtr &base,
4652 const QQmlJSMetaEnum &metaEnum,
4653 const QString &keyName)
4654{
4655 if (!m_noAotValidation)
4656 m_lookupSignaturesRecorder.recordEnumKeyLookup(base, metaEnum, keyName);
4657}
\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)