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 | #ifndef QV4COMPILER_P_H |
4 | #define QV4COMPILER_P_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <QtCore/qstring.h> |
18 | #include <QtCore/qhash.h> |
19 | #include <QtCore/qstringlist.h> |
20 | #include <private/qv4compilerglobal_p.h> |
21 | #include <private/qqmljsastfwd_p.h> |
22 | #include <private/qv4compileddata_p.h> |
23 | #include <private/qv4staticvalue_p.h> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QQmlPropertyData; |
28 | |
29 | namespace QV4 { |
30 | |
31 | namespace CompiledData { |
32 | struct Unit; |
33 | struct Lookup; |
34 | struct RegExp; |
35 | struct JSClassMember; |
36 | } |
37 | |
38 | namespace Compiler { |
39 | |
40 | struct Context; |
41 | struct Module; |
42 | struct Class; |
43 | struct TemplateObject; |
44 | |
45 | struct Q_QML_COMPILER_PRIVATE_EXPORT StringTableGenerator { |
46 | StringTableGenerator(); |
47 | |
48 | int registerString(const QString &str); |
49 | int getStringId(const QString &string) const; |
50 | QString stringForIndex(int index) const { return strings.at(i: index); } |
51 | uint stringCount() const { return strings.size() - backingUnitTableSize; } |
52 | |
53 | uint sizeOfTableAndData() const { return stringDataSize + ((stringCount() * sizeof(uint) + 7) & ~7); } |
54 | |
55 | void freeze() { frozen = true; } |
56 | |
57 | void clear(); |
58 | |
59 | void initializeFromBackingUnit(const CompiledData::Unit *unit); |
60 | |
61 | void serialize(CompiledData::Unit *unit); |
62 | QStringList allStrings() const { return strings.mid(pos: backingUnitTableSize); } |
63 | |
64 | private: |
65 | QHash<QString, int> stringToId; |
66 | QStringList strings; |
67 | uint stringDataSize; |
68 | uint backingUnitTableSize = 0; |
69 | bool frozen = false; |
70 | }; |
71 | |
72 | struct Q_QML_COMPILER_PRIVATE_EXPORT JSUnitGenerator { |
73 | enum LookupMode { LookupForStorage, LookupForCall }; |
74 | |
75 | static void generateUnitChecksum(CompiledData::Unit *unit); |
76 | |
77 | struct MemberInfo { |
78 | QString name; |
79 | bool isAccessor; |
80 | }; |
81 | |
82 | JSUnitGenerator(Module *module); |
83 | |
84 | int registerString(const QString &str) { return stringTable.registerString(str); } |
85 | int getStringId(const QString &string) const { return stringTable.getStringId(string); } |
86 | QString stringForIndex(int index) const { return stringTable.stringForIndex(index); } |
87 | |
88 | int registerGetterLookup(const QString &name, LookupMode mode); |
89 | int registerGetterLookup(int nameIndex, LookupMode mode); |
90 | int registerSetterLookup(const QString &name); |
91 | int registerSetterLookup(int nameIndex); |
92 | int registerGlobalGetterLookup(int nameIndex, LookupMode mode); |
93 | int registerQmlContextPropertyGetterLookup(int nameIndex, LookupMode mode); |
94 | int lookupNameIndex(int index) const { return lookups[index].nameIndex(); } |
95 | QString lookupName(int index) const { return stringForIndex(index: lookupNameIndex(index)); } |
96 | |
97 | int registerRegExp(QQmlJS::AST::RegExpLiteral *regexp); |
98 | |
99 | int registerConstant(ReturnedValue v); |
100 | ReturnedValue constant(int idx) const; |
101 | |
102 | int registerJSClass(const QStringList &members); |
103 | |
104 | int registerTranslation(const CompiledData::TranslationData &translation); |
105 | |
106 | enum GeneratorOption { |
107 | GenerateWithStringTable, |
108 | GenerateWithoutStringTable |
109 | }; |
110 | |
111 | QV4::CompiledData::Unit *generateUnit(GeneratorOption option = GenerateWithStringTable); |
112 | void writeFunction(char *f, Context *irFunction) const; |
113 | void writeClass(char *f, const Class &c); |
114 | void writeTemplateObject(char *f, const TemplateObject &o); |
115 | void writeBlock(char *f, Context *irBlock) const; |
116 | |
117 | StringTableGenerator stringTable; |
118 | QString codeGeneratorName; |
119 | private: |
120 | CompiledData::Unit (GeneratorOption option, quint32_le *functionOffsets, uint *jsClassDataOffset); |
121 | |
122 | Module *module; |
123 | |
124 | QList<CompiledData::Lookup> lookups; |
125 | QVector<CompiledData::RegExp> regexps; |
126 | QVector<ReturnedValue> constants; |
127 | QByteArray jsClassData; |
128 | QVector<int> jsClassOffsets; |
129 | QVector<CompiledData::TranslationData> translations; |
130 | }; |
131 | |
132 | } |
133 | |
134 | } |
135 | |
136 | QT_END_NAMESPACE |
137 | |
138 | #endif |
139 | |