aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmltc/qmltccompilerpieces.h
blob: 140a30745d88680b729327768b04f919e1e9844f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef QMLTCCOMPILERPIECES_H
#define QMLTCCOMPILERPIECES_H

#include <QtCore/qscopeguard.h>
#include <QtCore/qstringbuilder.h>
#include <QtCore/qfileinfo.h>

#include <private/qqmljsutils_p.h>

#include "qmltcoutputir.h"
#include "qmltcvisitor.h"

QT_BEGIN_NAMESPACE

/*!
    \internal

    Helper class that generates code for the output IR. Takes care of
    complicated, repetitive, nasty logic which is better kept in a single
    confined place.
*/
struct QmltcCodeGenerator
{
    static const QString privateEngineName;
    static const QString typeCountName;

    QString documentUrl;
    QmltcVisitor *visitor = nullptr;

    [[nodiscard]] inline decltype(auto) generate_initCode(QmltcType &current,
                                                          const QQmlJSScope::ConstPtr &type) const;
    inline void generate_initCodeForTopLevelComponent(QmltcType &current,
                                                      const QQmlJSScope::ConstPtr &type);

    inline void generate_qmltcInstructionCallCode(QmltcMethod *function,
                                                  const QQmlJSScope::ConstPtr &type,
                                                  const QString &baseInstructionArgs,
                                                  const QString &childInstructionArgs) const;
    inline void generate_endInitCode(QmltcType &current, const QQmlJSScope::ConstPtr &type) const;
    inline void generate_setComplexBindingsCode(QmltcType &current,
                                                const QQmlJSScope::ConstPtr &type) const;

    inline void generate_interfaceCallCode(QmltcMethod *function, const QQmlJSScope::ConstPtr &type,
                                           const QString &interfaceName,
                                           const QString &interfaceCall) const;
    inline void generate_beginClassCode(QmltcType &current,
                                        const QQmlJSScope::ConstPtr &type) const;
    inline void generate_completeComponentCode(QmltcType &current,
                                               const QQmlJSScope::ConstPtr &type) const;
    inline void generate_finalizeComponentCode(QmltcType &current,
                                               const QQmlJSScope::ConstPtr &type) const;
    inline void generate_handleOnCompletedCode(QmltcType &current,
                                               const QQmlJSScope::ConstPtr &type) const;

    static void generate_assignToProperty(QStringList *block, const QQmlJSScope::ConstPtr &type,
                                          const QQmlJSMetaProperty &p, const QString &value,
                                          const QString &accessor,
                                          bool constructFromQObject = false);
    static void generate_setIdValue(QStringList *block, const QString &context, qsizetype index,
                                    const QString &accessor, const QString &idString);

    inline QString generate_typeCount() const
    {
        return generate_typeCount([](const QQmlJSScope::ConstPtr &) { return false; });
    }
    template<typename Predicate>
    inline QString generate_typeCount(Predicate p) const;

    static void generate_callExecuteRuntimeFunction(QStringList *block, const QString &url,
                                                    QQmlJSMetaMethod::AbsoluteFunctionIndex index,
                                                    const QString &accessor,
                                                    const QString &returnType,
                                                    const QList<QmltcVariable> &parameters = {});

    static void generate_createBindingOnProperty(QStringList *block, const QString &unitVarName,
                                                 const QString &scope, qsizetype functionIndex,
                                                 const QString &target, int propertyIndex,
                                                 const QQmlJSMetaProperty &p, int valueTypeIndex,
                                                 const QString &subTarget);

    static inline void generate_getCompilationUnitFromUrl();

    struct PreparedValue
    {
        QStringList prologue;
        QString value;
        QStringList epilogue;
    };

    static PreparedValue wrap_mismatchingTypeConversion(const QQmlJSMetaProperty &p, QString value);
    static PreparedValue wrap_extensionType(const QQmlJSScope::ConstPtr &type,
                                            const QQmlJSMetaProperty &p, const QString &accessor);

    static QString wrap_privateClass(const QString &accessor, const QQmlJSMetaProperty &p);
    static QString wrap_qOverload(const QList<QmltcVariable> &parameters,
                                  const QString &overloaded);
    static QString wrap_addressof(const QString &addressed);

