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
qv4compileddata_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4#ifndef QV4COMPILEDDATA_P_H
5#define QV4COMPILEDDATA_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <functional>
19#include <optional>
20
21#include <QtCore/qcryptographichash.h>
22#include <QtCore/qhash.h>
23#include <QtCore/qhashfunctions.h>
24#include <QtCore/qlocale.h>
25#include <QtCore/qscopeguard.h>
26#include <QtCore/qstring.h>
27#include <QtCore/qstringlist.h>
28#include <QtCore/qurl.h>
29#include <QtCore/qvector.h>
30#include <QtCore/qtyperevision.h>
31#include <QtCore/qxpfunctional.h>
32
33#if QT_CONFIG(temporaryfile)
34#include <QtCore/qsavefile.h>
35#endif
36
37#include <private/qendian_p.h>
38#include <private/qqmlnullablevalue_p.h>
39#include <private/qqmlpropertycachevector_p.h>
40#include <private/qqmlrefcount_p.h>
41#include <private/qqmltype_p.h>
42#include <private/qv4compilationunitmapper_p.h>
43#include <private/qv4staticvalue_p.h>
44
45#include <functional>
46#include <limits.h>
47
48QT_BEGIN_NAMESPACE
49
50// Bump this whenever the compiler data structures change in an incompatible way.
51//
52// IMPORTANT:
53//
54// Also change the comment behind the number to describe the latest change. This has the added
55// benefit that if another patch changes the version too, it will result in a merge conflict, and
56// not get removed silently.
57// Also update the comparison functions in qqmlpreviewdiff.cpp when you change the data structures.
58#define QV4_DATA_STRUCTURE_VERSION 0x56 // Move dependency checksum into runtime data
59
60class QIODevice;
61class QQmlTypeNameCache;
62class QQmlType;
63class QQmlEngine;
65class QQmlScriptData;
66
67namespace QQmlPrivate {
69}
70
71namespace QmlIR {
72struct Document;
73}
74
75namespace QV4 {
76namespace Heap {
77struct Module;
78struct String;
79struct InternalClass;
80};
81
82struct Function;
83class EvalISelFactory;
85
86namespace CompiledData {
87
88// index is per-object binding index
90
91// map from name index
93{
94 bool addToHash(QCryptographicHash *hash, QHash<quintptr, QByteArray> *checksums) const;
95};
96
97struct String;
98struct Function;
99struct Lookup;
100struct RegExp;
101struct Unit;
102struct RuntimeDataBlock;
103
104template <typename ItemType, typename Container, const ItemType *(Container::*IndexedGetter)(int index) const>
106{
107 TableIterator(const Container *container, int index) : container(container), index(index) {}
108 const Container *container;
109 int index;
110
111 const ItemType *operator->() const { return (container->*IndexedGetter)(index); }
112 ItemType operator*() const {return *operator->();}
113
115 {
116 ++index;
117 return *this;
118 }
119
121 {
123 ++index;
124 return result;
125 }
126
127 TableIterator operator+(int offset) const { return TableIterator(container, index + offset); }
128 TableIterator operator-(int offset) const { return TableIterator(container, index - offset); }
129 int operator-(TableIterator other) const { return index - other.index; }
130
131 bool operator==(const TableIterator &rhs) const { return index == rhs.index; }
132 bool operator!=(const TableIterator &rhs) const { return index != rhs.index; }
133};
134
136{
138 Location(quint32 l, quint32 c) : Location()
139 {
140 m_data.set<LineField>(l);
141 m_data.set<ColumnField>(c);
142 Q_ASSERT(m_data.get<LineField>() == l);
143 Q_ASSERT(m_data.get<ColumnField>() == c);
144 }
145
146 inline bool operator<(const Location &other) const {
147 return m_data.get<LineField>() < other.m_data.get<LineField>()
148 || (m_data.get<LineField>() == other.m_data.get<LineField>()
149 && m_data.get<ColumnField>() < other.m_data.get<ColumnField>());
150 }
151
152 friend size_t qHash(const Location &location, size_t seed = 0)
153 {
154 return QT_PREPEND_NAMESPACE(qHash)(location.m_data.data(), seed);
155 }
156
157 friend bool comparesEqual(const Location &a, const Location &b) noexcept
158 {
159 return a.m_data.data()== b.m_data.data();
160 }
162
163 void set(quint32 line, quint32 column)
164 {
165 m_data.set<LineField>(line);
166 m_data.set<ColumnField>(column);
167 }
168
169 quint32 line() const { return m_data.get<LineField>(); }
170 quint32 column() const { return m_data.get<ColumnField>(); }
171
172private:
175
176 quint32_le_bitfield_union<LineField, ColumnField> m_data;
177};
178static_assert(sizeof(Location) == 4, "Location structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
179
180struct RegExp
181{
190
192
194 RegExp(quint32 flags, quint32 stringIndex) : RegExp()
195 {
196 m_data.set<FlagsField>(flags);
197 m_data.set<StringIndexField>(stringIndex);
198 }
199
200 quint32 flags() const { return m_data.get<FlagsField>(); }
201 quint32 stringIndex() const { return m_data.get<StringIndexField>(); }
202
203private:
206 quint32_le_bitfield_union<FlagsField, StringIndexField> m_data;
207};
208static_assert(sizeof(RegExp) == 4, "RegExp structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
209
210struct Lookup
211{
218
219 enum Mode : unsigned int {
222 };
223
224 quint32 type() const { return m_data.get<TypeField>(); }
225 quint32 nameIndex() const { return m_data.get<NameIndexField>(); }
226 quint32 mode() const { return m_data.get<ModeField>(); }
227
229 Lookup(Type type, Mode mode, quint32 nameIndex) : Lookup()
230 {
231 m_data.set<TypeField>(type);
232 m_data.set<ModeField>(mode);
233 m_data.set<NameIndexField>(nameIndex);
234 }
235
236private:
239 // 1 bit left
241 quint32_le_bitfield_union<TypeField, ModeField, NameIndexField> m_data;
242};
243static_assert(sizeof(Lookup) == 4, "Lookup structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
244
246{
248
249 void set(quint32 nameOffset, bool isAccessor)
250 {
251 m_data.set<NameOffsetField>(nameOffset);
252 m_data.set<IsAccessorField>(isAccessor ? 1 : 0);
253 }
254
255 quint32 nameOffset() const { return m_data.get<NameOffsetField>(); }
256 bool isAccessor() const { return m_data.get<IsAccessorField>() != 0; }
257
258private:
261 quint32_le_bitfield_union<NameOffsetField, IsAccessorField> m_data;
262};
263static_assert(sizeof(JSClassMember) == 4, "JSClassMember structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
264
266{
268 // JSClassMember[nMembers]
269
270 static int calculateSize(int nMembers) { return (sizeof(JSClass) + nMembers * sizeof(JSClassMember) + 7) & ~7; }
271};
272static_assert(sizeof(JSClass) == 4, "JSClass structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
273
274struct String
275{
277
278 static int calculateSize(const QString &str) {
279 // we cannot enconuter strings larger than INT_MAX anyway, as such a string
280 // would already break in other parts of the compilation process
281 return (sizeof(String) + (int(str.size()) + 1) * sizeof(quint16) + 7) & ~0x7;
282 }
283};
284
285static_assert (sizeof (String) == 4, "String structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
286
289 qint32_le line; // signed because debug instructions get negative line numbers
291};
292static_assert(sizeof(CodeOffsetToLineAndStatement) == 12, "CodeOffsetToLineAndStatement structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
293
294struct Block
295{
300
301 const quint32_le *localsTable() const { return reinterpret_cast<const quint32_le *>(reinterpret_cast<const char *>(this) + localsOffset); }
302
303 static int calculateSize(int nLocals) {
304 int trailingData = nLocals*sizeof (quint32);
305 size_t size = align(align(sizeof(Block)) + size_t(trailingData));
306 Q_ASSERT(size < INT_MAX);
307 return int(size);
308 }
309
310 static size_t align(size_t a) {
311 return (a + 7) & ~size_t(7);
312 }
313};
314static_assert(sizeof(Block) == 12, "Block structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
315
316enum class NamedBuiltin: unsigned int {
318};
319
320enum class CommonType : unsigned int {
321 // Actual named builtins
331
332 // Optimization for very common other types
334
335 // No type specified or not recognized
337};
338
340{
341 enum Flag {
342 NoFlag = 0x0,
343 Common = 0x1,
344 List = 0x2,
345 };
347
348 void set(Flags flags, quint32 typeNameIndexOrCommonType)
349 {
350 m_data.set<IsListField>(flags.testFlag(List) ? 1 : 0);
351 m_data.set<IndexIsCommonTypeField>(flags.testFlag(Common) ? 1 : 0);
352 m_data.set<TypeNameIndexOrCommonTypeField>(typeNameIndexOrCommonType);
353 }
354
355 bool indexIsCommonType() const
356 {
357 return m_data.get<IndexIsCommonTypeField>() != 0;
358 }
359
360 bool isList() const
361 {
362 return m_data.get<IsListField>() != 0;
363 }
364
366 {
367 return m_data.get<TypeNameIndexOrCommonTypeField>();
368 }
369
370private:
374 quint32_le_bitfield_union<IndexIsCommonTypeField, IsListField, TypeNameIndexOrCommonTypeField> m_data;
375};
376static_assert(sizeof(ParameterType) == 4, "ParameterType structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
377
383static_assert(sizeof(Parameter) == 8, "Parameter structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
384
385// Function is aligned on an 8-byte boundary to make sure there are no bus errors or penalties
386// for unaligned access. The ordering of the fields is also from largest to smallest.
388{
389 enum Flags : unsigned int {
390 IsStrict = 0x1,
394 };
395
396 // Absolute offset into file where the code for this function is located.
399
403 quint32_le formalsOffset; // Can't turn this into a calculated offset because of the mutation in CompilationUnit::createUnitData.
408 size_t lineAndStatementNumberOffset() const { return localsOffset + nLocals * sizeof(quint32); }
409 quint32_le nestedFunctionIndex; // for functions that only return a single closure, used in signal handlers
410
414
418
420 {
421 return lineAndStatementNumberOffset() + nLineAndStatementNumbers * sizeof(CodeOffsetToLineAndStatement);
422 }
423
424 // Keep all unaligned data at the end
427
428 // quint32 formalsIndex[nFormals]
429 // quint32 localsIndex[nLocals]
430
431 const Parameter *formalsTable() const
432 {
433 return reinterpret_cast<const Parameter *>(
434 reinterpret_cast<const char *>(this) + formalsOffset);
435 }
436 const quint32_le *localsTable() const
437 {
438 return reinterpret_cast<const quint32_le *>(
439 reinterpret_cast<const char *>(this) + localsOffset);
440 }
442 {
443 return reinterpret_cast<const CodeOffsetToLineAndStatement *>(
444 reinterpret_cast<const char *>(this) + lineAndStatementNumberOffset());
445 }
446
447 // --- QQmlPropertyCacheCreator interface
448 const Parameter *formalsBegin() const { return formalsTable(); }
449 const Parameter *formalsEnd() const { return formalsTable() + nFormals; }
450 // ---
451
452 const quint32_le *labelInfoTable() const { return reinterpret_cast<const quint32_le *>(reinterpret_cast<const char *>(this) + labelInfosOffset()); }
453
454 const char *code() const { return reinterpret_cast<const char *>(this) + codeOffset; }
455
456 static int calculateSize(
457 int nFormals, int nLocals, int nLinesAndStatements, int labelInfoSize, int codeSize)
458 {
459 const size_t trailingData = nFormals * sizeof(Parameter)
460 + (nLocals + labelInfoSize) * sizeof (quint32)
461 + nLinesAndStatements * sizeof(CodeOffsetToLineAndStatement);
462 size_t size = align(align(sizeof(Function)) + size_t(trailingData)) + align(codeSize);
463 Q_ASSERT(size < INT_MAX);
464 return int(size);
465 }
466
467 static size_t align(size_t a) {
468 return (a + 7) & ~size_t(7);
469 }
470};
471static_assert(sizeof(Function) == 56, "Function structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
472
484static_assert(sizeof(Method) == 12, "Method structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
485
486struct Class
487{
494
495 const Method *methodTable() const { return reinterpret_cast<const Method *>(reinterpret_cast<const char *>(this) + methodTableOffset); }
496
497 static int calculateSize(int nStaticMethods, int nMethods) {
498 int trailingData = (nStaticMethods + nMethods) * sizeof(Method);
499 size_t size = align(sizeof(Class) + trailingData);
500 Q_ASSERT(size < INT_MAX);
501 return int(size);
502 }
503
504 static size_t align(size_t a) {
505 return (a + 7) & ~size_t(7);
506 }
507};
508static_assert(sizeof(Class) == 24, "Class structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
509
511{
513
514 static int calculateSize(int size) {
515 int trailingData = 2 * size * sizeof(quint32_le);
516 size_t s = align(sizeof(TemplateObject) + trailingData);
517 Q_ASSERT(s < INT_MAX);
518 return int(s);
519 }
520
521 static size_t align(size_t a) {
522 return (a + 7) & ~size_t(7);
523 }
524
525 const quint32_le *stringTable() const {
526 return reinterpret_cast<const quint32_le *>(reinterpret_cast<const char *>(this + 1));
527 }
528
529 uint stringIndexAt(uint i) const {
530 return stringTable()[i];
531 }
532 uint rawStringIndexAt(uint i) const {
533 return stringTable()[size + i];
534 }
535};
536static_assert(sizeof(TemplateObject) == 4, "Template object structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
537
546static_assert(sizeof(ExportEntry) == 20, "ExportEntry structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
547
555static_assert(sizeof(ImportEntry) == 16, "ImportEntry structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
556
557// Qml data structures
558
567static_assert(sizeof(TranslationData) == 16, "TranslationData structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
568
570{
572
586
594
598
599 void clearFlags() { flagsAndType.set<FlagsField>(0); }
600 void setFlag(Flag flag) { flagsAndType.set<FlagsField>(flagsAndType.get<FlagsField>() | flag); }
601 bool hasFlag(Flag flag) const { return Flags(flagsAndType.get<FlagsField>()) & flag; }
602 Flags flags() const { return Flags(flagsAndType.get<FlagsField>()); }
603
604 void setType(Type type) { flagsAndType.set<TypeField>(type); }
605 Type type() const { return Type(flagsAndType.get<TypeField>()); }
606
607 union {
608 bool b;
609 quint32_le constantValueIndex;
610 quint32_le compiledScriptIndex; // used when Type_Script
611 quint32_le objectIndex;
612 quint32_le translationDataIndex; // used when Type_Translation
613 quint32 nullMarker;
614 } value;
615 quint32_le stringIndex; // Set for Type_String and Type_Script (the latter because of script strings)
616
619
622
624
625 //reverse of Lexer::singleEscape()
626 static QString escapedString(const QString &string)
627 {
628 QString tmp = QLatin1String("\"");
629 for (int i = 0; i < string.size(); ++i) {
630 const QChar &c = string.at(i);
631 switch (c.unicode()) {
632 case 0x08:
633 tmp += QLatin1String("\\b");
634 break;
635 case 0x09:
636 tmp += QLatin1String("\\t");
637 break;
638 case 0x0A:
639 tmp += QLatin1String("\\n");
640 break;
641 case 0x0B:
642 tmp += QLatin1String("\\v");
643 break;
644 case 0x0C:
645 tmp += QLatin1String("\\f");
646 break;
647 case 0x0D:
648 tmp += QLatin1String("\\r");
649 break;
650 case 0x22:
651 tmp += QLatin1String("\\\"");
652 break;
653 case 0x27:
654 tmp += QLatin1String("\\\'");
655 break;
656 case 0x5C:
657 tmp += QLatin1String("\\\\");
658 break;
659 default:
660 tmp += c;
661 break;
662 }
663 }
664 tmp += QLatin1Char('\"');
665 return tmp;
666 }
667
669 {
670 const Binding::Type bindingType = type();
671 return bindingType == Type_Translation || bindingType == Type_TranslationById;
672 }
674
675 bool isNumberBinding() const { return type() == Type_Number; }
676
677 bool valueAsBoolean() const
678 {
679 if (type() == Type_Boolean)
680 return value.b;
681 return false;
682 }
683};
684
685static_assert(sizeof(Binding) == 24, "Binding structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
686
693
694static_assert(sizeof(InlineComponent) == 12, "InlineComponent structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
695
701static_assert(sizeof(EnumValue) == 8, "EnumValue structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
702
703struct Enum
704{
708
709 const EnumValue *enumValueAt(int idx) const {
710 return reinterpret_cast<const EnumValue*>(this + 1) + idx;
711 }
712
713 static int calculateSize(int nEnumValues) {
714 return (sizeof(Enum)
715 + nEnumValues * sizeof(EnumValue)
716 + 7) & ~0x7;
717 }
718
719 // --- QQmlPropertyCacheCreatorInterface
720 const EnumValue *enumValuesBegin() const { return enumValueAt(0); }
721 const EnumValue *enumValuesEnd() const { return enumValueAt(nEnumValues); }
722 int enumValueCount() const { return nEnumValues; }
723 // ---
724};
725static_assert(sizeof(Enum) == 12, "Enum structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
726
727struct Signal
728{
732 // Parameter parameters[1];
733
734 const Parameter *parameterAt(int idx) const {
735 return reinterpret_cast<const Parameter*>(this + 1) + idx;
736 }
737
738 static int calculateSize(int nParameters) {
739 return (sizeof(Signal)
740 + nParameters * sizeof(Parameter)
741 + 7) & ~0x7;
742 }
743
744 // --- QQmlPropertyCacheCceatorInterface
745 const Parameter *parametersBegin() const { return parameterAt(0); }
746 const Parameter *parametersEnd() const { return parameterAt(nParameters); }
747 int parameterCount() const { return nParameters; }
748 // ---
749};
750static_assert(sizeof(Signal) == 12, "Signal structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
751
752/*
753 * We aim to minimize the size of a struct as much as possible, while preserving at least 28 bits
754 * for each index.
755 *
756 * Note that Name and Type indices are provided by StringTableGenerator, containing a hashmap
757 * (and a list) of unique strings within one Compilation Unit. It sounds rather unrealistic to have
758 * 2^28 (260+ million) unique strings within 1 CU (even if it's some form of global registry), not
759 * to mention the amount of memory needed to maintain such a StringTableGenerator.
760 * Therefore, after some preliminary analysis, it seems that even 20 bits
761 * should be a rather conservative cap.
762 *
763 * However it doesn't seem to easily provide many benefits atm other than better (logically) grouped
764 * unions like, let's say nameIndexAndAttributes.
765 *
766 * Atm 32-bit member nameIndexAndVirtSpecifiers looks smth like this:
767 *
768 * NameIndexField IsVirtualField IsOverrideField IsFinalField
769 * |10100000101000111000001110000| 0 | 1 | 0 |
770 *
771 */
773{
774private:
779
785
786public:
796
797 quint32 nameIndex() const { return nameIndexAndVirtSpecifiers.get<NameIndexField>(); }
798 void setNameIndex(int nameIndex) { nameIndexAndVirtSpecifiers.set<NameIndexField>(nameIndex); }
799
800 bool isVirtual() const { return nameIndexAndVirtSpecifiers.get<IsVirtualField>(); }
801 void setIsVirtual(bool isVirtual)
802 {
803 nameIndexAndVirtSpecifiers.set<IsVirtualField>(isVirtual ? 1 : 0);
804 }
805
806 bool isOverride() const { return nameIndexAndVirtSpecifiers.get<IsOverrideField>(); }
807 void setIsOverride(bool isOverride)
808 {
809 nameIndexAndVirtSpecifiers.set<IsOverrideField>(isOverride ? 1 : 0);
810 }
811
812 bool isFinal() const { return nameIndexAndVirtSpecifiers.get<IsFinalField>(); }
813 void setIsFinal(bool isFinal) { nameIndexAndVirtSpecifiers.set<IsFinalField>(isFinal ? 1 : 0); }
814
816 {
817 data.set<CommonTypeOrTypeNameIndexField>(static_cast<quint32>(t));
818 data.set<IsCommonTypeField>(true);
819 }
820
822 if (isCommonType())
823 return CommonType(data.get<CommonTypeOrTypeNameIndexField>());
824 return CommonType::Invalid;
825 }
826
827 void setTypeNameIndex(int nameIndex)
828 {
829 data.set<CommonTypeOrTypeNameIndexField>(nameIndex);
830 data.set<IsCommonTypeField>(false);
831 }
832
833 int typeNameIndex() const
834 {
835 return data.get<IsCommonTypeField>() ? -1 : data.get<CommonTypeOrTypeNameIndexField>();
836 }
837
838 bool isCommonType() const { return data.get<IsCommonTypeField>(); }
839 uint commonTypeOrTypeNameIndex() const { return data.get<CommonTypeOrTypeNameIndexField>(); }
840
841 bool isList() const { return data.get<IsListField>(); }
842 void setIsList(bool isList) { data.set<IsListField>(isList); }
843
844 bool isRequired() const { return data.get<IsRequiredField>(); }
845 void setIsRequired(bool isRequired) { data.set<IsRequiredField>(isRequired); }
846
847 bool isReadOnly() const { return data.get<IsReadOnlyField>(); }
848 void setIsReadOnly(bool isReadOnly) { data.set<IsReadOnlyField>(isReadOnly); }
849};
850static_assert(sizeof(Property) == 12, "Property structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
851
855
856static_assert (sizeof(RequiredPropertyExtraData) == 4, "RequiredPropertyExtraData structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
857
858struct Alias
859{
860 bool isReadOnly() const { return m_nameIndexAndFlags.get<IsReadOnlyField>(); }
861 void setIsReadOnly(bool isReadOnly) { m_nameIndexAndFlags.set<IsReadOnlyField>(isReadOnly); }
862
863 quint32 nameIndex() const { return m_nameIndexAndFlags.get<NameIndexField>(); }
864 void setNameIndex(quint32 nameIndex)
865 {
866 m_nameIndexAndFlags.set<NameIndexField>(nameIndex);
867 }
868
869 quint32 idIndex() const { return m_idIndexField; }
870 void setIdIndex(quint32 idIndex) { m_idIndexField = idIndex; }
871
872 quint32 propertyNameIndex() const { return m_propertyNameIndex; }
873 void setPropertyNameIndex(quint32 propertyNameIndex)
874 {
875 m_propertyNameIndex = propertyNameIndex;
876 }
877
878 Location location() const { return m_location; }
879 void setLocation(Location location) { m_location = location; }
880
881 Location referenceLocation() const { return m_referenceLocation; }
882 void setReferenceLocation(Location referenceLocation)
883 {
884 m_referenceLocation = referenceLocation;
885 }
886
887private:
890
891 quint32_le_bitfield_union<NameIndexField, IsReadOnlyField> m_nameIndexAndFlags;
892 quint32_le m_idIndexField;
893 quint32_le m_propertyNameIndex; // string index
894 Location m_location;
895 Location m_referenceLocation;
896};
897static_assert(sizeof(Alias) == 20, "Alias structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
898
899struct Object
900{
901private:
904 // 16 bits vacant
905public:
906 enum Flag : unsigned int {
907 NoFlag = 0x0,
910 };
912
913 // Depending on the use, this may be the type name to instantiate before instantiating this
914 // object. For grouped properties the type name will be empty and for attached properties
915 // it will be the name of the attached type.
920 qint32_le indexOfDefaultPropertyOrAlias; // -1 means no default property declared in this object
928 quint32_le offsetToEnums; // which in turn will be a table with offsets to variable-sized Enum objects
929 quint32_le offsetToSignals; // which in turn will be a table with offsets to variable-sized Signal objects
939// Function[]
940// Property[]
941// Signal[]
942// Binding[]
943// InlineComponent[]
944// RequiredPropertyExtraData[]
945
946 Flags flags() const
947 {
948 return Flags(flagsAndDefaultPropertyIsAlias.get<FlagsField>());
949 }
950
951 bool hasFlag(Flag flag) const
952 {
953 return flagsAndDefaultPropertyIsAlias.get<FlagsField>() & flag;
954 }
955
956 void setFlag(Flag flag)
957 {
958 flagsAndDefaultPropertyIsAlias.set<FlagsField>(
959 flagsAndDefaultPropertyIsAlias.get<FlagsField>() | flag);
960 }
961
962 void setFlags(Flags flags)
963 {
964 flagsAndDefaultPropertyIsAlias.set<FlagsField>(flags);
965 }
966
968 {
969 return flagsAndDefaultPropertyIsAlias.get<DefaultPropertyIsAliasField>();
970 }
971
972 void setHasAliasAsDefaultProperty(bool defaultAlias)
973 {
974 flagsAndDefaultPropertyIsAlias.set<DefaultPropertyIsAliasField>(defaultAlias);
975 }
976
977 static int calculateSizeExcludingSignalsAndEnums(int nFunctions, int nProperties, int nAliases, int nEnums, int nSignals, int nBindings, int nInlineComponents, int nRequiredPropertyExtraData)
978 {
979 return ( sizeof(Object)
980 + nFunctions * sizeof(quint32)
981 + nProperties * sizeof(Property)
982 + nAliases * sizeof(Alias)
983 + nEnums * sizeof(quint32)
984 + nSignals * sizeof(quint32)
985 + nBindings * sizeof(Binding)
986 + nInlineComponents * sizeof(InlineComponent)
987 + nRequiredPropertyExtraData * sizeof(RequiredPropertyExtraData)
988 + 0x7
989 ) & ~0x7;
990 }
991
993 {
994 return reinterpret_cast<const quint32_le*>(reinterpret_cast<const char *>(this) + offsetToFunctions);
995 }
996
997 const Property *propertyTable() const
998 {
999 return reinterpret_cast<const Property*>(reinterpret_cast<const char *>(this) + offsetToProperties);
1000 }
1001
1002 const Alias *aliasTable() const
1003 {
1004 return reinterpret_cast<const Alias*>(reinterpret_cast<const char *>(this) + offsetToAliases);
1005 }
1006
1007 const Binding *bindingTable() const
1008 {
1009 return reinterpret_cast<const Binding*>(reinterpret_cast<const char *>(this) + offsetToBindings);
1010 }
1011
1012 const Enum *enumAt(int idx) const
1013 {
1014 const quint32_le *offsetTable = reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToEnums);
1015 const quint32_le offset = offsetTable[idx];
1016 return reinterpret_cast<const Enum*>(reinterpret_cast<const char*>(this) + offset);
1017 }
1018
1019 const Signal *signalAt(int idx) const
1020 {
1021 const quint32_le *offsetTable = reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToSignals);
1022 const quint32_le offset = offsetTable[idx];
1023 return reinterpret_cast<const Signal*>(reinterpret_cast<const char*>(this) + offset);
1024 }
1025
1027 {
1028 return inlineComponentTable() + idx;
1029 }
1030
1032 {
1033 return reinterpret_cast<const InlineComponent*>(reinterpret_cast<const char *>(this) + offsetToInlineComponents);
1034 }
1035
1040
1042 {
1043 return reinterpret_cast<const RequiredPropertyExtraData*>(reinterpret_cast<const char *>(this) + offsetToRequiredPropertyExtraData);
1044 }
1045
1046 // --- QQmlPropertyCacheCreator interface
1047 int propertyCount() const { return nProperties; }
1048 int aliasCount() const { return nAliases; }
1049 int enumCount() const { return nEnums; }
1050 int signalCount() const { return nSignals; }
1051 int functionCount() const { return nFunctions; }
1052 int inlineComponentCount() const { return nInlineComponents; }
1053
1054 const Binding *bindingsBegin() const { return bindingTable(); }
1055 const Binding *bindingsEnd() const { return bindingTable() + nBindings; }
1056 int bindingCount() const { return nBindings; }
1057
1058 const Property *propertiesBegin() const { return propertyTable(); }
1059 const Property *propertiesEnd() const { return propertyTable() + nProperties; }
1060
1061 const Alias *aliasesBegin() const { return aliasTable(); }
1062 const Alias *aliasesEnd() const { return aliasTable() + nAliases; }
1063
1065 EnumIterator enumsBegin() const { return EnumIterator(this, 0); }
1066 EnumIterator enumsEnd() const { return EnumIterator(this, nEnums); }
1067
1069 SignalIterator signalsBegin() const { return SignalIterator(this, 0); }
1070 SignalIterator signalsEnd() const { return SignalIterator(this, nSignals); }
1071
1073 InlineComponentIterator inlineComponentsBegin() const {return InlineComponentIterator(this, 0);}
1074 InlineComponentIterator inlineComponentsEnd() const {return InlineComponentIterator(this, nInlineComponents);}
1075
1077 RequiredPropertyExtraDataIterator requiredPropertyExtraDataBegin() const {return RequiredPropertyExtraDataIterator(this, 0); }
1078 RequiredPropertyExtraDataIterator requiredPropertyExtraDataEnd() const {return RequiredPropertyExtraDataIterator(this, nRequiredPropertyExtraData); }
1079};
1080static_assert(sizeof(Object) == 76, "Object structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
1081
1083{
1084 enum ImportType : unsigned int {
1089 };
1091
1094
1098
1100 {
1101 type = 0; uriIndex = 0; qualifierIndex = 0; version = QTypeRevision::zero(); reserved = 0;
1102 }
1103};
1104static_assert(sizeof(Import) == 20, "Import structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
1105
1107{
1112
1113 const Import *importAt(int idx) const {
1114 return reinterpret_cast<const Import*>((reinterpret_cast<const char *>(this)) + offsetToImports + idx * sizeof(Import));
1115 }
1116
1117 const Object *objectAt(int idx) const {
1118 const quint32_le *offsetTable = reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToObjects);
1119 const quint32_le offset = offsetTable[idx];
1120 return reinterpret_cast<const Object*>(reinterpret_cast<const char*>(this) + offset);
1121 }
1122};
1123static_assert(sizeof(QmlUnit) == 16, "QmlUnit structure needs to have the expected size to be binary compatible on disk when generated by host compiler and loaded by target");
1124
1125static const char magic_str[] = "qv4cdata";
1126
1127struct Unit
1128{
1129 // DO NOT CHANGE THESE FIELDS EVER
1130 char magic[8];
1132 // END DO NOT CHANGE THESE FIELDS EVER
1133
1134 quint32_le reserved; // For predictable alignment and size. Used to be Qt version.
1135
1136 // A sourceTimeStamp of -1 marks a unit that is validated by source content hashing rather
1137 // than by time stamp. In that case sourceChecksum holds the checksum of the source code and
1138 // is compared against the freshly computed checksum of the source on load. This is used for
1139 // cache files generated at run time, where the source's time stamp may be unreliable (e.g.
1140 // reproducible builds that pin mtimes via SOURCE_DATE_EPOCH). Units generated by qmlcachegen
1141 // at compile time keep their actual time stamp and leave sourceChecksum zeroed.
1143 quint32_le unitSize; // Size of the Unit and any depending data.
1144
1145 // Kept before md5Checksum, and therefore outside its coverage, so that it can be patched in
1146 // when saving a unit to disk without invalidating md5Checksum.
1148
1149 char md5Checksum[16]; // checksum of all bytes following this field.
1150
1151 enum : unsigned int {
1153 StaticData = 0x2, // Unit data persistent in memory?
1155 IsSharedLibrary = 0x8, // .pragma shared?
1157 PendingTypeCompilation = 0x20, // the QML data structures present are incomplete and require type compilation
1158 IsStrict = 0x40,
1169 };
1204
1206
1209
1210 /* QML specific fields */
1211
1212 const QmlUnit *qmlUnit() const {
1213 return reinterpret_cast<const QmlUnit *>(reinterpret_cast<const char *>(this) + offsetToQmlUnit);
1214 }
1215
1217 return reinterpret_cast<QmlUnit *>(reinterpret_cast<char *>(this) + offsetToQmlUnit);
1218 }
1219
1221 {
1222 return runtimeDataSize
1223 ? reinterpret_cast<const RuntimeDataBlock *>(
1224 reinterpret_cast<const char *>(this) + offsetToRuntimeData)
1225 : nullptr;
1226 }
1227
1228 bool isSingleton() const {
1229 return flags & Unit::IsSingleton;
1230 }
1231
1233 // ListPropertyAssignReplace is the combination of both replace bits, so it must be tested
1234 // for an exact match.
1235 return (flags & Unit::ListPropertyAssignReplace) == Unit::ListPropertyAssignReplace;
1236 }
1237
1239 return flags & Unit::ListPropertyAssignReplaceIfNotDefault;
1240 }
1241 /* end QML specific fields*/
1242
1243 QString stringAtInternal(uint idx) const {
1244 Q_ASSERT(idx < stringTableSize);
1245 const quint32_le *offsetTable = reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToStringTable);
1246 const quint32_le offset = offsetTable[idx];
1247 const String *str = reinterpret_cast<const String*>(reinterpret_cast<const char *>(this) + offset);
1248 Q_ASSERT(str->size >= 0);
1249#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
1250 const QChar *characters = reinterpret_cast<const QChar *>(str + 1);
1251 if (flags & StaticData)
1252 return QString::fromRawData(characters, str->size);
1253 return QString(characters, str->size);
1254#else
1255 const quint16_le *characters = reinterpret_cast<const quint16_le *>(str + 1);
1256 QString qstr(str->size, Qt::Uninitialized);
1257 QChar *ch = qstr.data();
1258 for (int i = 0; i < str->size; ++i)
1259 ch[i] = QChar(quint16(characters[i]));
1260 return qstr;
1261#endif
1262 }
1263
1264 const quint32_le *functionOffsetTable() const { return reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToFunctionTable); }
1265 const quint32_le *classOffsetTable() const { return reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToClassTable); }
1266 const quint32_le *templateObjectOffsetTable() const { return reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToTemplateObjectTable); }
1267 const quint32_le *blockOffsetTable() const { return reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToBlockTable); }
1268
1269 const Function *functionAt(int idx) const {
1270 const quint32_le *offsetTable = functionOffsetTable();
1271 const quint32_le offset = offsetTable[idx];
1272 return reinterpret_cast<const Function*>(reinterpret_cast<const char *>(this) + offset);
1273 }
1274
1275 const Class *classAt(int idx) const {
1276 const quint32_le *offsetTable = classOffsetTable();
1277 const quint32_le offset = offsetTable[idx];
1278 return reinterpret_cast<const Class *>(reinterpret_cast<const char *>(this) + offset);
1279 }
1280
1281 const TemplateObject *templateObjectAt(int idx) const {
1282 const quint32_le *offsetTable = templateObjectOffsetTable();
1283 const quint32_le offset = offsetTable[idx];
1284 return reinterpret_cast<const TemplateObject *>(reinterpret_cast<const char *>(this) + offset);
1285 }
1286
1287 const Block *blockAt(int idx) const {
1288 const quint32_le *offsetTable = blockOffsetTable();
1289 const quint32_le offset = offsetTable[idx];
1290 return reinterpret_cast<const Block *>(reinterpret_cast<const char *>(this) + offset);
1291 }
1292
1293 const Lookup *lookupTable() const { return reinterpret_cast<const Lookup*>(reinterpret_cast<const char *>(this) + offsetToLookupTable); }
1294 const RegExp *regexpAt(int index) const {
1295 return reinterpret_cast<const RegExp*>(reinterpret_cast<const char *>(this) + offsetToRegexpTable + index * sizeof(RegExp));
1296 }
1297 const quint64_le *constants() const {
1298 return reinterpret_cast<const quint64_le*>(reinterpret_cast<const char *>(this) + offsetToConstantTable);
1299 }
1300
1301 const JSClassMember *jsClassAt(int idx, int *nMembers) const {
1302 const quint32_le *offsetTable = reinterpret_cast<const quint32_le *>(reinterpret_cast<const char *>(this) + offsetToJSClassTable);
1303 const quint32_le offset = offsetTable[idx];
1304 const char *ptr = reinterpret_cast<const char *>(this) + offset;
1305 const JSClass *klass = reinterpret_cast<const JSClass *>(ptr);
1306 *nMembers = klass->nMembers;
1307 return reinterpret_cast<const JSClassMember*>(ptr + sizeof(JSClass));
1308 }
1309
1311 return reinterpret_cast<const TranslationData *>(reinterpret_cast<const char *>(this) + offsetToTranslationTable);
1312 }
1313
1315 if ( translationTableSize == 0)
1316 return nullptr;
1317 return reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this))
1318 + offsetToTranslationTable
1319 + translationTableSize * sizeof(CompiledData::TranslationData)); }
1320
1322 if ( translationTableSize == 0)
1323 return nullptr;
1324 return reinterpret_cast<quint32_le*>((reinterpret_cast<char *>(this))
1325 + offsetToTranslationTable
1326 + translationTableSize * sizeof(CompiledData::TranslationData)); }
1327
1328 const ImportEntry *importEntryTable() const { return reinterpret_cast<const ImportEntry *>(reinterpret_cast<const char *>(this) + offsetToImportEntryTable); }
1329 const ExportEntry *localExportEntryTable() const { return reinterpret_cast<const ExportEntry *>(reinterpret_cast<const char *>(this) + offsetToLocalExportEntryTable); }
1330 const ExportEntry *indirectExportEntryTable() const { return reinterpret_cast<const ExportEntry *>(reinterpret_cast<const char *>(this) + offsetToIndirectExportEntryTable); }
1331 const ExportEntry *starExportEntryTable() const { return reinterpret_cast<const ExportEntry *>(reinterpret_cast<const char *>(this) + offsetToStarExportEntryTable); }
1332
1333 const quint32_le *moduleRequestTable() const { return reinterpret_cast<const quint32_le*>((reinterpret_cast<const char *>(this)) + offsetToModuleRequestTable); }
1334
1335 bool verifyHeader(QDateTime expectedSourceTimeStamp, QString *errorString) const;
1336};
1337
1338static_assert(sizeof(Unit) == 208,
1339 "Unit structure needs to have the expected size to be binary compatible on disk when "
1340 "generated by host compiler and loaded by target");
1341
1342// The runtime data holds what we can only calculate once the types a unit refers to are resolved.
1343// It is stored separately from the unit, in tables parallel to the unit's objects and bindings.
1359static_assert(sizeof(RuntimeBinding) == 8,
1360 "RuntimeBinding structure needs to have the expected size to be binary compatible on "
1361 "disk when generated by host compiler and loaded by target");
1362
1364
1366{
1373
1379
1381 {
1382 return reinterpret_cast<const RuntimeBinding *>(
1383 reinterpret_cast<const char *>(this) + sizeof(RuntimeObject));
1384 }
1385
1387 {
1388 return reinterpret_cast<RuntimeBinding *>(
1389 reinterpret_cast<char *>(this) + sizeof(RuntimeObject));
1390 }
1391
1393 {
1394 return reinterpret_cast<const RuntimeBindingIndex *>(
1395 reinterpret_cast<const char *>(this) + offsetToBindingOrder);
1396 }
1397
1399 {
1400 return reinterpret_cast<RuntimeBindingIndex *>(
1401 reinterpret_cast<char *>(this) + offsetToBindingOrder);
1402 }
1403
1405 {
1406 return reinterpret_cast<const quint32_le *>(
1407 reinterpret_cast<const char *>(this) + offsetToNamedObjectsInComponent);
1408 }
1409
1411 {
1412 return reinterpret_cast<quint32_le *>(
1413 reinterpret_cast<char *>(this) + offsetToNamedObjectsInComponent);
1414 }
1415};
1416static_assert(sizeof(RuntimeObject) == 20,
1417 "RuntimeObject structure needs to have the expected size to be binary compatible on "
1418 "disk when generated by host compiler and loaded by target");
1419
1422{
1425
1426 const RuntimeObject *objectAt(int idx) const
1427 {
1428 const quint32_le *offsetTable = reinterpret_cast<const quint32_le *>(
1429 reinterpret_cast<const char *>(this) + sizeof(RuntimeDataBlock));
1430 return reinterpret_cast<const RuntimeObject *>(
1431 reinterpret_cast<const char *>(this) + offsetTable[idx]);
1432 }
1433
1435 {
1436 const quint32_le *offsetTable = reinterpret_cast<const quint32_le *>(
1437 reinterpret_cast<const char *>(this) + sizeof(RuntimeDataBlock));
1438 return reinterpret_cast<RuntimeObject *>(
1439 reinterpret_cast<char *>(this) + offsetTable[idx]);
1440 }
1441
1442 bool verifyChecksum(const CompiledData::DependentTypesHasher &dependencyHasher) const;
1443};
1444static_assert(sizeof(RuntimeDataBlock) == 20,
1445 "RuntimeDataBlock structure needs to have the expected size to be binary compatible "
1446 "on disk when generated by host compiler and loaded by target");
1447
1449{
1451 : location(loc)
1452 , needsCreation(false)
1453 , errorWhenNotFound(false)
1454 {}
1455 Location location; // first use
1456 bool needsCreation : 1; // whether the type needs to be creatable or not
1458};
1459
1460// Map from name index to location of first use.
1462{
1463 TypeReference &add(int nameIndex, const Location &loc) {
1464 Iterator it = find(nameIndex);
1465 if (it != end())
1466 return *it;
1467 return *insert(nameIndex, loc);
1468 }
1469
1470 template <typename CompiledObject>
1471 void collectFromObject(const CompiledObject *obj)
1472 {
1473 if (obj->inheritedTypeNameIndex != 0) {
1474 TypeReference &r = this->add(obj->inheritedTypeNameIndex, obj->location);
1475 r.needsCreation = true;
1476 r.errorWhenNotFound = true;
1477 }
1478
1479 auto binding = obj->bindingsBegin();
1480 auto const bindingEnd = obj->bindingsEnd();
1481 for ( ; binding != bindingEnd; ++binding) {
1483 this->add(binding->propertyNameIndex, binding->location);
1484 }
1485
1486 auto ic = obj->inlineComponentsBegin();
1487 auto const icEnd = obj->inlineComponentsEnd();
1488 for (; ic != icEnd; ++ic) {
1489 this->add(ic->nameIndex, ic->location);
1490 }
1491 }
1492
1493 template <typename Iterator>
1494 void collectFromObjects(Iterator it, Iterator end)
1495 {
1496 for (; it != end; ++it)
1497 collectFromObject(*it);
1498 }
1499};
1500
1502
1504 InlineComponentData(const QQmlType &qmlType, int objectIndex, int nameIndex)
1505 : qmlType(qmlType)
1506 , objectIndex(objectIndex)
1507 , nameIndex(nameIndex)
1508 {}
1509
1510 QQmlType qmlType;
1511 int objectIndex = -1;
1512 int nameIndex = -1;
1513};
1514
1516{
1518
1519 const Unit *data = nullptr;
1520 const QmlUnit *qmlData = nullptr;
1523
1527
1528 // pointers either to data->constants() or little-endian memory copy.
1529 const StaticValue *constants = nullptr;
1530
1532
1534
1535 // index is object index. This allows fast access to the
1536 // property data when initializing bindings, avoiding expensive
1537 // lookups by string (property name).
1539
1542
1544
1545 QQmlType qmlType;
1546
1548
1549public:
1550 // --- interface for QQmlPropertyCacheCreator
1554
1555 using IdToObjectMap = QMap<int, int>;
1556
1557 explicit CompilationUnit(const Unit *unitData,
1558 const QQmlPrivate::AOTCompiledFunction *aotCompiledFunctions,
1559 LookupValidationFn validateLookupSignatures,
1560 const QString &fileName = QString(),
1561 const QString &finalUrlString = QString())
1562 : CompilationUnit(unitData, fileName, finalUrlString)
1563 {
1564 this->aotCompiledFunctions = aotCompiledFunctions;
1565 this->validateLookupSignatures = validateLookupSignatures;
1566 }
1567
1569 const Unit *unitData = nullptr, const QString &fileName = QString(),
1570 const QString &finalUrlString = QString());
1571
1573
1574 const Unit *unitData() const { return data; }
1575
1576 void setUnitData(const Unit *unitData, const QmlUnit *qmlUnit = nullptr,
1577 const QString &fileName = QString(), const QString &finalUrlString = QString())
1578 {
1579 data = unitData;
1580 qmlData = nullptr;
1581#if Q_BYTE_ORDER == Q_BIG_ENDIAN
1582 delete [] constants;
1583#endif
1584 constants = nullptr;
1585 m_fileName.clear();
1586 m_finalUrlString.clear();
1587 if (!data)
1588 return;
1589
1590 qmlData = qmlUnit ? qmlUnit : data->qmlUnit();
1591
1592#if Q_BYTE_ORDER == Q_BIG_ENDIAN
1593 StaticValue *bigEndianConstants = new StaticValue[data->constantTableSize];
1594 const quint64_le *littleEndianConstants = data->constants();
1595 for (uint i = 0; i < data->constantTableSize; ++i)
1596 bigEndianConstants[i] = StaticValue::fromReturnedValue(littleEndianConstants[i]);
1597 constants = bigEndianConstants;
1598#else
1599 constants = reinterpret_cast<const StaticValue*>(data->constants());
1600#endif
1601
1602 m_fileName = !fileName.isEmpty() ? fileName : stringAt(data->sourceFileIndex);
1603 m_finalUrlString = !finalUrlString.isEmpty() ? finalUrlString : stringAt(data->finalUrlIndex);
1604 }
1605
1606 QString stringAt(uint index) const
1607 {
1608 if (index < data->stringTableSize)
1609 return data->stringAtInternal(index);
1610
1611 const qsizetype dynamicIndex = index - data->stringTableSize;
1612 Q_ASSERT(dynamicIndex < dynamicStrings.size());
1613 return dynamicStrings.at(dynamicIndex);
1614 }
1615
1616 QString fileName() const { return m_fileName; }
1617 QString finalUrlString() const { return m_finalUrlString; }
1618
1620 {
1621 using namespace CompiledData;
1622 switch (binding->type()) {
1623 case Binding::Type_Script:
1624 case Binding::Type_String:
1625 return stringAt(binding->stringIndex);
1626 case Binding::Type_Null:
1627 return QStringLiteral("null");
1629 return binding->value.b ? QStringLiteral("true") : QStringLiteral("false");
1630 case Binding::Type_Number:
1631 return QString::number(bindingValueAsNumber(binding), 'g', QLocale::FloatingPointShortest);
1633 return QString();
1634 case Binding::Type_TranslationById:
1635 case Binding::Type_Translation:
1636 return stringAt(data->translations()[binding->value.translationDataIndex].stringIndex);
1637 default:
1638 break;
1639 }
1640 return QString();
1641 }
1642
1644 {
1646 ? CompiledData::Binding::escapedString(stringAt(binding->stringIndex))
1647 : bindingValueAsString(binding);
1648 }
1649
1650 double bindingValueAsNumber(const CompiledData::Binding *binding) const
1651 {
1653 return 0.0;
1654 return constants[binding->value.constantValueIndex].doubleValue();
1655 }
1656
1657 Q_QML_EXPORT static QString localCacheFilePath(const QUrl &url);
1658
1659 // sourceChecksum is invoked to obtain the checksum of the current source code. It is only
1660 // called when the cached unit is in content-hash mode (sourceTimeStamp == -1), so units
1661 // validated by time stamp never pay the cost of reading and hashing the source.
1663 const QUrl &url, const QDateTime &sourceTimeStamp,
1665
1666 // saveToDisk always stores the unit in content-hash mode: it sets sourceTimeStamp to -1 and
1667 // writes the result of sourceChecksum() into the unit. It is used for cache files generated
1668 // at run time.
1670 const QUrl &unitUrl, qxp::function_ref<QByteArray() const> sourceChecksum,
1671 QString *errorString) const;
1672
1673 bool persistRuntimeData(const QUrl &unitUrl, QString *errorString);
1674 bool writeWithRuntimeData(const QUrl &unitUrl, Unit *header, QString *errorString) const;
1675
1676 int importCount() const { return qmlData->nImports; }
1677 const CompiledData::Import *importAt(int index) const { return qmlData->importAt(index); }
1678
1679 Q_QML_EXPORT QStringList moduleRequests() const;
1680
1681 // url() and fileName() shall be used to load the actual QML/JS code or to show errors or
1682 // warnings about that code. They include any potential URL interceptions and thus represent the
1683 // "physical" location of the code.
1684 //
1685 // finalUrl() and finalUrlString() shall be used to resolve further URLs referred to in the code
1686 // They are _not_ intercepted and thus represent the "logical" name for the code.
1687
1688 QUrl url() const
1689 {
1690 if (!m_url.isValid())
1691 m_url = QUrl(fileName());
1692 return m_url;
1693 }
1694
1696 {
1697 if (!m_finalUrl.isValid())
1698 m_finalUrl = QUrl(finalUrlString());
1699 return m_finalUrl;
1700 }
1701
1702 ResolvedTypeReference *resolvedType(int id) const { return resolvedTypes.value(id); }
1703 ResolvedTypeReference *resolvedType(QMetaType type) const;
1704
1706 {
1707 return propertyCaches.isEmpty()
1708 ? QQmlPropertyCache::ConstPtr()
1709 : propertyCaches.at(/*root object*/0);
1710 }
1711
1712 int objectCount() const { return qmlData->nObjects; }
1713
1714 // Resolves implicit component wrapper indices (>= objectCount()) to the
1715 // wrapped child object index. Returns the index unchanged for real objects.
1716 int resolvedIndex(int index) const
1717 {
1718 if (index < objectCount())
1719 return index;
1720 const QQmlPropertyData *p = propertyCaches.at(index)->defaultProperty();
1721 Q_ASSERT(p && p->isComponentWrapper());
1722 return p->wrappedObjectIndex();
1723 }
1724
1725 const CompiledObject *objectAt(int index) const
1726 {
1728 }
1729
1730 const RuntimeObject *runtimeObjectAt(int index) const
1731 {
1732 Q_ASSERT(runtimeData);
1734 }
1735
1736 bool objectHasDeferredBindings(int objectIndex) const
1737 {
1738 return runtimeObjectAt(objectIndex)->flags & RuntimeObject::HasDeferredBindings;
1739 }
1740
1741 bool objectHasCustomParserBindings(int objectIndex) const
1742 {
1744 }
1745
1746 bool objectIsPopulatedByDeferredBinding(int objectIndex) const
1747 {
1749 }
1750
1751 bool objectIsComponent(int objectIndex) const
1752 {
1753 return runtimeObjectAt(objectIndex)->flags & RuntimeObject::IsComponent;
1754 }
1755
1756 int objectId(int objectIndex) const { return runtimeObjectAt(objectIndex)->objectId; }
1757
1759 {
1760 return runtimeObjectAt(objectIndex)->nNamedObjectsInComponent;
1761 }
1762
1763 bool bindingIsDeferred(int objectIndex, int bindingIndex) const
1764 {
1765 return runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex].flags
1767 }
1768
1769 bool bindingIsCustomParser(int objectIndex, int bindingIndex) const
1770 {
1771 return runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex].flags
1773 }
1774
1775 bool bindingIsSignalHandlerExpression(int objectIndex, int bindingIndex) const
1776 {
1777 return runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex].flags
1779 }
1780
1781 bool bindingIsSignalHandlerObject(int objectIndex, int bindingIndex) const
1782 {
1783 return runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex].flags
1785 }
1786
1787 bool bindingIsPropertyObserver(int objectIndex, int bindingIndex) const
1788 {
1789 return runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex].flags
1791 }
1792
1793 bool bindingIsSignalHandler(int objectIndex, int bindingIndex) const
1794 {
1795 return runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex].flags
1798 }
1799
1800 std::optional<int> bindingFoldedEnum(int objectIndex, int bindingIndex) const
1801 {
1802 const RuntimeBinding &binding = runtimeObjectAt(objectIndex)->bindingTable()[bindingIndex];
1803 if (binding.flags & RuntimeBinding::IsFoldedEnum)
1804 return int(binding.enumValue);
1805 return std::nullopt;
1806 }
1807
1808 const RuntimeBindingIndex *bindingApplicationOrder(int objectIndex) const
1809 {
1810 return runtimeObjectAt(objectIndex)->bindingOrder();
1811 }
1812
1813 void allocateRuntimeData();
1814 quint32_le *allocateNamedObjectsInComponent(int objectIndex, quint32 count);
1815
1817 {
1818 runtimeData = nullptr;
1819 runtimeDataStorage.clear();
1820 }
1821
1822 bool hasImmutableRuntimeData() const { return runtimeData && runtimeDataStorage.isEmpty(); }
1823
1825 {
1826 return runtimeDataStorage.isEmpty()
1827 ? nullptr
1828 : reinterpret_cast<RuntimeDataBlock *>(runtimeDataStorage.data());
1829 }
1830
1831 // Returns the index of the implicit component wrapper, or -1.
1832 // Scans property caches at indices >= objectCount().
1833 int implicitComponentForObject(int index) const
1834 {
1835 for (int i = objectCount(), end = propertyCaches.count(); i < end; ++i) {
1836 const QQmlPropertyData *p = propertyCaches.at(i)->defaultProperty();
1837 Q_ASSERT(p && p->isComponentWrapper());
1838 if (p->wrappedObjectIndex() == index)
1839 return i;
1840 }
1841 return -1;
1842 }
1843
1844 int inlineComponentId(const QString &inlineComponentName) const
1845 {
1846 for (uint i = 0; i < qmlData->nObjects; ++i) {
1847 auto *object = qmlData->objectAt(i);
1848 for (auto it = object->inlineComponentsBegin(), end = object->inlineComponentsEnd();
1849 it != end; ++it) {
1850 if (stringAt(it->nameIndex) == inlineComponentName)
1851 return it->objectIndex;
1852 }
1853 }
1854 return -1;
1855 }
1856
1857 void finalizeCompositeType(const QQmlType &type);
1858
1869
1874
1879
1881 {
1883 }
1884
1889
1891 {
1893 }
1894
1896 {
1898 }
1899
1900 bool isESModule() const
1901 {
1903 }
1904
1905 bool isSharedLibrary() const
1906 {
1908 }
1909
1911 {
1912 FunctionIterator(const CompiledData::Unit *unit, const CompiledObject *object, int index)
1913 : unit(unit), object(object), index(index) {}
1917
1919 {
1920 return unit->functionAt(object->functionOffsetTable()[index]);
1921 }
1922
1923 void operator++() { ++index; }
1924 bool operator==(const FunctionIterator &rhs) const { return index == rhs.index; }
1925 bool operator!=(const FunctionIterator &rhs) const { return index != rhs.index; }
1926 };
1927
1929 {
1930 return FunctionIterator(unitData(), object, 0);
1931 }
1932
1934 {
1935 return FunctionIterator(unitData(), object, object->nFunctions);
1936 }
1937
1938 QQmlType qmlTypeForComponent(const QString &inlineComponentName = QString()) const;
1939 QMetaType metaType() const { return qmlType.typeId(); }
1940
1941private:
1942 QString m_fileName; // initialized from data->sourceFileIndex
1943 QString m_finalUrlString; // initialized from data->finalUrlIndex
1944
1945 mutable QQmlNullableValue<QUrl> m_url;
1946 mutable QQmlNullableValue<QUrl> m_finalUrl;
1947};
1948
1950{
1952public:
1958
1960
1961 template<typename Char>
1962 bool saveToDisk(const std::function<bool(const Char *, quint32)> &writer) const
1963 {
1964 const quint32_le oldFlags = mutableFlags();
1965 auto cleanup = qScopeGuard([this, oldFlags]() { mutableFlags() = oldFlags; });
1966 mutableFlags() |= temporaryFlags;
1967 return writer(data<Char>(), size());
1968 }
1969
1970 static bool writeDataToFile(const QString &outputFileName, const char *data, quint32 size,
1971 QString *errorString)
1972 {
1973 return writeDataToFile(outputFileName, nullptr, 0, data, 0, size, nullptr, size, 0,
1974 errorString);
1975 }
1976
1977 // Writes data, but with its first headSize bytes replaced by head. After that, writes zero
1978 // padding up to tailOffset, then tail. tailOffset is relative to the start of the file and
1979 // must not be smaller than size.
1980 static bool writeDataToFile(const QString &outputFileName,
1981 const char *head, quint32 headSize,
1982 const char *data, quint32 offset, quint32 size,
1983 const char *tail, quint32 tailOffset, quint32 tailSize,
1984 QString *errorString)
1985 {
1986#if QT_CONFIG(temporaryfile)
1987 Q_ASSERT(headSize <= offset);
1988 Q_ASSERT(tailOffset >= offset + size);
1989 QSaveFile cacheFile(outputFileName);
1990 if (!cacheFile.open(QIODevice::WriteOnly | QIODevice::Truncate)
1991 || (headSize > 0 && cacheFile.write(head, headSize) != headSize)
1992 || (size > 0
1993 && ((offset > headSize
1994 && (cacheFile.write(QByteArray(offset - headSize, '\0'))
1995 != offset - headSize))
1996 || cacheFile.write(data, size) != size))
1997 || (tailSize > 0
1998 && ((tailOffset > (offset + size)
1999 && cacheFile.write(QByteArray(tailOffset - size - offset, '\0'))
2000 != tailOffset - size - offset)
2001 || cacheFile.write(tail, tailSize) != tailSize))
2002 || !cacheFile.commit()) {
2003 *errorString = cacheFile.errorString();
2004 return false;
2005 }
2006
2007 errorString->clear();
2008 return true;
2009#else
2010 Q_UNUSED(outputFileName);
2011 *errorString = QStringLiteral("features.temporaryfile is disabled.");
2012 return false;
2013#endif
2014 }
2015
2016private:
2017 const Unit *unit;
2018 quint32 temporaryFlags;
2019
2020 quint32_le &mutableFlags() const
2021 {
2022 return const_cast<Unit *>(unit)->flags;
2023 }
2024
2025 template<typename Char>
2026 const Char *data() const
2027 {
2028 Q_STATIC_ASSERT(sizeof(Char) == 1);
2029 const Char *dataPtr;
2030 memcpy(&dataPtr, &unit, sizeof(dataPtr));
2031 return dataPtr;
2032 }
2033
2034 quint32 size() const
2035 {
2036 return unit->unitSize;
2037 }
2038};
2039
2040
2041} // CompiledData namespace
2042} // QV4 namespace
2043
2046
2047QT_END_NAMESPACE
2048
2049#endif
static bool writeDataToFile(const QString &outputFileName, const char *head, quint32 headSize, const char *data, quint32 offset, quint32 size, const char *tail, quint32 tailOffset, quint32 tailSize, QString *errorString)
static bool writeDataToFile(const QString &outputFileName, const char *data, quint32 size, QString *errorString)
bool saveToDisk(const std::function< bool(const Char *, quint32)> &writer) const
Combined button and popup list for selecting options.
static quint32 alignedTo(quint32 size)
static const char magic_str[]
Definition qjsvalue.h:24
Q_DECLARE_TYPEINFO(QV4::CompiledData::JSClassMember, Q_PRIMITIVE_TYPE)
#define QV4_DATA_STRUCTURE_VERSION
Q_DECLARE_OPERATORS_FOR_FLAGS(QV4::CompiledData::ParameterType::Flags)
void setPropertyNameIndex(quint32 propertyNameIndex)
quint32 propertyNameIndex() const
void setIsReadOnly(bool isReadOnly)
void setReferenceLocation(Location referenceLocation)
void setNameIndex(quint32 nameIndex)
Location referenceLocation() const
void setIdIndex(quint32 idIndex)
void setLocation(Location location)
quint32_le_bitfield_union< FlagsField, TypeField > flagsAndType
bool hasFlag(Flag flag) const
static QString escapedString(const QString &string)
const quint32_le * localsTable() const
static size_t align(size_t a)
static int calculateSize(int nLocals)
static int calculateSize(int nStaticMethods, int nMethods)
static size_t align(size_t a)
const Method * methodTable() const
bool operator!=(const FunctionIterator &rhs) const
bool operator==(const FunctionIterator &rhs) const
FunctionIterator(const CompiledData::Unit *unit, const CompiledObject *object, int index)
std::unique_ptr< CompilationUnitMapper > backingFile
QString bindingValueAsScriptString(const CompiledData::Binding *binding) const
QHash< QString, InlineComponentData > inlineComponentData
void setUnitData(const Unit *unitData, const QmlUnit *qmlUnit=nullptr, const QString &fileName=QString(), const QString &finalUrlString=QString())
int inlineComponentId(const QString &inlineComponentName) const
quint32 namedObjectCountInComponent(int objectIndex) const
double bindingValueAsNumber(const CompiledData::Binding *binding) const
std::optional< int > bindingFoldedEnum(int objectIndex, int bindingIndex) const
bool bindingIsDeferred(int objectIndex, int bindingIndex) const
void finalizeCompositeType(const QQmlType &type)
bool persistRuntimeData(const QUrl &unitUrl, QString *errorString)
ResolvedTypeReference * resolvedType(int id) const
const CompiledData::Function CompiledFunction
const QQmlPrivate::AOTCompiledFunction * aotCompiledFunctions
quint32_le * allocateNamedObjectsInComponent(int objectIndex, quint32 count)
bool bindingIsSignalHandlerExpression(int objectIndex, int bindingIndex) const
bool bindingIsPropertyObserver(int objectIndex, int bindingIndex) const
int objectId(int objectIndex) const
bool writeWithRuntimeData(const QUrl &unitUrl, Unit *header, QString *errorString) const
int implicitComponentForObject(int index) const
bool bindingIsCustomParser(int objectIndex, int bindingIndex) const
bool bindingIsSignalHandlerObject(int objectIndex, int bindingIndex) const
ResolvedTypeReferenceMap resolvedTypes
bool objectHasCustomParserBindings(int objectIndex) const
QList< BindingPropertyData > bindingPropertyDataPerObject
bool objectIsPopulatedByDeferredBinding(int objectIndex) const
QQmlType qmlTypeForComponent(const QString &inlineComponentName=QString()) const
FunctionIterator objectFunctionsBegin(const CompiledObject *object) const
QQmlRefPointer< QQmlTypeNameCache > typeNameCache
Q_QML_EXPORT bool saveToDisk(const QUrl &unitUrl, qxp::function_ref< QByteArray() const > sourceChecksum, QString *errorString) const
const RuntimeObject * runtimeObjectAt(int index) const
FunctionIterator objectFunctionsEnd(const CompiledObject *object) const
ListPropertyAssignBehavior listPropertyAssignBehavior() const
const CompiledObject * objectAt(int index) const
const CompiledData::Object CompiledObject
const CompiledData::Binding CompiledBinding
const CompiledData::Import * importAt(int index) const
bool objectIsComponent(int objectIndex) const
CompilationUnit(const Unit *unitData, const QQmlPrivate::AOTCompiledFunction *aotCompiledFunctions, LookupValidationFn validateLookupSignatures, const QString &fileName=QString(), const QString &finalUrlString=QString())
QList< QQmlRefPointer< QQmlScriptData > > dependentScripts
bool bindingIsSignalHandler(int objectIndex, int bindingIndex) const
QString bindingValueAsString(const CompiledData::Binding *binding) const
Q_QML_EXPORT bool loadFromDisk(const QUrl &url, const QDateTime &sourceTimeStamp, qxp::function_ref< QByteArray() const > sourceChecksum, QString *errorString)
QQmlPropertyCache::ConstPtr rootPropertyCache() const
bool objectHasDeferredBindings(int objectIndex) const
const RuntimeBindingIndex * bindingApplicationOrder(int objectIndex) const
const EnumValue * enumValueAt(int idx) const
const EnumValue * enumValuesEnd() const
const EnumValue * enumValuesBegin() const
static int calculateSize(int nEnumValues)
const quint32_le * localsTable() const
static size_t align(size_t a)
const CodeOffsetToLineAndStatement * lineAndStatementNumberTable() const
size_t lineAndStatementNumberOffset() const
static int calculateSize(int nFormals, int nLocals, int nLinesAndStatements, int labelInfoSize, int codeSize)
const Parameter * formalsTable() const
const Parameter * formalsEnd() const
const Parameter * formalsBegin() const
const quint32_le * labelInfoTable() const
InlineComponentData(const QQmlType &qmlType, int objectIndex, int nameIndex)
void set(quint32 nameOffset, bool isAccessor)
static int calculateSize(int nMembers)
Location(quint32 l, quint32 c)
void set(quint32 line, quint32 column)
friend size_t qHash(const Location &location, size_t seed=0)
bool operator<(const Location &other) const
friend bool comparesEqual(const Location &a, const Location &b) noexcept
Lookup(Type type, Mode mode, quint32 nameIndex)
TableIterator< Enum, Object, &Object::enumAt > EnumIterator
EnumIterator enumsBegin() const
Q_DECLARE_FLAGS(Flags, Flag)
const RequiredPropertyExtraData * requiredPropertyExtraDataAt(int idx) const
EnumIterator enumsEnd() const
const Enum * enumAt(int idx) const
RequiredPropertyExtraDataIterator requiredPropertyExtraDataEnd() const
const RequiredPropertyExtraData * requiredPropertyExtraDataTable() const
TableIterator< Signal, Object, &Object::signalAt > SignalIterator
const Binding * bindingsEnd() const
bool hasFlag(Flag flag) const
const Property * propertyTable() const
SignalIterator signalsBegin() const
InlineComponentIterator inlineComponentsEnd() const
const Alias * aliasTable() const
static int calculateSizeExcludingSignalsAndEnums(int nFunctions, int nProperties, int nAliases, int nEnums, int nSignals, int nBindings, int nInlineComponents, int nRequiredPropertyExtraData)
const InlineComponent * inlineComponentAt(int idx) const
const Property * propertiesEnd() const
const Binding * bindingTable() const
const Signal * signalAt(int idx) const
const Property * propertiesBegin() const
const quint32_le * functionOffsetTable() const
TableIterator< InlineComponent, Object, &Object::inlineComponentAt > InlineComponentIterator
const InlineComponent * inlineComponentTable() const
InlineComponentIterator inlineComponentsBegin() const
TableIterator< RequiredPropertyExtraData, Object, &Object::requiredPropertyExtraDataAt > RequiredPropertyExtraDataIterator
SignalIterator signalsEnd() const
quint32_le_bitfield_union< FlagsField, DefaultPropertyIsAliasField > flagsAndDefaultPropertyIsAlias
const Alias * aliasesEnd() const
const Binding * bindingsBegin() const
RequiredPropertyExtraDataIterator requiredPropertyExtraDataBegin() const
void setHasAliasAsDefaultProperty(bool defaultAlias)
const Alias * aliasesBegin() const
void set(Flags flags, quint32 typeNameIndexOrCommonType)
void setNameIndex(int nameIndex)
void setIsReadOnly(bool isReadOnly)
void setTypeNameIndex(int nameIndex)
void setIsVirtual(bool isVirtual)
quint32_le_bitfield_union< CommonTypeOrTypeNameIndexField, IsRequiredField, IsCommonTypeField, IsListField, IsReadOnlyField > data
quint32_le_bitfield_union< NameIndexField, IsVirtualField, IsOverrideField, IsFinalField > nameIndexAndVirtSpecifiers
void setIsOverride(bool isOverride)
void setIsRequired(bool isRequired)
const Object * objectAt(int idx) const
const Import * importAt(int idx) const
RegExp(quint32 flags, quint32 stringIndex)
Q_DECLARE_FLAGS(Flags, Flag)
bool addToHash(QCryptographicHash *hash, QHash< quintptr, QByteArray > *checksums) const
const RuntimeObject * objectAt(int idx) const
bool verifyChecksum(const CompiledData::DependentTypesHasher &dependencyHasher) const
const RuntimeBindingIndex * bindingOrder() const
const RuntimeBinding * bindingTable() const
RuntimeBindingIndex * bindingOrder()
const quint32_le * namedObjectsInComponentTable() const
const Parameter * parametersBegin() const
static int calculateSize(int nParameters)
const Parameter * parametersEnd() const
const Parameter * parameterAt(int idx) const
static int calculateSize(const QString &str)
bool operator==(const TableIterator &rhs) const
bool operator!=(const TableIterator &rhs) const
TableIterator(const Container *container, int index)
TableIterator operator+(int offset) const
const ItemType * operator->() const
int operator-(TableIterator other) const
TableIterator operator-(int offset) const
const quint32_le * stringTable() const
void collectFromObject(const CompiledObject *obj)
TypeReference & add(int nameIndex, const Location &loc)
void collectFromObjects(Iterator it, Iterator end)
quint32_le offsetToIndirectExportEntryTable
quint32_le * translationContextIndex()
const TranslationData * translations() const
const ExportEntry * indirectExportEntryTable() const
const quint64_le * constants() const
QString stringAtInternal(uint idx) const
const quint32_le * functionOffsetTable() const
bool isListPropertyAssignReplaceIfNotDefault() const
bool verifyHeader(QDateTime expectedSourceTimeStamp, QString *errorString) const
const Function * functionAt(int idx) const
const TemplateObject * templateObjectAt(int idx) const
const ExportEntry * starExportEntryTable() const
const Lookup * lookupTable() const
const RuntimeDataBlock * runtimeData() const
const RegExp * regexpAt(int index) const
const Block * blockAt(int idx) const
const ExportEntry * localExportEntryTable() const
const quint32_le * translationContextIndex() const
const quint32_le * classOffsetTable() const
const quint32_le * moduleRequestTable() const
bool isListPropertyAssignReplace() const
const Class * classAt(int idx) const
const ImportEntry * importEntryTable() const
const QmlUnit * qmlUnit() const
const quint32_le * blockOffsetTable() const
const JSClassMember * jsClassAt(int idx, int *nMembers) const
const quint32_le * templateObjectOffsetTable() const