diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2023-03-14 15:48:49 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2023-03-31 17:42:58 +0200 |
commit | b83de5f9a43b094bbb77b3aeea77983ea508a2b0 (patch) | |
tree | dcd800c2067d70691ad41cb05f8c3f032cc270ce /src/tools/moc | |
parent | ecd7ddcc3e7ccf7750190b4aedd3ad683dd4f97d (diff) |
moc: Record types of enumerations
This will be helpful in a number of places, in particular in order to
support enums of different sizes in QML. We record the type as string in
the JSON output and as QMetaTypeInterface in the generated C++.
Task-number: QTBUG-112180
Change-Id: I943fac67f8b25b013d3860301416cdd293c0c69e
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/tools/moc')
-rw-r--r-- | src/tools/moc/generator.cpp | 17 | ||||
-rw-r--r-- | src/tools/moc/moc.cpp | 4 | ||||
-rw-r--r-- | src/tools/moc/moc.h | 1 |
3 files changed, 17 insertions, 5 deletions
diff --git a/src/tools/moc/generator.cpp b/src/tools/moc/generator.cpp index ca6a4181b6..cef460b7dd 100644 --- a/src/tools/moc/generator.cpp +++ b/src/tools/moc/generator.cpp @@ -378,7 +378,7 @@ void Generator::generateCode() int enumsIndex = index; for (int i = 0; i < cdef->enumList.size(); ++i) - index += 5 + (cdef->enumList.at(i).values.size() * 2); + index += QMetaObjectPrivate::IntsPerEnum + (cdef->enumList.at(i).values.size() * 2); fprintf(out, " %4d, %4d, // constructors\n", isConstructible ? int(cdef->constructorList.size()) : 0, isConstructible ? index : 0); @@ -397,8 +397,8 @@ void Generator::generateCode() // generateClassInfos(); - // all property metatypes, + 1 for the type of the current class itself - int initialMetaTypeOffset = cdef->propertyList.size() + 1; + // all property metatypes + all enum metatypes + 1 for the type of the current class itself + int initialMetaTypeOffset = cdef->propertyList.size() + cdef->enumList.size() + 1; // // Build signals array first, otherwise the signal indices would be wrong @@ -576,6 +576,15 @@ void Generator::generateCode() comma = ","; } + // metatypes for enums + for (int i = 0; i < cdef->enumList.size(); ++i) { + const EnumDef &e = cdef->enumList.at(i); + fprintf(out, "%s\n // enum '%s'\n %s", + comma, e.name.constData(), + stringForType(cdef->classname % "::" % e.name, true).constData()); + comma = ","; + } + // type name for the Q_OJBECT/GADGET itself, void for namespaces auto ownType = !cdef->hasQNamespace ? cdef->classname.data() : "void"; fprintf(out, "%s\n // Q_OBJECT / Q_GADGET\n %s", @@ -943,7 +952,7 @@ void Generator::generateEnums(int index) return; fprintf(out, "\n // enums: name, alias, flags, count, data\n"); - index += 5 * cdef->enumList.size(); + index += QMetaObjectPrivate::IntsPerEnum * cdef->enumList.size(); int i; for (i = 0; i < cdef->enumList.size(); ++i) { const EnumDef &e = cdef->enumList.at(i); diff --git a/src/tools/moc/moc.cpp b/src/tools/moc/moc.cpp index 64e98ae63b..743742ba97 100644 --- a/src/tools/moc/moc.cpp +++ b/src/tools/moc/moc.cpp @@ -243,7 +243,7 @@ bool Moc::parseEnum(EnumDef *def) } if (test(COLON)) { // C++11 strongly typed enum // enum Foo : unsigned long { ... }; - parseType(); //ignore the result + def->type = normalizeType(parseType().name); } if (!test(LBRACE)) return false; @@ -2107,6 +2107,8 @@ QJsonObject EnumDef::toJson(const ClassDef &cdef) const def["name"_L1] = QString::fromUtf8(name); if (!enumName.isEmpty()) def["alias"_L1] = QString::fromUtf8(enumName); + if (!type.isEmpty()) + def["type"_L1] = QString::fromUtf8(type); def["isFlag"_L1] = cdef.enumDeclarations.value(name); def["isClass"_L1] = isEnumClass; diff --git a/src/tools/moc/moc.h b/src/tools/moc/moc.h index edeac03b82..5df15409a7 100644 --- a/src/tools/moc/moc.h +++ b/src/tools/moc/moc.h @@ -43,6 +43,7 @@ struct EnumDef { QByteArray name; QByteArray enumName; + QByteArray type; QList<QByteArray> values; bool isEnumClass; // c++11 enum class EnumDef() : isEnumClass(false) {} |