    QString urlMethodName() const
    {
        using namespace Qt::StringLiterals;
        QFileInfo fi(documentUrl);
        return u"q_qmltc_docUrl_" + fi.fileName().replace(u".qml"_s, u""_s).replace(u'.', u'_');
    }
};

/*!
    \internal

    Generates \a{current.init}'s code. The init method sets up a QQmlContext for
    the object and (in case \a type is a document root) calls other object
    creation methods in a well-defined order:
    1. current.beginClass
    2. current.endInit
    3. current.completeComponent
    4. current.finalizeComponent
    5. current.handleOnCompleted

    This function returns a QScopeGuard with the final instructions that have to
    be generated at a later point, once everything else is compiled.

    \sa generate_initCodeForTopLevelComponent
*/
inline decltype(auto) QmltcCodeGenerator::generate_initCode(QmltcType &current,
                                                            const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;

    // qmltc_init()'s parameters:
    // * QQmltcObjectCreationHelper* creator
    // * QQmlEngine* engine
    // * const QQmlRefPointer<QQmlContextData>& parentContext
    // * bool canFinalize [optional, when document root]
    const bool isDocumentRoot = type == visitor->result();

    current.init.body << u"Q_UNUSED(creator);"_s; // can happen sometimes

    current.init.body << u"auto context = parentContext;"_s;

    // if parent scope has a QML base type and is not a (current) document root,
    // the parentContext we passed as input to this object is a context of
    // another document. we need to fix it by using parentContext->parent()

    const auto realQmlScope = [](const QQmlJSScope::ConstPtr &scope) {
        if (scope->isArrayScope())
            return scope->parentScope();
        return scope;
    };

    if (auto parentScope = realQmlScope(type->parentScope());
        parentScope != visitor->result() && QQmlJSUtils::hasCompositeBase(parentScope)) {
        current.init.body << u"// NB: context->parent() is the context of this document"_s;
        current.init.body << u"context = context->parent();"_s;
    }

    // any object with QML base class has to call base's init method
    if (auto base = type->baseType(); base->isComposite()) {
        QString lhs;
        // init creates new context. for document root, it's going to be a real
        // parent context, so store it temporarily in `context` variable
        if (isDocumentRoot)
            lhs = u"context = "_s;
        current.init.body << u"// 0. call base's init method"_s;

        Q_ASSERT(!isDocumentRoot || visitor->qmlTypesWithQmlBases()[0] == visitor->result());
        const auto isCurrentType = [&](const QQmlJSScope::ConstPtr &qmlType) {
            return qmlType == type;
        };
        const QString creationOffset = generate_typeCount(isCurrentType);

        current.init.body << u"{"_s;
        current.init.body << u"QQmltcObjectCreationHelper subCreator(creator, %1);"_s.arg(
                creationOffset);
        current.init.body
                << QStringLiteral("%1%2::%3(&subCreator, engine, context, /* finalize */ false);")
                           .arg(lhs, base->internalName(), current.init.name);
        current.init.body << u"}"_s;
    }

    current.init.body
            << QStringLiteral("auto %1 = QQmlEnginePrivate::get(engine);").arg(privateEngineName);
    current.init.body << QStringLiteral("Q_UNUSED(%1);").arg(privateEngineName); // precaution

    // when generating root, we need to create a new (document-level) context.
    // otherwise, just use existing context as is
    if (isDocumentRoot) {
        current.init.body << u"// 1. create new QML context for this document"_s;
        // TODO: the last 2 parameters are {0, true} because we deal with
        // document root only here. in reality, there are inline components
        // which need { index, true } instead
        current.init.body
                << QStringLiteral(
                           "context = %1->createInternalContext(%1->compilationUnitFromUrl(%2()), "
                           "context, 0, true);")
                           .arg(privateEngineName, urlMethodName());
    } else {
        current.init.body << u"// 1. use current context as this object's context"_s;
        current.init.body << u"// context = context;"_s;
    }

    if (!type->baseType()->isComposite() || isDocumentRoot) {
        current.init.body << u"// 2. set context for this object"_s;
        current.init.body << QStringLiteral(
                                     "%1->setInternalContext(this, context, QQmlContextData::%2);")
                                     .arg(privateEngineName,
                                          (isDocumentRoot ? u"DocumentRoot"_s
                                                          : u"OrdinaryObject"_s));
        if (isDocumentRoot)
            current.init.body << u"context->setContextObject(this);"_s;
    }

    // context is this document's context. we must remember it in each type
    current.variables.emplaceBack(u"QQmlRefPointer<QQmlContextData>"_s, u"q_qmltc_thisContext"_s,
                                  u"nullptr"_s);
    current.init.body << u"%1::q_qmltc_thisContext = context;"_s.arg(type->internalName());

    if (int id = visitor->runtimeId(type); id >= 0) {
        current.init.body << u"// 3. set id since it is provided"_s;
        QString idString = visitor->addressableScopes().id(type);
        if (idString.isEmpty())
            idString = u"<unknown>"_s;
        QmltcCodeGenerator::generate_setIdValue(&current.init.body, u"context"_s, id, u"this"_s,
                                                idString);
    }

    // if type has an extension, create a dynamic meta object for it
    bool hasExtension = false;
    for (auto cppBase = QQmlJSScope::nonCompositeBaseType(type); cppBase;
         cppBase = cppBase->baseType()) {
        // QObject is special: we have a pseudo-extension on it due to builtins
        if (cppBase->internalName() == u"QObject"_s)
            break;
        if (cppBase->extensionType().extensionSpecifier != QQmlJSScope::NotExtension) {
            hasExtension = true;
            break;
        }
    }
    if (hasExtension) {
        current.init.body << u"{"_s;
        current.init.body << u"auto cppData = QmltcTypeData(this);"_s;
        current.init.body << u"qmltcCreateDynamicMetaObject(this, cppData);"_s;
        current.init.body << u"}"_s;
    }

    const auto generateFinalLines = [&current, isDocumentRoot]() {
        if (isDocumentRoot) {
            current.init.body << u"// 4. finish the document root creation"_s;
            current.init.body << u"if (canFinalize) {"_s;
            current.init.body << QStringLiteral("    %1(creator, /* finalize */ true);")
                                         .arg(current.beginClass.name);
            current.init.body << QStringLiteral("    %1(creator, engine);")
                                         .arg(current.endInit.name);
            current.init.body << QStringLiteral("    %1(creator, engine);")
                                         .arg(current.setComplexBindings.name);
            current.init.body << QStringLiteral("    %1(creator, /* finalize */ true);")
                                         .arg(current.completeComponent.name);
            current.init.body << QStringLiteral("    %1(creator, /* finalize */ true);")
                                         .arg(current.finalizeComponent.name);
            current.init.body << QStringLiteral("    %1(creator);")
                                         .arg(current.handleOnCompleted.name);
            current.init.body << u"}"_s;
        }
        current.init.body << u"return context;"_s;
    };

    return QScopeGuard(generateFinalLines);
}

