aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlprivate.h
blob: 92c9765509889b4df1ceab47382457bf81284ba1 (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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QQMLPRIVATE_H
#define QQMLPRIVATE_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtQml/qjsprimitivevalue.h>
#include <QtQml/qjsvalue.h>
#include <QtQml/qqmllist.h>
#include <QtQml/qqmlparserstatus.h>
#include <QtQml/qqmlpropertyvaluesource.h>
#include <QtQml/qtqmlglobal.h>

#include <QtCore/qdatetime.h>
#include <QtCore/qdebug.h>
#include <QtCore/qglobal.h>
#include <QtCore/qmetacontainer.h>
#include <QtCore/qmetaobject.h>
#include <QtCore/qpointer.h>
#include <QtCore/qurl.h>
#include <QtCore/qvariant.h>
#include <QtCore/qversionnumber.h>

#include <functional>
#include <limits>
#include <type_traits>

QT_BEGIN_NAMESPACE

class QQmlPropertyValueInterceptor;
class QQmlContextData;
class QQmlFinalizerHook;

namespace QQmlPrivate {
struct CachedQmlUnit;
template<typename A>
using QQmlAttachedPropertiesFunc = A *(*)(QObject *);
}

namespace QV4 {
struct ExecutionEngine;
class ExecutableCompilationUnit;
namespace CompiledData {
struct Unit;
}
}
namespace QmlIR {
struct Document;
typedef void (*IRLoaderFunction)(Document *, const QQmlPrivate::CachedQmlUnit *);
}

using QQmlAttachedPropertiesFunc = QQmlPrivate::QQmlAttachedPropertiesFunc<QObject>;

inline size_t qHash(QQmlAttachedPropertiesFunc func, size_t seed = 0)
{
    return qHash(quintptr(func), seed);
}

template <typename TYPE>
class QQmlTypeInfo
{
public:
    enum {
        hasAttachedProperties = 0
    };
};


class QJSEngine;
class QQmlEngine;
class QQmlCustomParser;
class QQmlTypeNotAvailable;

class QQmlV4Function;
using QQmlV4FunctionPtr = QQmlV4Function *;
using QQmlV4ExecutionEnginePtr = QV4::ExecutionEngine *;

template<class T>
QQmlCustomParser *qmlCreateCustomParser()
{
    return nullptr;
}

namespace QQmlPrivate
{
    void Q_QML_EXPORT qdeclarativeelement_destructor(QObject *);
    template<typename T>
    class QQmlElement final : public T
    {
    public:
        ~QQmlElement() override {
            QQmlPrivate::qdeclarativeelement_destructor(this);
        }
        static void operator delete(void *ptr) {
            // We allocate memory from this class in QQmlType::create
            // along with some additional memory.
            // So we override the operator delete in order to avoid the
            // sized operator delete to be called with a different size than
            // the size that was allocated.
            ::operator delete (ptr);
        }
#ifdef Q_CC_MSVC
        static void operator delete(void *, void *) {
            // Deliberately empty placement delete operator.
            // Silences MSVC warning C4291: no matching operator delete found
            // On MinGW it causes -Wmismatched-new-delete, though.
        }
#endif
    };

    enum class SingletonConstructionMode
    {
        None,
        Constructor,
        Factory,
        FactoryWrapper
    };

    template<typename T, typename WrapperT = T, typename = std::void_t<>>
    struct HasSingletonFactory
    {
        static constexpr bool value = false;
    };

    template<typename T, typename WrapperT>
    struct HasSingletonFactory<T, WrapperT, std::void_t<decltype(WrapperT::create(
                                                               static_cast<QQmlEngine *>(nullptr),
                                                               static_cast<QJSEngine *>(nullptr)))>>
    {
        static constexpr bool value = std::is_same_v<
            decltype(WrapperT::create(static_cast<QQmlEngine *>(nullptr),
                               static_cast<QJSEngine *>(nullptr))), T *>;
    };

    template<typename T, typename WrapperT>
    constexpr SingletonConstructionMode singletonConstructionMode()
    {
        if constexpr (!std::is_base_of<QObject, T>::value)
            return SingletonConstructionMode::None;
        if constexpr (!std::is_same_v<T, WrapperT> && HasSingletonFactory<T, WrapperT>::value)
            return SingletonConstructionMode::FactoryWrapper;
        if constexpr (std::is_default_constructible<T>::value)
            return SingletonConstructionMode::Constructor;
        if constexpr (HasSingletonFactory<T>::value)
            return SingletonConstructionMode::Factory;

        return SingletonConstructionMode::None;
    }

    template<typename>
    struct QmlMarkerFunction;

    template<typename Ret, typename Class>
    struct QmlMarkerFunction<Ret (Class::*)()>
    {
        using ClassType = Class;
    };

    template<typename T, typename Marker>
    using QmlTypeHasMarker = std::is_same<T, typename QmlMarkerFunction<Marker>::ClassType>;

    template<typename T>
    void createInto(void *memory, void *) { new (memory) QQmlElement<T>; }

    template<typename T, typename WrapperT, SingletonConstructionMode Mode>
    QObject *createSingletonInstance(QQmlEngine *q, QJSEngine *j)
    {
        Q_UNUSED(q);
        Q_UNUSED(j);
        if constexpr (Mode == SingletonConstructionMode::Constructor)
            return new T;
        else if constexpr (Mode == SingletonConstructionMode::Factory)
            return T::create(q, j);
        else if constexpr (Mode == SingletonConstructionMode::FactoryWrapper)
            return WrapperT::create(q, j);
        else
            return nullptr;
    }

    template<typename T>
    QObject *createParent(QObject *p) { return new T(p); }

    using CreateIntoFunction = void (*)(void *, void *);
    using CreateSingletonFunction = QObject *(*)(QQmlEngine *, QJSEngine *);
    using CreateParentFunction = QObject *(*)(QObject *);
    using CreateValueTypeFunction = QVariant (*)(const QJSValue &);

    template<typename T, typename WrapperT = T,
             SingletonConstructionMode Mode = singletonConstructionMode<T, WrapperT>()>
    struct Constructors;

    template<typename T, typename WrapperT>
    struct Constructors<T, WrapperT, SingletonConstructionMode::Constructor>
    {
        static constexpr CreateIntoFunction createInto
                = QQmlPrivate::createInto<T>;
        static constexpr CreateSingletonFunction createSingletonInstance
                = QQmlPrivate::createSingletonInstance<
                    T, WrapperT, SingletonConstructionMode::Constructor>;
    };

    template<typename T, typename WrapperT>
    struct Constructors<T, WrapperT, SingletonConstructionMode::None>
    {
        static constexpr CreateIntoFunction createInto = nullptr;
        static constexpr CreateSingletonFunction createSingletonInstance = nullptr;
    };

    template<typename T, typename WrapperT>
    struct Constructors<T, WrapperT, SingletonConstructionMode::Factory>
    {
        static constexpr CreateIntoFunction createInto = nullptr;
        static constexpr CreateSingletonFunction createSingletonInstance
                = QQmlPrivate::createSingletonInstance<
                    T, WrapperT, SingletonConstructionMode::Factory>;
    };

    template<typename T, typename WrapperT>
    struct Constructors<T, WrapperT, SingletonConstructionMode::FactoryWrapper>
    {
        static constexpr CreateIntoFunction createInto = nullptr;
        static constexpr CreateSingletonFunction createSingletonInstance
                = QQmlPrivate::createSingletonInstance<
                    T, WrapperT, SingletonConstructionMode::FactoryWrapper>;
    };

    template<typename T,
             bool IsObject = std::is_base_of<QObject, T>::value,
             bool IsGadget = QtPrivate::IsGadgetHelper<T>::IsRealGadget>
    struct ExtendedType;

    template<typename T>
    struct ExtendedType<T, false, false>
    {
        static constexpr const CreateParentFunction createParent = nullptr;
        static const QMetaObject *staticMetaObject() { return nullptr; }
    };

    // If it's a QObject, we actually want an error if the ctor or the metaobject is missing.
    template<typename T>
    struct ExtendedType<T, true, false>
    {
        static constexpr const CreateParentFunction createParent = QQmlPrivate::createParent<T>;
        static const QMetaObject *staticMetaObject() { return &T::staticMetaObject; }
    };

    // If it's a Q_GADGET, we don't want the ctor.
    template<typename T>
    struct ExtendedType<T, false, true>
    {
        static constexpr const CreateParentFunction createParent = nullptr;
        static const QMetaObject *staticMetaObject() { return &T::staticMetaObject; }
    };

    template<typename F, typename Result = void>
    struct ValueTypeFactory
    {
        static constexpr const Result (*create)(const QJSValue &) = nullptr;
    };

    template<typename F>
    struct ValueTypeFactory<F, std::void_t<decltype(F::create(QJSValue()))>>
    {
        static decltype(F::create(QJSValue())) create(const QJSValue &params)
        {
            return F::create(params);
        }
    };

    template<typename T, typename F,
             bool HasCtor = std::is_constructible_v<T, QJSValue>,
             bool HasFactory = std::is_constructible_v<
                 QVariant, decltype(ValueTypeFactory<F>::create(QJSValue()))>>
    struct ValueType;

    template<typename T, typename F>
    struct ValueType<T, F, false, false>
    {
        static constexpr const CreateValueTypeFunction create = nullptr;
    };

    template<typename T, typename F, bool HasCtor>
    struct ValueType<T, F, HasCtor, true>
    {
        static QVariant create(const QJSValue &params)
        {
            return F::create(params);
        }
    };

    template<typename T, typename F>
    struct ValueType<T, F, true, false>
    {
        static QVariant create(const QJSValue &params)
        {
            return QVariant::fromValue(T(params));
        }
    };

    template<class From, class To, int N>
    struct StaticCastSelectorClass
    {
        static inline int cast() { return -1; }
    };

    template<class From, class To>
    struct StaticCastSelectorClass<From, To, sizeof(int)>
    {
        static inline int cast() { return int(reinterpret_cast<quintptr>(static_cast<To *>(reinterpret_cast<From *>(0x10000000)))) - 0x10000000; }
    };

    template<class From, class To>
    struct StaticCastSelector
    {
        typedef int yes_type;
        typedef char no_type;

        static yes_type checkType(To *);
        static no_type checkType(...);

        static inline int cast()
        {
            return StaticCastSelectorClass<From, To, sizeof(checkType(reinterpret_cast<From *>(0)))>::cast();
        }
    };

    // You can prevent subclasses from using the same attached type by specialzing this.
    // This is reserved for internal types, though.
    template<class T, class A>
    struct OverridableAttachedType
    {
        using Type = A;
    };

    template<class T, class = std::void_t<>, bool OldStyle = QQmlTypeInfo<T>::hasAttachedProperties>
    struct QmlAttached
    {
        using Type = void;
        using Func = QQmlAttachedPropertiesFunc<QObject>;
        static const QMetaObject *staticMetaObject() { return nullptr; }
        static Func attachedPropertiesFunc() { return nullptr; }
    };

    // Defined inline via QML_ATTACHED
    template<class T>
    struct QmlAttached<T, std::void_t<typename OverridableAttachedType<T, typename T::QmlAttachedType>::Type>, false>
    {
        // Normal attached properties
        template <typename Parent, typename Attached>
        struct Properties
        {
            using Func = QQmlAttachedPropertiesFunc<Attached>;
            static const QMetaObject *staticMetaObject() { return &Attached::staticMetaObject; }
            static Func attachedPropertiesFunc() { return Parent::qmlAttachedProperties; }
        };

        // Disabled via OverridableAttachedType
        template<typename Parent>
        struct Properties<Parent, void>
        {
            using Func = QQmlAttachedPropertiesFunc<QObject>;
            static const QMetaObject *staticMetaObject() { return nullptr; }
            static Func attachedPropertiesFunc() { return nullptr; }
        };

        using Type = typename std::conditional<
                QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_attached)>::value,
                typename OverridableAttachedType<T, typename T::QmlAttachedType>::Type, void>::type;
        using Func = typename Properties<T, Type>::Func;

        static const QMetaObject *staticMetaObject()
        {
            return Properties<T, Type>::staticMetaObject();
        }

        static Func attachedPropertiesFunc()
        {
            return Properties<T, Type>::attachedPropertiesFunc();
        }
    };

    // Separately defined via QQmlTypeInfo
    template<class T>
    struct QmlAttached<T, std::void_t<decltype(T::qmlAttachedProperties)>, true>
    {
        using Type = typename std::remove_pointer<decltype(T::qmlAttachedProperties(nullptr))>::type;
        using Func = QQmlAttachedPropertiesFunc<Type>;

        static const QMetaObject *staticMetaObject() { return &Type::staticMetaObject; }
        static Func attachedPropertiesFunc() { return T::qmlAttachedProperties; }
    };

    // This is necessary because both the type containing a default template parameter and the type
    // instantiating the template need to have access to the default template parameter type. In
    // this case that's T::QmlAttachedType. The QML_FOREIGN macro needs to befriend specific other
    // types. Therefore we need some kind of "accessor". Because of compiler bugs in gcc and clang,
    // we cannot befriend attachedPropertiesFunc() directly. Wrapping the actual access into another
    // struct "fixes" that. For convenience we still want the free standing functions in addition.
    template<class T>
    struct QmlAttachedAccessor
    {
        static QQmlAttachedPropertiesFunc<QObject> attachedPropertiesFunc()
        {
            return QQmlAttachedPropertiesFunc<QObject>(QmlAttached<T>::attachedPropertiesFunc());
        }

        static const QMetaObject *staticMetaObject()
        {
            return QmlAttached<T>::staticMetaObject();
        }
    };

    template<typename T>
    inline QQmlAttachedPropertiesFunc<QObject> attachedPropertiesFunc()
    {
        return QmlAttachedAccessor<T>::attachedPropertiesFunc();
    }

    template<typename T>
    inline const QMetaObject *attachedPropertiesMetaObject()
    {
        return QmlAttachedAccessor<T>::staticMetaObject();
    }

    enum AutoParentResult { Parented, IncompatibleObject, IncompatibleParent };
    typedef AutoParentResult (*AutoParentFunction)(QObject *object, QObject *parent);

    enum class ValueTypeCreationMethod { None, Construct, Structured };

    struct RegisterType {
        enum StructVersion: int {
            Base = 0,
            FinalizerCast = 1,
            CreationMethod = 2,
            CurrentVersion = CreationMethod,
        };

        bool has(StructVersion v) const { return structVersion >= int(v); }

        int structVersion;

        QMetaType typeId;
        QMetaType listId;
        int objectSize;
        // The second parameter of create is for userdata
        void (*create)(void *, void *);
        void *userdata;
        QString noCreationReason;

        // ### Qt7: Get rid of this. It can be covered by creationMethod below.
        QVariant (*createValueType)(const QJSValue &);

        const char *uri;
        QTypeRevision version;
        const char *elementName;
        const QMetaObject *metaObject;

        QQmlAttachedPropertiesFunc<QObject> attachedPropertiesFunction;
        const QMetaObject *attachedPropertiesMetaObject;

        int parserStatusCast;
        int valueSourceCast;
        int valueInterceptorCast;

        QObject *(*extensionObjectCreate)(QObject *);
        const QMetaObject *extensionMetaObject;

        QQmlCustomParser *customParser;

        QTypeRevision revision;
        int finalizerCast;

        ValueTypeCreationMethod creationMethod;
        // If this is extended ensure "version" is bumped!!!
    };

    struct RegisterTypeAndRevisions {
        int structVersion;

        QMetaType typeId;
        QMetaType listId;
        int objectSize;
        void (*create)(void *, void *);
        void *userdata;

        QVariant (*createValueType)(const QJSValue &);

        const char *uri;
        QTypeRevision version;

        const QMetaObject *metaObject;
        const QMetaObject *classInfoMetaObject;

        QQmlAttachedPropertiesFunc<QObject> attachedPropertiesFunction;
        const QMetaObject *attachedPropertiesMetaObject;

        int parserStatusCast;
        int valueSourceCast;
        int valueInterceptorCast;

        QObject *(*extensionObjectCreate)(QObject *);
        const QMetaObject *extensionMetaObject;

        QQmlCustomParser *(*customParserFactory)();
        QVector<int> *qmlTypeIds;
        int finalizerCast;

        bool forceAnonymous;
        QMetaSequence listMetaSequence;
    };

    struct RegisterInterface {
        int structVersion;

        QMetaType typeId;
        QMetaType listId;

        const char *iid;

        const char *uri;
        QTypeRevision version;
    };

    struct RegisterAutoParent {
        int structVersion;

        AutoParentFunction function;
    };

    struct RegisterSingletonType {
        int structVersion;

        const char *uri;
        QTypeRevision version;
        const char *typeName;

        std::function<QJSValue(QQmlEngine *, QJSEngine *)> scriptApi;
        std::function<QObject*(QQmlEngine *, QJSEngine *)> qObjectApi;

        const QMetaObject *instanceMetaObject;
        QMetaType typeId;

        QObject *(*extensionObjectCreate)(QObject *);
        const QMetaObject *extensionMetaObject;

        QTypeRevision revision;
    };

    struct RegisterSingletonTypeAndRevisions {
        int structVersion;
        const char *uri;
        QTypeRevision version;

        std::function<QObject*(QQmlEngine *, QJSEngine *)> qObjectApi;

        const QMetaObject *instanceMetaObject;
        const QMetaObject *classInfoMetaObject;

        QMetaType typeId;

        QObject *(*extensionObjectCreate)(QObject *);
        const QMetaObject *extensionMetaObject;

        QVector<int> *qmlTypeIds;
    };

    struct RegisterCompositeType {
        int structVersion;
        QUrl url;
        const char *uri;
        QTypeRevision version;
        const char *typeName;
    };

    struct RegisterCompositeSingletonType {
        int structVersion;
        QUrl url;
        const char *uri;
        QTypeRevision version;
        const char *typeName;
    };

    struct RegisterSequentialContainer {
        int structVersion;
        const char *uri;
        QTypeRevision version;

        // ### Qt7: Remove typeName. It's ignored because the only valid name is "list",
        //          and that's automatic.
        const char *typeName;

        QMetaType typeId;
        QMetaSequence metaSequence;
        QTypeRevision revision;
    };

    struct RegisterSequentialContainerAndRevisions {
        int structVersion;
        const char *uri;
        QTypeRevision version;

        const QMetaObject *classInfoMetaObject;
        QMetaType typeId;
        QMetaSequence metaSequence;

        QVector<int> *qmlTypeIds;
    };

    struct Q_QML_EXPORT AOTCompiledContext {
        enum: uint { InvalidStringId = (std::numeric_limits<uint>::max)() };

        QQmlContextData *qmlContext;
        QObject *qmlScopeObject;
        QJSEngine *engine;
        union {
            QV4::ExecutableCompilationUnit *compilationUnit;
            qintptr extraData;
        };

        QObject *thisObject() const;
        QQmlEngine *qmlEngine() const;

        QJSValue jsMetaType(int index) const;
        void setInstructionPointer(int offset) const;
        void setReturnValueUndefined() const;

        // Run QQmlPropertyCapture::captureProperty() without retrieving the value.
        bool captureLookup(uint index, QObject *object) const;
        bool captureQmlContextPropertyLookup(uint index) const;
        void captureTranslation() const;
        QString translationContext() const;
        QMetaType lookupResultMetaType(uint index) const;
        void storeNameSloppy(uint nameIndex, void *value, QMetaType type) const;
        QJSValue javaScriptGlobalProperty(uint nameIndex) const;

        const QLoggingCategory *resolveLoggingCategory(QObject *wrapper, bool *ok) const;

        void writeToConsole(
                QtMsgType type, const QString &message,
                const QLoggingCategory *loggingCategory) const;

        QVariant constructValueType(
                QMetaType resultMetaType, const QMetaObject *resultMetaObject,
                int ctorIndex, void *ctorArg) const;

        // Those are explicit arguments to the Date() ctor, not implicit coercions.
        QDateTime constructDateTime(double timestamp) const;
        QDateTime constructDateTime(const QString &string) const;
        QDateTime constructDateTime(const QJSPrimitiveValue &arg) const
        {
            return arg.type() == QJSPrimitiveValue::String
                    ? constructDateTime(arg.toString())
                    : constructDateTime(arg.toDouble());
        }

        QDateTime constructDateTime(
                double year, double month, double day = 1,
                double hours = 0, double minutes = 0, double seconds = 0, double msecs = 0) const;

        // All of these lookup functions should be used as follows:
        //
        // while (!fooBarLookup(...)) {
        //     setInstructionPointer(...);
        //     initFooBarLookup(...);
        //     if (engine->hasException()) {
        //         ...
        //         break;
        //     }
        // }
        //
        // The bool-returning *Lookup functions exclusively run the happy path and return false if
        // that fails in any way. The failure may either be in the lookup structs not being
        // initialized or an exception being thrown.
        // The init*Lookup functions initialize the lookup structs and amend any exceptions
        // previously thrown with line numbers. They might also throw their own exceptions. If an
        // exception is present after the initialization there is no way to carry out the lookup and
        // the exception should be propagated. If not, the original lookup can be tried again.

        bool callQmlContextPropertyLookup(
                uint index, void **args, const QMetaType *types, int argc) const;
        void initCallQmlContextPropertyLookup(uint index) const;

        bool loadContextIdLookup(uint index, void *target) const;
        void initLoadContextIdLookup(uint index) const;

        bool callObjectPropertyLookup(uint index, QObject *object,
                                      void **args, const QMetaType *types, int argc) const;
        void initCallObjectPropertyLookup(uint index) const;

        bool callGlobalLookup(uint index, void **args, const QMetaType *types, int argc) const;
        void initCallGlobalLookup(uint index) const;

        bool loadGlobalLookup(uint index, void *target, QMetaType type) const;
        void initLoadGlobalLookup(uint index) const;

        bool loadScopeObjectPropertyLookup(uint index, void *target) const;
        bool writeBackScopeObjectPropertyLookup(uint index, void *source) const;
        void initLoadScopeObjectPropertyLookup(uint index, QMetaType type) const;

        bool loadSingletonLookup(uint index, void *target) const;
        void initLoadSingletonLookup(uint index, uint importNamespace) const;

        bool loadAttachedLookup(uint index, QObject *object, void *target) const;
        void initLoadAttachedLookup(uint index, uint importNamespace, QObject *object) const;

        bool loadTypeLookup(uint index, void *target) const;
        void initLoadTypeLookup(uint index, uint importNamespace) const;

        bool getObjectLookup(uint index, QObject *object, void *target) const;
        bool writeBackObjectLookup(uint index, QObject *object, void *source) const;
        void initGetObjectLookup(uint index, QObject *object, QMetaType type) const;

        bool getValueLookup(uint index, void *value, void *target) const;
        bool writeBackValueLookup(uint index, void *value, void *source) const;
        void initGetValueLookup(uint index, const QMetaObject *metaObject, QMetaType type) const;

        bool getEnumLookup(uint index, void *target) const;
#if QT_QML_REMOVED_SINCE(6, 6)
        bool getEnumLookup(uint index, int *target) const;
#endif
        void initGetEnumLookup(uint index, const QMetaObject *metaObject,
                               const char *enumerator, const char *enumValue) const;

        bool setObjectLookup(uint index, QObject *object, void *value) const;
        void initSetObjectLookup(uint index, QObject *object, QMetaType type) const;

        bool setValueLookup(uint index, void *target, void *value) const;
        void initSetValueLookup(uint index, const QMetaObject *metaObject, QMetaType type) const;
    };

    struct AOTCompiledFunction {
        int functionIndex;
        int numArguments;
        void (*signature)(QV4::ExecutableCompilationUnit *unit, QMetaType *argTypes);
        void (*functionPtr)(const AOTCompiledContext *context, void **argv);
    };

#if QT_DEPRECATED_SINCE(6, 6)
    QT_DEPRECATED_VERSION_X(6, 6, "Use AOTCompiledFunction instead")
    typedef AOTCompiledFunction TypedFunction;
#endif

    struct CachedQmlUnit {
        const QV4::CompiledData::Unit *qmlData;
        const AOTCompiledFunction *aotCompiledFunctions;
        void *unused2;
    };

    typedef const CachedQmlUnit *(*QmlUnitCacheLookupFunction)(const QUrl &url);
    struct RegisterQmlUnitCacheHook {
        int structVersion;
        QmlUnitCacheLookupFunction lookupCachedQmlUnit;
    };

    enum RegistrationType {
        TypeRegistration       = 0,
        InterfaceRegistration  = 1,
        AutoParentRegistration = 2,
        SingletonRegistration  = 3,
        CompositeRegistration  = 4,
        CompositeSingletonRegistration = 5,
        QmlUnitCacheHookRegistration = 6,
        TypeAndRevisionsRegistration = 7,
        SingletonAndRevisionsRegistration = 8,
        SequentialContainerRegistration = 9,
        SequentialContainerAndRevisionsRegistration = 10,
    };

    int Q_QML_EXPORT qmlregister(RegistrationType, void *);
    void Q_QML_EXPORT qmlunregister(RegistrationType, quintptr);

#if QT_DEPRECATED_SINCE(6, 3)
    struct Q_QML_EXPORT SingletonFunctor
    {
        QT_DEPRECATED QObject *operator()(QQmlEngine *, QJSEngine *);
        QPointer<QObject> m_object;
        bool alreadyCalled = false;
    };
#endif

    struct Q_QML_EXPORT SingletonInstanceFunctor
    {
        QObject *operator()(QQmlEngine *, QJSEngine *);

        QPointer<QObject> m_object;

        // Not a QPointer, so that you cannot assign it to a different
        // engine when the first one is deleted.
        // That would mess up the QML contexts.
        QQmlEngine *m_engine = nullptr;
    };

    static int indexOfOwnClassInfo(const QMetaObject *metaObject, const char *key, int startOffset = -1)
    {
        if (!metaObject || !key)
            return -1;

        const int offset = metaObject->classInfoOffset();
        const int start = (startOffset == -1)
                ? (metaObject->classInfoCount() + offset - 1)
                : startOffset;
        for (int i = start; i >= offset; --i)
            if (qstrcmp(key, metaObject->classInfo(i).name()) == 0) {
                return i;
        }
        return -1;
    }

    inline const char *classInfo(const QMetaObject *metaObject, const char *key)
    {
        return metaObject->classInfo(indexOfOwnClassInfo(metaObject, key)).value();
    }

    inline QTypeRevision revisionClassInfo(const QMetaObject *metaObject, const char *key,
                                       QTypeRevision defaultValue = QTypeRevision())
    {
        const int index = indexOfOwnClassInfo(metaObject, key);
        return (index == -1) ? defaultValue
                             : QTypeRevision::fromEncodedVersion(
                                   QLatin1StringView(metaObject->classInfo(index).value()).toInt());
    }

    Q_QML_EXPORT QList<QTypeRevision> revisionClassInfos(const QMetaObject *metaObject, const char *key);

    inline bool boolClassInfo(const QMetaObject *metaObject, const char *key,
                              bool defaultValue = false)
    {
        const int index = indexOfOwnClassInfo(metaObject, key);
        if (index == -1)
            return defaultValue;
        return qstrcmp(metaObject->classInfo(index).value(), "true") == 0;
    }

    template<class T, class = std::void_t<>>
    struct QmlExtended
    {
        using Type = void;
    };

    template<class T>
    struct QmlExtended<T, std::void_t<typename T::QmlExtendedType>>
    {
        using Type = typename std::conditional<
                QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_extended)>::value,
                typename T::QmlExtendedType, void>::type;
    };

    template<class T, class = std::void_t<>>
    struct QmlExtendedNamespace
    {
        static constexpr const QMetaObject *metaObject() { return nullptr; }
    };

    template<class T>
    struct QmlExtendedNamespace<T, std::void_t<decltype(T::qmlExtendedNamespace())>>
    {
        static constexpr const QMetaObject *metaObject()
        {
            if constexpr (QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_extendedNamespace)>::value)
                return T::qmlExtendedNamespace();
            else
                return nullptr;
        }
    };

    template<class T, class = std::void_t<>>
    struct QmlResolved
    {
        using Type = T;
    };

    template<class T>
    struct QmlResolved<T, std::void_t<typename T::QmlForeignType>>
    {
        using Type = typename std::conditional<
                QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_foreign)>::value,
                typename T::QmlForeignType, T>::type;
    };

    template<class T, class = std::void_t<>>
    struct QmlUncreatable
    {
        static constexpr bool Value = false;
    };

    template<class T>
    struct QmlUncreatable<T, std::void_t<typename T::QmlIsUncreatable>>
    {
        static constexpr bool Value =
                QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_uncreatable)>::value
                && bool(T::QmlIsUncreatable::yes);
    };

    template<class T, class = std::void_t<>>
    struct QmlAnonymous
    {
        static constexpr bool Value = false;
    };

    template<class T>
    struct QmlAnonymous<T, std::void_t<typename T::QmlIsAnonymous>>
    {
        static constexpr bool Value =
                QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_anonymous)>::value
                && bool(T::QmlIsAnonymous::yes);
    };


    template<class T, class = std::void_t<>>
    struct QmlSingleton
    {
        static constexpr bool Value = false;
    };

    template<class T>
    struct QmlSingleton<T, std::void_t<typename T::QmlIsSingleton>>
    {
        static constexpr bool Value =
                QmlTypeHasMarker<T, decltype(&T::qt_qmlMarker_singleton)>::value
                && bool(T::QmlIsSingleton::yes);
    };

    template<class T, class = std::void_t<>>
    struct QmlSequence
    {
        static constexpr bool Value = false;
    };

    template<class T>
    struct QmlSequence<T, std::void_t<typename T::QmlIsSequence>>
    {
        Q_STATIC_ASSERT((std::is_same_v<typename T::QmlSequenceValueType,
                                        typename QmlResolved<T>::Type::value_type>));
        static constexpr bool Value = bool(T::QmlIsSequence::yes);
    };

    template<class T, class = std::void_t<>>
    struct QmlInterface
    {
        static constexpr bool Value = false;
    };

    template<class T>
    struct QmlInterface<T, std::void_t<typename T::QmlIsInterface, decltype(qobject_interface_iid<T *>())>>
    {
        static constexpr bool Value = bool(T::QmlIsInterface::yes);
    };

    template<class T, typename = std::void_t<>>
    struct StaticMetaObject
    {
        static const QMetaObject *staticMetaObject() { return nullptr; }
    };

    template<class T>
    struct StaticMetaObject<T, std::void_t<decltype(T::staticMetaObject)>>
    {
        static const QMetaObject *staticMetaObject() { return &T::staticMetaObject; }
    };

    template<class T>
    struct QmlMetaType
    {
        static constexpr bool hasAcceptableCtors()
        {
            if constexpr (!std::is_default_constructible_v<T>)
                return false;
            else if constexpr (std::is_base_of_v<QObject, T>)
                return true;
            else
                return std::is_copy_constructible_v<T>;
        }

        static constexpr QMetaType self()
        {
            if constexpr (std::is_base_of_v<QObject, T>)
                return QMetaType::fromType<T*>();
            else
                return QMetaType::fromType<T>();
        }

        static constexpr QMetaType list()
        {
            if constexpr (std::is_base_of_v<QObject, T>)
                return QMetaType::fromType<QQmlListProperty<T>>();
            else
                return QMetaType::fromType<QList<T>>();
        }

        static constexpr QMetaSequence sequence()
        {
            if constexpr (std::is_base_of_v<QObject, T>)
                return QMetaSequence();
            else
                return QMetaSequence::fromContainer<QList<T>>();
        }

        static constexpr int size()
        {
            return sizeof(T);
        }
    };

    template<>
    struct QmlMetaType<void>
    {
        static constexpr bool hasAcceptableCtors() { return true; }
        static constexpr QMetaType self() { return QMetaType(); }
        static constexpr QMetaType list() { return QMetaType(); }
        static constexpr QMetaSequence sequence() { return QMetaSequence(); }
        static constexpr int size() { return 0; }
    };

    template<typename T, typename E, typename WrapperT = T>
    void qmlRegisterSingletonAndRevisions(const char *uri, int versionMajor,
                                          const QMetaObject *classInfoMetaObject,
                                          QVector<int> *qmlTypeIds, const QMetaObject *extension)
    {
        static_assert(std::is_base_of_v<QObject, T>);
        RegisterSingletonTypeAndRevisions api = {
            0,

            uri,
            QTypeRevision::fromMajorVersion(versionMajor),

            Constructors<T, WrapperT>::createSingletonInstance,

            StaticMetaObject<T>::staticMetaObject(),
            classInfoMetaObject,

            QmlMetaType<T>::self(),

            ExtendedType<E>::createParent,
            extension ? extension : ExtendedType<E>::staticMetaObject(),

            qmlTypeIds
        };

        qmlregister(SingletonAndRevisionsRegistration, &api);
    }

    template<typename T, typename E>
    void qmlRegisterTypeAndRevisions(const char *uri, int versionMajor,
                                     const QMetaObject *classInfoMetaObject,
                                     QVector<int> *qmlTypeIds, const QMetaObject *extension,
                                     bool forceAnonymous = false)
    {
        RegisterTypeAndRevisions type = {
            3,
            QmlMetaType<T>::self(),
            QmlMetaType<T>::list(),
            QmlMetaType<T>::size(),
            Constructors<T>::createInto,
            nullptr,
            ValueType<T, E>::create,

            uri,
            QTypeRevision::fromMajorVersion(versionMajor),

            StaticMetaObject<T>::staticMetaObject(),
            classInfoMetaObject,

            attachedPropertiesFunc<T>(),
            attachedPropertiesMetaObject<T>(),

            StaticCastSelector<T, QQmlParserStatus>::cast(),
            StaticCastSelector<T, QQmlPropertyValueSource>::cast(),
            StaticCastSelector<T, QQmlPropertyValueInterceptor>::cast(),

            ExtendedType<E>::createParent,
            extension ? extension : ExtendedType<E>::staticMetaObject(),

            &qmlCreateCustomParser<T>,
            qmlTypeIds,
            StaticCastSelector<T, QQmlFinalizerHook>::cast(),

            forceAnonymous,
            QmlMetaType<T>::sequence(),
        };

        // Initialize the extension so that we can find it by name or ID.
        qMetaTypeId<E>();

        qmlregister(TypeAndRevisionsRegistration, &type);
    }

    template<typename T>
    void qmlRegisterSequenceAndRevisions(const char *uri, int versionMajor,
                                         const QMetaObject *classInfoMetaObject,
                                         QVector<int> *qmlTypeIds)
    {
        RegisterSequentialContainerAndRevisions type = {
            0,
            uri,
            QTypeRevision::fromMajorVersion(versionMajor),
            classInfoMetaObject,
            QMetaType::fromType<T>(),
            QMetaSequence::fromContainer<T>(),
            qmlTypeIds
        };

        qmlregister(SequentialContainerAndRevisionsRegistration, &type);
    }

    template<>
    void Q_QML_EXPORT qmlRegisterTypeAndRevisions<QQmlTypeNotAvailable, void>(
            const char *uri, int versionMajor, const QMetaObject *classInfoMetaObject,
            QVector<int> *qmlTypeIds, const QMetaObject *, bool);

    constexpr QtPrivate::QMetaTypeInterface metaTypeForNamespace(
            const QtPrivate::QMetaTypeInterface::MetaObjectFn &metaObjectFunction, const char *name)
    {
        return {
            /*.revision=*/ 0,
            /*.alignment=*/ 0,
            /*.size=*/ 0,
            /*.flags=*/ 0,
            /*.typeId=*/ {},
            /*.metaObject=*/ metaObjectFunction,
            /*.name=*/ name,
            /*.defaultCtr=*/ nullptr,
            /*.copyCtr=*/ nullptr,
            /*.moveCtr=*/ nullptr,
            /*.dtor=*/ nullptr,
            /*.equals*/ nullptr,
            /*.lessThan*/ nullptr,
            /*.debugStream=*/ nullptr,
            /*.dataStreamOut=*/ nullptr,
            /*.dataStreamIn=*/ nullptr,
            /*.legacyRegisterOp=*/ nullptr
        };
    }

    Q_QML_EXPORT QObject *qmlExtendedObject(QObject *, int);

    enum QmlRegistrationWarning {
        UnconstructibleType,
        UnconstructibleSingleton,
        NonQObjectWithAtached,
    };

    Q_QML_EXPORT void qmlRegistrationWarning(QmlRegistrationWarning warning, QMetaType type);

    Q_QML_EXPORT QMetaType compositeMetaType(
            QV4::ExecutableCompilationUnit *unit, const QString &elementName);
    Q_QML_EXPORT QMetaType compositeListMetaType(
            QV4::ExecutableCompilationUnit *unit, const QString &elementName);

} // namespace QQmlPrivate

QT_END_NAMESPACE

Q_DECLARE_OPAQUE_POINTER(QQmlV4FunctionPtr)
Q_DECLARE_OPAQUE_POINTER(QQmlV4ExecutionEnginePtr)

#endif // QQMLPRIVATE_H