From c6452857a96d24325906ebd5529cfdda916a68fc Mon Sep 17 00:00:00 2001 From: Ulf Hermann Date: Thu, 12 Nov 2020 10:00:41 +0100 Subject: QmlCompiler: Store values QQmlJSMetaEnum when available When parsing QML files, we need to keep the enum values around as this is the only place where we can find them (without parsing the same file again, that is). With C++ types we don't strictly need them as they are also available from the C++ header, but if the qmltypes file is nice enough to provide them, we can use them. (Which will be for types that are not actually C++ types, but rather types produced by registering a QML file with qmlRegisterType()). Change-Id: Ibcc93b30e523a00e1eeb80029943c48b83d0e1b5 Reviewed-by: Fabian Kosmale --- src/qmlcompiler/qqmljsimportvisitor.cpp | 4 +++- src/qmlcompiler/qqmljsmetatypes_p.h | 8 ++++++++ src/qmlcompiler/qqmljstypedescriptionreader.cpp | 15 +++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp index b81f7b59a7..41e8b4da88 100644 --- a/src/qmlcompiler/qqmljsimportvisitor.cpp +++ b/src/qmlcompiler/qqmljsimportvisitor.cpp @@ -281,8 +281,10 @@ void QQmlJSImportVisitor::endVisit(UiScriptBinding *scriptBinding) bool QQmlJSImportVisitor::visit(QQmlJS::AST::UiEnumDeclaration *uied) { QQmlJSMetaEnum qmlEnum(uied->name.toString()); - for (const auto *member = uied->members; member; member = member->next) + for (const auto *member = uied->members; member; member = member->next) { qmlEnum.addKey(member->member.toString()); + qmlEnum.addValue(int(member->value)); + } m_currentScope->addEnumeration(qmlEnum); return true; } diff --git a/src/qmlcompiler/qqmljsmetatypes_p.h b/src/qmlcompiler/qqmljsmetatypes_p.h index dcb388abfb..86a49c89c7 100644 --- a/src/qmlcompiler/qqmljsmetatypes_p.h +++ b/src/qmlcompiler/qqmljsmetatypes_p.h @@ -59,6 +59,7 @@ class QQmlJSScope; class QQmlJSMetaEnum { QStringList m_keys; + QList m_values; // empty if values unknown. QString m_name; QString m_alias; bool m_isFlag = false; @@ -80,6 +81,13 @@ public: void addKey(const QString &key) { m_keys.append(key); } QStringList keys() const { return m_keys; } + + void addValue(int value) { m_values.append(value); } + QList values() const { return m_values; } + + bool hasValues() const { return !m_values.isEmpty(); } + int value(const QString &key) const { return m_values.value(m_keys.indexOf(key)); } + bool hasKey(const QString &key) const { return m_keys.indexOf(key) != -1; } }; class QQmlJSMetaMethod diff --git a/src/qmlcompiler/qqmljstypedescriptionreader.cpp b/src/qmlcompiler/qqmljstypedescriptionreader.cpp index 0a5c97c3db..8bdfd89370 100644 --- a/src/qmlcompiler/qqmljstypedescriptionreader.cpp +++ b/src/qmlcompiler/qqmljstypedescriptionreader.cpp @@ -677,10 +677,25 @@ void QQmlJSTypeDescriptionReader::readEnumValues(UiScriptBinding *ast, QQmlJSMet } if (auto *objectLit = cast(expStmt->expression)) { + int currentValue = -1; for (PatternPropertyList *it = objectLit->properties; it; it = it->next) { if (PatternProperty *assignement = it->property) { if (auto *name = cast(assignement->name)) { metaEnum->addKey(name->id.toString()); + + if (auto *value = AST::cast(assignement->initializer)) { + currentValue = int(value->value); + } else if (auto *minus = AST::cast( + assignement->initializer)) { + if (auto *value = AST::cast(minus->expression)) + currentValue = -int(value->value); + else + ++currentValue; + } else { + ++currentValue; + } + + metaEnum->addValue(currentValue); continue; } } -- cgit v1.2.3