/*!
    \internal

    Generates \a{current.init}'s code in case when \a type is a top-level
    Component type. The init method in this case mimics
    QQmlObjectCreator::createComponent() logic.

    \sa generate_initCode
*/
inline void
QmltcCodeGenerator::generate_initCodeForTopLevelComponent(QmltcType &current,
                                                          const QQmlJSScope::ConstPtr &type)
{
    Q_UNUSED(type);

    using namespace Qt::StringLiterals;

    // since we create a document root as QQmlComponent, we only need to fake
    // QQmlComponent construction in init:
    current.init.body << u"// init QQmlComponent: see QQmlObjectCreator::createComponent()"_s;
    current.init.body << u"{"_s;
    // we already called QQmlComponent(parent) constructor. now we need:
    // 1. QQmlComponent(engine, parent) logic:
    current.init.body << u"// QQmlComponent(engine, parent):"_s;
    current.init.body << u"auto d = QQmlComponentPrivate::get(this);"_s;
    current.init.body << u"Q_ASSERT(d);"_s;
    current.init.body << u"d->engine = engine;"_s;
    current.init.body << u"QObject::connect(engine, &QObject::destroyed, this, [d]() {"_s;
    current.init.body << u"    d->state.creator.reset();"_s;
    current.init.body << u"    d->engine = nullptr;"_s;
    current.init.body << u"});"_s;
    // 2. QQmlComponent(engine, compilationUnit, start, parent) logic:
    current.init.body << u"// QQmlComponent(engine, compilationUnit, start, parent):"_s;
    current.init.body
            << u"auto compilationUnit = QQmlEnginePrivate::get(engine)->compilationUnitFromUrl("
                    + QmltcCodeGenerator::urlMethodName() + u"());";
    current.init.body << u"d->compilationUnit = compilationUnit;"_s;
    current.init.body << u"d->start = 0;"_s;
    current.init.body << u"d->url = compilationUnit->finalUrl();"_s;
    current.init.body << u"d->progress = 1.0;"_s;
    // 3. QQmlObjectCreator::createComponent() logic which is left:
    current.init.body << u"// QQmlObjectCreator::createComponent():"_s;
    current.init.body << u"d->creationContext = context;"_s;
    current.init.body << u"Q_ASSERT(QQmlData::get(this, /*create*/ false));"_s;
    current.init.body << u"}"_s;
}

