diff options
Diffstat (limited to 'sources/shiboken6/ApiExtractor/abstractmetaenum.cpp')
-rw-r--r-- | sources/shiboken6/ApiExtractor/abstractmetaenum.cpp | 156 |
1 files changed, 140 insertions, 16 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetaenum.cpp b/sources/shiboken6/ApiExtractor/abstractmetaenum.cpp index b912273bb..780170c22 100644 --- a/sources/shiboken6/ApiExtractor/abstractmetaenum.cpp +++ b/sources/shiboken6/ApiExtractor/abstractmetaenum.cpp @@ -11,6 +11,8 @@ #include <QtCore/QDebug> +#include <algorithm> + using namespace Qt::StringLiterals; class AbstractMetaEnumValueData : public QSharedData @@ -20,6 +22,7 @@ public: QString m_stringValue; EnumValue m_value; Documentation m_doc; + bool m_deprecated = false; }; AbstractMetaEnumValue::AbstractMetaEnumValue() : @@ -29,8 +32,8 @@ AbstractMetaEnumValue::AbstractMetaEnumValue() : AbstractMetaEnumValue::AbstractMetaEnumValue(const AbstractMetaEnumValue &) = default; AbstractMetaEnumValue &AbstractMetaEnumValue::operator=(const AbstractMetaEnumValue &) = default; -AbstractMetaEnumValue::AbstractMetaEnumValue(AbstractMetaEnumValue &&) = default; -AbstractMetaEnumValue &AbstractMetaEnumValue::operator=(AbstractMetaEnumValue &&) = default; +AbstractMetaEnumValue::AbstractMetaEnumValue(AbstractMetaEnumValue &&) noexcept = default; +AbstractMetaEnumValue &AbstractMetaEnumValue::operator=(AbstractMetaEnumValue &&) noexcept = default; AbstractMetaEnumValue::~AbstractMetaEnumValue() = default; EnumValue AbstractMetaEnumValue::value() const @@ -66,6 +69,17 @@ void AbstractMetaEnumValue::setName(const QString &name) d->m_name = name; } +bool AbstractMetaEnumValue::isDeprecated() const +{ + return d->m_deprecated; +} + +void AbstractMetaEnumValue::setDeprecated(bool deprecated) +{ + if (d->m_deprecated != deprecated) + d->m_deprecated = deprecated; +} + Documentation AbstractMetaEnumValue::documentation() const { return d->m_doc; @@ -82,29 +96,67 @@ void AbstractMetaEnumValue::setDocumentation(const Documentation &doc) class AbstractMetaEnumData : public QSharedData { public: - AbstractMetaEnumData() : m_hasQenumsDeclaration(false), m_signed(true) + AbstractMetaEnumData() : m_deprecated(false), + m_hasQenumsDeclaration(false), m_signed(true) { } + int unsignedUsedBits() const; + int signedUsedBits() const; + AbstractMetaEnumValueList m_enumValues; - EnumTypeEntry *m_typeEntry = nullptr; + EnumTypeEntryCPtr m_typeEntry; Documentation m_doc; + QString m_underlyingType; EnumKind m_enumKind = CEnum; Access m_access = Access::Public; + uint m_deprecated : 1; uint m_hasQenumsDeclaration : 1; uint m_signed : 1; }; +static int _usedBits(uint64_t v) +{ + return (v >> 32) ? 64 : (v >> 16) ? 32 : (v >> 8) ? 16 : 8; +} + +static int _usedBits(int64_t v) +{ + return (v >> 31) ? 64 : (v >> 15) ? 32 : (v >> 7) ? 16 : 8; +} + +int AbstractMetaEnumData::unsignedUsedBits() const +{ + uint64_t maxValue = 0; + for (const auto &v : m_enumValues) { + if (const auto uv = v.value().unsignedValue(); uv > maxValue) + maxValue = uv; + } + return _usedBits(maxValue); +} + +int AbstractMetaEnumData::signedUsedBits() const +{ + int64_t maxValue = 0; + for (const auto &v : m_enumValues) { + const auto sv = v.value().value(); + const auto absV = sv < 0 ? ~sv : sv; + if (absV > maxValue) + maxValue = absV; + } + return _usedBits(maxValue); +} + AbstractMetaEnum::AbstractMetaEnum() : d(new AbstractMetaEnumData) { } AbstractMetaEnum::AbstractMetaEnum(const AbstractMetaEnum &) = default; AbstractMetaEnum &AbstractMetaEnum::operator=(const AbstractMetaEnum&) = default; -AbstractMetaEnum::AbstractMetaEnum(AbstractMetaEnum &&) = default; -AbstractMetaEnum &AbstractMetaEnum::operator=(AbstractMetaEnum &&) = default; +AbstractMetaEnum::AbstractMetaEnum(AbstractMetaEnum &&) noexcept = default; +AbstractMetaEnum &AbstractMetaEnum::operator=(AbstractMetaEnum &&) noexcept = default; AbstractMetaEnum::~AbstractMetaEnum() = default; const AbstractMetaEnumValueList &AbstractMetaEnum::values() const @@ -112,6 +164,16 @@ const AbstractMetaEnumValueList &AbstractMetaEnum::values() const return d->m_enumValues; } +AbstractMetaEnumValueList AbstractMetaEnum::nonRejectedValues() const +{ + auto te = d->m_typeEntry; + AbstractMetaEnumValueList result = d->m_enumValues; + auto pred = [te](const AbstractMetaEnumValue &v) { + return te->isEnumValueRejected(v.name()); }; + result.erase(std::remove_if(result.begin(), result.end(), pred), result.end()); + return result; +} + void AbstractMetaEnum::addEnumValue(const AbstractMetaEnumValue &enumValue) { d->m_enumValues << enumValue; @@ -164,6 +226,36 @@ void AbstractMetaEnum::setAccess(Access a) d->m_access = a; } +bool AbstractMetaEnum::isDeprecated() const +{ + return d->m_deprecated; +} + +void AbstractMetaEnum::setDeprecated(bool deprecated) +{ + if (d->m_deprecated != deprecated) + d->m_deprecated = deprecated; +} + +static bool isDeprecatedValue(const AbstractMetaEnumValue &v) +{ + return v.isDeprecated(); +}; + +bool AbstractMetaEnum::hasDeprecatedValues() const +{ + return std::any_of(d->m_enumValues.cbegin(), d->m_enumValues.cend(), + isDeprecatedValue); +} + +AbstractMetaEnumValueList AbstractMetaEnum::deprecatedValues() const +{ + AbstractMetaEnumValueList result; + std::copy_if(d->m_enumValues.cbegin(), d->m_enumValues.cend(), + std::back_inserter(result), isDeprecatedValue); + return result; +} + const Documentation &AbstractMetaEnum::documentation() const { return d->m_doc; @@ -217,12 +309,12 @@ void AbstractMetaEnum::setHasQEnumsDeclaration(bool on) d->m_hasQenumsDeclaration = on; } -EnumTypeEntry *AbstractMetaEnum::typeEntry() const +EnumTypeEntryCPtr AbstractMetaEnum::typeEntry() const { return d->m_typeEntry; } -void AbstractMetaEnum::setTypeEntry(EnumTypeEntry *entry) +void AbstractMetaEnum::setTypeEntry(const EnumTypeEntryCPtr &entry) { if (d->m_typeEntry != entry) d->m_typeEntry = entry; @@ -239,11 +331,39 @@ void AbstractMetaEnum::setSigned(bool s) d->m_signed = s; } +QString AbstractMetaEnum::underlyingType() const +{ + return d->m_underlyingType; +} + +void AbstractMetaEnum::setUnderlyingType(const QString &underlyingType) +{ + if (d->m_underlyingType != underlyingType) + d->m_underlyingType = underlyingType; +} + +int AbstractMetaEnum::usedBits() const +{ + return isSigned() ? d->signedUsedBits() : d->unsignedUsedBits(); +} + +QString AbstractMetaEnum::intTypeForSize(int usedBits, bool isSigned) +{ + QString result = u"int"_s + QString::number(usedBits) + u"_t"_s; + return isSigned ? result : u'u' + result; +} + #ifndef QT_NO_DEBUG_STREAM -static void formatMetaEnumValue(QDebug &d, const AbstractMetaEnumValue &v) +static void formatMetaEnumValue(QDebug &d, const AbstractMetaEnumValue &v, bool forceHex = false) { - d << v.name() << '=' << v.value(); + d << v.name() << '='; + if (forceHex) + v.value().formatDebugHex(d); + else + v.value().formatDebug(d); + if (v.isDeprecated()) + d << " (deprecated)"; } QDebug operator<<(QDebug d, const AbstractMetaEnumValue &v) @@ -259,15 +379,19 @@ QDebug operator<<(QDebug d, const AbstractMetaEnumValue &v) static void formatMetaEnum(QDebug &d, const AbstractMetaEnum &e) { - d << e.fullName(); + d << '"' << e.fullName() << '"'; + if (e.isDeprecated()) + d << " (deprecated)"; + d << " \"" << e.underlyingType() << '"'; if (!e.isSigned()) - d << " (unsigned) "; - d << '['; + d << " (unsigned)"; + d << " ["; const AbstractMetaEnumValueList &values = e.values(); - for (int i = 0, count = values.size(); i < count; ++i) { + const bool hasFlags = e.typeEntry()->flags() != nullptr; + for (qsizetype i = 0, count = values.size(); i < count; ++i) { if (i) - d << ' '; - formatMetaEnumValue(d, values.at(i)); + d << ", "; + formatMetaEnumValue(d, values.at(i), hasFlags); } d << ']'; } |