| 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_EXPORT StringTableGenerator { |
| 46 | StringTableGenerator(); |
| 47 | |
| 48 | int registerString(const QString &str); |
| 49 | int getStringId(const QString &string) const; |
| 50 | bool hasStringId(const QString &string) const { return stringToId.contains(key: string); } |
| 51 | QString stringForIndex(int index) const { return strings.at(i: index); } |
| 52 | uint stringCount() const { return strings.size() - backingUnitTableSize; } |
| 53 | |
| 54 | uint sizeOfTableAndData() const { return stringDataSize + ((stringCount() * sizeof(uint) + 7) & ~7); } |
| 55 | |
| 56 | void freeze() { frozen = true; } |
| 57 | |
| 58 | void clear(); |
| 59 | |
| 60 | void initializeFromBackingUnit(const CompiledData::Unit *unit); |
| 61 | |
| 62 | void serialize(CompiledData::Unit *unit); |
| 63 | QStringList allStrings() const { return strings.mid(pos: backingUnitTableSize); } |
| 64 | |
| 65 | private: |
| 66 | QHash<QString, int> stringToId; |
| 67 | QStringList strings; |
| 68 | uint stringDataSize; |
| 69 | uint backingUnitTableSize = 0; |
| 70 | bool frozen = false; |
| 71 | }; |
| 72 | |
| 73 | struct Q_QML_COMPILER_EXPORT JSUnitGenerator { |
| 74 | enum LookupMode { LookupForStorage, LookupForCall }; |
| 75 | |
| 76 | static void generateUnitChecksum(CompiledData::Unit *unit); |
| 77 | |
| 78 | struct MemberInfo { |
| 79 | QString name; |
| 80 | bool isAccessor; |
| 81 | }; |
| 82 | |
| 83 | JSUnitGenerator(Module *module); |
| 84 | |
| 85 | int registerString(const QString &str) { return stringTable.registerString(str); } |
| 86 | int getStringId(const QString &string) const { return stringTable.getStringId(string); } |
| 87 | bool hasStringId(const QString &string) const { return stringTable.hasStringId(string); } |
| 88 | QString stringForIndex(int index) const { return stringTable.stringForIndex(index); } |
| 89 | |
| 90 | int registerGetterLookup(const QString &name, LookupMode mode); |
| 91 | int registerGetterLookup(int nameIndex, LookupMode mode); |
| 92 | int registerSetterLookup(const QString &name); |
| 93 | int registerSetterLookup(int nameIndex); |
| 94 | int registerGlobalGetterLookup(int nameIndex, LookupMode mode); |
| 95 | int registerQmlContextPropertyGetterLookup(int nameIndex, LookupMode mode); |
| 96 | int lookupNameIndex(int index) const { return lookups[index].nameIndex(); } |
| 97 | QString lookupName(int index) const { return stringForIndex(index: lookupNameIndex(index)); } |
| 98 | |
| 99 | int registerRegExp(QQmlJS::AST::RegExpLiteral *regexp); |
| 100 | |
| 101 | int registerConstant(ReturnedValue v); |
| 102 | ReturnedValue constant(int idx) const; |
| 103 | |
| 104 | int registerJSClass(const QStringList &members); |
| 105 | int jsClassSize(int jsClassId) const; |
| 106 | QString jsClassMember(int jsClassId, int member) const; |
| 107 | |
| 108 | int registerTranslation(const CompiledData::TranslationData &translation); |
| 109 | |
| 110 | enum GeneratorOption { |
| 111 | GenerateWithStringTable, |
| 112 | GenerateWithoutStringTable |
| 113 | }; |
| 114 | |
| 115 | QV4::CompiledData::Unit *generateUnit(GeneratorOption option = GenerateWithStringTable); |
| 116 | void writeFunction(char *f, Context *irFunction) const; |
| 117 | void writeClass(char *f, const Class &c); |
| 118 | void writeTemplateObject(char *f, const TemplateObject &o); |
| 119 | void writeBlock(char *f, Context *irBlock) const; |
| 120 | |
| 121 | StringTableGenerator stringTable; |
| 122 | QString codeGeneratorName; |
| 123 | |
| 124 | private: |
| 125 | CompiledData::Unit (GeneratorOption option, quint32_le *functionOffsets, uint *jsClassDataOffset); |
| 126 | |
| 127 | Module *module; |
| 128 | |
| 129 | QList<CompiledData::Lookup> lookups; |
| 130 | QVector<CompiledData::RegExp> regexps; |
| 131 | QVector<ReturnedValue> constants; |
| 132 | QByteArray jsClassData; |
| 133 | QVector<int> jsClassOffsets; |
| 134 | QVector<CompiledData::TranslationData> translations; |
| 135 | }; |
| 136 | |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | QT_END_NAMESPACE |
| 142 | |
| 143 | #endif |
| 144 | |