/*!
    \internal

    A generic helper function that generates special qmltc instruction code
    boilerplate, adding it to a passed \a function. This is a building block
    used to generate e.g. QML_endInit code.
*/
inline void QmltcCodeGenerator::generate_qmltcInstructionCallCode(
        QmltcMethod *function, const QQmlJSScope::ConstPtr &type,
        const QString &baseInstructionArgs, const QString &childInstructionArgs) const
{
    using namespace Qt::StringLiterals;

    const bool isDocumentRoot = type == visitor->result();
    if (auto base = type->baseType(); base->isComposite()) {
        function->body << u"// call base's method"_s;
        Q_ASSERT(!isDocumentRoot || visitor->qmlTypesWithQmlBases()[0] == visitor->result());
        const auto isCurrentType = [&](const QQmlJSScope::ConstPtr &qmlType) {
            return qmlType == type;
        };
        const QString creationOffset = generate_typeCount(isCurrentType);
        function->body << u"{"_s;
        function->body << u"QQmltcObjectCreationHelper subCreator(creator, %1);"_s.arg(
                creationOffset);
        if (!baseInstructionArgs.isEmpty()) {
            function->body << u"%1::%2(&subCreator, %3);"_s.arg(
                    base->internalName(), function->name, baseInstructionArgs);
        } else {
            function->body << u"%1::%2(&subCreator);"_s.arg(base->internalName(), function->name);
        }
        function->body << u"}"_s;
    }

    if (!isDocumentRoot) // document root does all the work here
        return;

    const auto types = visitor->pureQmlTypes();
    function->body << u"// call children's methods"_s;
    for (qsizetype i = 1; i < types.size(); ++i) {
        const auto &type = types[i];
        Q_ASSERT(!type->isComponentRootElement());
        function->body << u"creator->get<%1>(%2)->%3(%4);"_s.arg(
                type->internalName(), QString::number(i), function->name, childInstructionArgs);
    }
    function->body << u"// call own method code"_s;
}

/*!
    \internal

    Generates \a{current.endInit}'s code. The endInit method creates bindings,
    connects signals with slots and generally performs other within-object
    initialization. Additionally, the QML document root's endInit calls endInit
    methods of all the necessary QML types within the document.
*/
inline void QmltcCodeGenerator::generate_endInitCode(QmltcType &current,
                                                     const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;

    // QML_endInit()'s parameters:
    // * QQmltcObjectCreationHelper* creator
    // * QQmlEngine* engine
    current.endInit.body << u"Q_UNUSED(creator);"_s;
    current.endInit.body << u"Q_UNUSED(engine);"_s;

    generate_qmltcInstructionCallCode(&current.endInit, type, u"engine"_s, u"creator, engine"_s);

    if (visitor->hasDeferredBindings(type)) {
        current.endInit.body << u"{ // defer bindings"_s;
        current.endInit.body << u"auto ddata = QQmlData::get(this);"_s;
        current.endInit.body << u"auto thisContext = ddata->outerContext;"_s;
        current.endInit.body << u"Q_ASSERT(thisContext);"_s;
        current.endInit.body << QStringLiteral("ddata->deferData(%1, "
                                               "QQmlEnginePrivate::get(engine)->"
                                               "compilationUnitFromUrl(%2()), thisContext);")
                                        .arg(QString::number(visitor->qmlIrObjectIndex(type)),
                                             QmltcCodeGenerator::urlMethodName());
        current.endInit.body << u"}"_s;
    }
}

/*!
    \internal

    Generates \a{current.setComplexBindings}'s code. The setComplexBindings
    method creates complex bindings (such as script bindings). Additionally, the
    QML document root's setComplexBindings calls setComplexBindings methods of
    all the necessary QML types within the document.
*/
inline void
QmltcCodeGenerator::generate_setComplexBindingsCode(QmltcType &current,
                                                    const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;

    // QML_setComplexBindings()'s parameters:
    // * QQmltcObjectCreationHelper* creator
    // * QQmlEngine* engine
    current.setComplexBindings.body << u"Q_UNUSED(creator);"_s;
    current.setComplexBindings.body << u"Q_UNUSED(engine);"_s;

    generate_qmltcInstructionCallCode(&current.setComplexBindings, type, u"engine"_s,
                                      u"creator, engine"_s);
}

/*!
    \internal

    A generic helper function that generates interface code boilerplate, adding
    it to a passed \a function. This is a building block used to generate e.g.
    QQmlParserStatus API calls.
*/
inline void QmltcCodeGenerator::generate_interfaceCallCode(QmltcMethod *function,
                                                           const QQmlJSScope::ConstPtr &type,
                                                           const QString &interfaceName,
                                                           const QString &interfaceCall) const
{
    using namespace Qt::StringLiterals;

    // function's parameters:
    // * QQmltcObjectCreationHelper* creator
    // * bool canFinalize [optional, when document root]
    const bool isDocumentRoot = type == visitor->result();
    function->body << u"Q_UNUSED(creator);"_s;
    if (isDocumentRoot)
        function->body << u"Q_UNUSED(canFinalize);"_s;

    if (auto base = type->baseType(); base->isComposite()) {
        function->body << u"// call base's method"_s;
        Q_ASSERT(!isDocumentRoot || visitor->qmlTypesWithQmlBases()[0] == visitor->result());
        const auto isCurrentType = [&](const QQmlJSScope::ConstPtr &qmlType) {
            return qmlType == type;
        };
        const QString creationOffset = generate_typeCount(isCurrentType);
        function->body << u"{"_s;
        function->body << u"QQmltcObjectCreationHelper subCreator(creator, %1);"_s.arg(
                creationOffset);
        function->body << u"%1::%2(&subCreator, /* finalize */ false);"_s.arg(base->internalName(),
                                                                              function->name);
        function->body << u"}"_s;
    }

    if (!isDocumentRoot)
        return;

    const auto types = visitor->pureQmlTypes();
    function->body << u"// call children's methods"_s;
    for (qsizetype i = 1; i < types.size(); ++i) {
        const auto &type = types[i];
        Q_ASSERT(!type->isComponentRootElement());
        function->body << u"{"_s;
        function->body << u"auto child = creator->get<%1>(%2);"_s.arg(type->internalName(),
                                                                      QString::number(i));
        function->body << u"child->%1(creator);"_s.arg(function->name);
        if (type->hasInterface(interfaceName)) {
            function->body << u"Q_ASSERT(dynamic_cast<%1 *>(child) != nullptr);"_s.arg(
                    interfaceName);
            function->body << u"child->%1();"_s.arg(interfaceCall);
        }
        function->body << u"}"_s;
    }

    if (type->hasInterface(interfaceName)) {
        function->body << u"if (canFinalize) {"_s;
        function->body << u"    // call own method"_s;
        function->body << u"    this->%1();"_s.arg(interfaceCall);
        function->body << u"}"_s;
    }
}

/*!
    \internal

    Generates \a{current.beginClass}'s code. The beginClass method optionally
    calls QQmlParserStatus::classBegin() when \a type implements the
    corresponding interface.
*/
inline void QmltcCodeGenerator::generate_beginClassCode(QmltcType &current,
                                                        const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;
    generate_interfaceCallCode(&current.beginClass, type, u"QQmlParserStatus"_s, u"classBegin"_s);
}

/*!
    \internal

    Generates \a{current.completeComponent}'s code. The completeComponent method
    optionally calls QQmlParserStatus::componentComplete() when \a type
    implements the corresponding interface.
*/
inline void
QmltcCodeGenerator::generate_completeComponentCode(QmltcType &current,
                                                   const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;
    generate_interfaceCallCode(&current.completeComponent, type, u"QQmlParserStatus"_s,
                               u"componentComplete"_s);
}

/*!
    \internal

    Generates \a{current.finalizeComponent}'s code. The finalizeComponent method
    optionally calls QQmlFinalizerHook::componentFinalized() when \a type
    implements the corresponding interface.
*/
inline void
QmltcCodeGenerator::generate_finalizeComponentCode(QmltcType &current,
                                                   const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;
    generate_interfaceCallCode(&current.finalizeComponent, type, u"QQmlFinalizerHook"_s,
                               u"componentFinalized"_s);
}

/*!
    \internal

    Generates \a{current.handleOnCompleted}'s code. The handleOnCompleted method
    optionally calls a Component.onCompleted handler if that is present in \a
    type.
*/
inline void
QmltcCodeGenerator::generate_handleOnCompletedCode(QmltcType &current,
                                                   const QQmlJSScope::ConstPtr &type) const
{
    using namespace Qt::StringLiterals;

    // QML_handleOnCompleted()'s parameters:
    // * QQmltcObjectCreationHelper* creator
    current.handleOnCompleted.body << u"Q_UNUSED(creator);"_s;

    generate_qmltcInstructionCallCode(&current.handleOnCompleted, type, QString(), u"creator"_s);
}

/*!
    \internal

    Generates a constexpr function consisting of a sum of type counts for a
    current QML document. Predicate \a p acts as a stop condition to prematurely
    end the sum generation.

    The high-level idea:

    Each qmltc-compiled document root has a notion of type count. Type count is
    a number of types the current QML document contains (except for
    Component-wrapped types) plus the sum of all type counts of all the QML
    documents used in the current document: if current document has a type with
    QML base type, this type's type count is added to the type count of the
    current document.

    To be able to lookup created objects during the creation process, one needs
    to know an index of each object within the document + an offset of the
    document. Index comes from QmltcVisitor and is basically a serial number of
    a type in the document (index < type count of the document root type). The
    offset is more indirect.

    The current document always starts with an offset of 0, each type that has a
    QML base type also "has a sub-document". Each sub-document has a non-0
    offset X, where X is calculated as a sum of the current document's type
    count and a cumulative type count of all the previous sub-documents that
    appear before the sub-document of interest:

    \code
    // A.qml
    Item {  // offset: 0; number of types == 1 (document root) + 3 (children)

        QmlBase1 { } // offset: 4 (number of types in A.qml itself)

        QmlBase2 { } // offset: 4 + N, where N == typeCount(QmlBase1.qml)

        QmlBase3 { } // offset: (4 + N) + M, where M == typeCount(QmlBase2.qml)

    } // typeCount(A.qml) == 4 + N + M + O, where O == typeCount(QmlBase3.qml)
    \endcode

    As all objects are put into an array, schematically you can look at it in
    the following way:

    ```
    count:       4         N          M     O
    objects:    aaaa|xxxxxxxxxxxxx|yyyyyyy|zzz
                ^    ^             ^       ^
    files:      |    QmlBase1.qml  |       QmlBase3.qml
                A.qml              QmlBase2.qml
    ```

    For the object lookup logic itself, see QQmltcObjectCreationHelper
*/
template<typename Predicate>
inline QString QmltcCodeGenerator::generate_typeCount(Predicate p) const
{
    using namespace Qt::StringLiterals;

    const QList<QQmlJSScope::ConstPtr> typesWithBaseTypeCount = visitor->qmlTypesWithQmlBases();
    QStringList components;
    components.reserve(1 + typesWithBaseTypeCount.size());

    // add this document's type counts minus document root
    Q_ASSERT(visitor->pureQmlTypes().size() > 0);
    components << QString::number(visitor->pureQmlTypes().size() - 1);

    // traverse types with QML base classes
    for (const QQmlJSScope::ConstPtr &t : typesWithBaseTypeCount) {
        if (p(t))
            break;
        QString typeCountTemplate = u"QQmltcObjectCreationHelper::typeCount<%1>()"_s;
        if (t == visitor->result()) { // t is this document's root
            components << typeCountTemplate.arg(t->baseTypeName());
        } else {
            components << typeCountTemplate.arg(t->internalName());
        }
    }

    return components.join(u" + "_s);
}

QT_END_NAMESPACE

#endif // QMLTCCOMPILERPIECES_H