aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2021-09-21 20:38:35 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2021-09-22 09:55:14 +0200
commitd50cf744b8de18d9b3756712cdd07f270512fa91 (patch)
tree33a8b03b9417ec20d1a1e602bdb6f3cb548a2d42
parent01a8e9f30d4396378e97a31e4b6d2ee489cb0ac9 (diff)
shiboken6: Refactor PrimitiveTypeEntry::basicReferencedTypeEntry()
Change it to always return "this" or the referenced type and add another getter referencesType() to check whether it actually references another entry. Also add another convenience function TypeEntry::asPrimitive(). This saves a lot of if's. Also remove ShibokenGenerator::pythonPrimitiveTypeName(PrimitiveTypeEntry *). Task-number: PYSIDE-1660 Change-Id: I7b3c2f32e67d64176bf0b9f11a2c4dea2d6273ba Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/shiboken6/ApiExtractor/abstractmetafunction.cpp7
-rw-r--r--sources/shiboken6/ApiExtractor/typesystem.cpp44
-rw-r--r--sources/shiboken6/ApiExtractor/typesystem.h22
-rw-r--r--sources/shiboken6/ApiExtractor/typesystem_typedefs.h1
-rw-r--r--sources/shiboken6/generator/shiboken/cppgenerator.cpp8
-rw-r--r--sources/shiboken6/generator/shiboken/overloaddata.cpp19
-rw-r--r--sources/shiboken6/generator/shiboken/shibokengenerator.cpp36
-rw-r--r--sources/shiboken6/generator/shiboken/shibokengenerator.h1
8 files changed, 59 insertions, 79 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp b/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp
index 8aae6c19b..2d9ee3dcb 100644
--- a/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp
+++ b/sources/shiboken6/ApiExtractor/abstractmetafunction.cpp
@@ -229,11 +229,8 @@ bool AbstractMetaFunction::returnsBool() const
{
if (d->m_type.typeUsagePattern() != AbstractMetaType::PrimitivePattern)
return false;
- auto *pte = static_cast<const PrimitiveTypeEntry *>(d->m_type.typeEntry());
- // Walk along typedefs
- while (auto *referencedPte = pte->referencedTypeEntry())
- pte =referencedPte;
- return pte->name() == u"bool";
+ const auto *pte = d->m_type.typeEntry()->asPrimitive();
+ return pte->basicReferencedTypeEntry()->name() == u"bool";
}
bool AbstractMetaFunction::isOperatorBool() const
diff --git a/sources/shiboken6/ApiExtractor/typesystem.cpp b/sources/shiboken6/ApiExtractor/typesystem.cpp
index 678acefc3..88aa15e7f 100644
--- a/sources/shiboken6/ApiExtractor/typesystem.cpp
+++ b/sources/shiboken6/ApiExtractor/typesystem.cpp
@@ -214,7 +214,7 @@ bool TypeEntry::isCppPrimitive() const
const PrimitiveTypeEntry *referencedType =
static_cast<const PrimitiveTypeEntry *>(this)->basicReferencedTypeEntry();
- const QString &typeName = referencedType ? referencedType->name() : m_d->m_name;
+ const QString &typeName = referencedType->name();
return typeName.contains(QLatin1Char(' ')) || primitiveCppTypes().contains(typeName);
}
@@ -500,15 +500,19 @@ void TypeEntry::setSourceLocation(const SourceLocation &sourceLocation)
m_d->m_sourceLocation = sourceLocation;
}
+const PrimitiveTypeEntry *TypeEntry::asPrimitive() const
+{
+ Q_ASSERT(m_d->m_type == PrimitiveType);
+ return static_cast<const PrimitiveTypeEntry *>(this);
+}
+
bool TypeEntry::isUserPrimitive() const
{
if (!isPrimitive())
return false;
- const auto *trueType = static_cast<const PrimitiveTypeEntry *>(this);
- if (trueType->basicReferencedTypeEntry())
- trueType = trueType->basicReferencedTypeEntry();
- return trueType->isPrimitive() && !trueType->isCppPrimitive()
- && trueType->qualifiedCppName() != u"std::string";
+ const auto *type = asPrimitive()->basicReferencedTypeEntry();
+ return !type->isCppPrimitive()
+ && type->qualifiedCppName() != u"std::string";
}
bool TypeEntry::isWrapperType() const
@@ -520,10 +524,8 @@ bool TypeEntry::isCppIntegralPrimitive() const
{
if (!isCppPrimitive())
return false;
- const auto *trueType = static_cast<const PrimitiveTypeEntry *>(this);
- if (trueType->basicReferencedTypeEntry())
- trueType = trueType->basicReferencedTypeEntry();
- QString typeName = trueType->qualifiedCppName();
+ const auto *type = asPrimitive()->basicReferencedTypeEntry();
+ QString typeName = type->qualifiedCppName();
return !typeName.contains(u"double")
&& !typeName.contains(u"float")
&& !typeName.contains(u"wchar");
@@ -535,10 +537,8 @@ bool TypeEntry::isExtendedCppPrimitive() const
return true;
if (!isPrimitive())
return false;
- const auto *trueType = static_cast<const PrimitiveTypeEntry *>(this);
- if (trueType->basicReferencedTypeEntry())
- trueType = trueType->basicReferencedTypeEntry();
- return trueType->qualifiedCppName() == u"std::string";
+ const auto *type = asPrimitive()->basicReferencedTypeEntry();
+ return type->qualifiedCppName() == u"std::string";
}
const TypeEntryPrivate *TypeEntry::d_func() const
@@ -903,14 +903,18 @@ void PrimitiveTypeEntry::setReferencedTypeEntry(PrimitiveTypeEntry *referencedTy
d->m_referencedTypeEntry = referencedTypeEntry;
}
-PrimitiveTypeEntry *PrimitiveTypeEntry::basicReferencedTypeEntry() const
+const PrimitiveTypeEntry *PrimitiveTypeEntry::basicReferencedTypeEntry() const
{
- S_D(const PrimitiveTypeEntry);
- if (!d->m_referencedTypeEntry)
- return nullptr;
+ auto *result = this;
+ while (auto *referenced = result->referencedTypeEntry())
+ result = referenced;
+ return result;
+}
- PrimitiveTypeEntry *baseReferencedTypeEntry = d->m_referencedTypeEntry->basicReferencedTypeEntry();
- return baseReferencedTypeEntry ? baseReferencedTypeEntry : d->m_referencedTypeEntry;
+bool PrimitiveTypeEntry::referencesType() const
+{
+ S_D(const PrimitiveTypeEntry);
+ return d->m_referencedTypeEntry != nullptr;
}
bool PrimitiveTypeEntry::preferredTargetLangType() const
diff --git a/sources/shiboken6/ApiExtractor/typesystem.h b/sources/shiboken6/ApiExtractor/typesystem.h
index f6a00cab5..38b8c0143 100644
--- a/sources/shiboken6/ApiExtractor/typesystem.h
+++ b/sources/shiboken6/ApiExtractor/typesystem.h
@@ -41,6 +41,7 @@ class CustomConversion;
class EnumValueTypeEntry;
class FlagsTypeEntry;
class SourceLocation;
+class PrimitiveTypeEntry;
class TypeSystemTypeEntry;
class TypeEntryPrivate;
@@ -251,6 +252,8 @@ public:
SourceLocation sourceLocation() const;
void setSourceLocation(const SourceLocation &sourceLocation);
+ const PrimitiveTypeEntry *asPrimitive() const;
+
// Query functions for generators
/// Returns true if the type is a primitive but not a C++ primitive.
bool isUserPrimitive() const;
@@ -380,24 +383,25 @@ public:
/**
* The PrimitiveTypeEntry pointed by this type entry if it
* represents a typedef).
- * /return the type referenced by the typedef, or a null pointer
+ * \return the type referenced by the typedef, or a null pointer
* if the current object is not an typedef
*/
PrimitiveTypeEntry *referencedTypeEntry() const;
/**
* Defines type referenced by this entry.
- * /param referencedTypeEntry type referenced by this entry
+ * \param referencedTypeEntry type referenced by this entry
*/
void setReferencedTypeEntry(PrimitiveTypeEntry* referencedTypeEntry);
- /**
- * Finds the most basic primitive type that the typedef represents,
- * i.e. a type that is not an typedef'ed.
- * /return the most basic non-typedef'ed primitive type represented
- * by this typedef
- */
- PrimitiveTypeEntry* basicReferencedTypeEntry() const;
+ /// Finds the most basic primitive type that the typedef represents,
+ /// i.e. a type that is not an typedef'ed.
+ /// \return the most basic non-typedef'ed primitive type represented
+ /// by this typedef or self in case it is not a reference.
+ const PrimitiveTypeEntry* basicReferencedTypeEntry() const;
+
+ /// Returns whether this entry references another entry.
+ bool referencesType() const;
bool preferredTargetLangType() const;
void setPreferredTargetLangType(bool b);
diff --git a/sources/shiboken6/ApiExtractor/typesystem_typedefs.h b/sources/shiboken6/ApiExtractor/typesystem_typedefs.h
index e7a72aa46..e6edd3a68 100644
--- a/sources/shiboken6/ApiExtractor/typesystem_typedefs.h
+++ b/sources/shiboken6/ApiExtractor/typesystem_typedefs.h
@@ -49,5 +49,4 @@ using DocModificationList = QList<DocModification>;
using FieldModificationList = QList<FieldModification>;
using FunctionModificationList = QList<FunctionModification>;
using TypeEntries = QList<const TypeEntry *>;
-
#endif // TYPESYSTEM_TYPEDEFS_H
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
index 43a2b4e78..d0dfe7f27 100644
--- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp
@@ -1105,9 +1105,7 @@ void CppGenerator::writeVirtualMethodNative(TextStream &s,
|| argType.referenceType() == LValueReference;
if (!convert && argTypeEntry->isPrimitive()) {
- const auto *pte = static_cast<const PrimitiveTypeEntry *>(argTypeEntry);
- if (pte->basicReferencedTypeEntry())
- pte = pte->basicReferencedTypeEntry();
+ const auto *pte = argTypeEntry->asPrimitive()->basicReferencedTypeEntry();
convert = !formatUnits().contains(pte->name());
}
@@ -6392,9 +6390,9 @@ bool CppGenerator::finishGeneration()
for (const PrimitiveTypeEntry *pte : primitiveTypeList) {
if (!pte->generateCode() || !pte->isCppPrimitive())
continue;
- const TypeEntry *referencedType = pte->basicReferencedTypeEntry();
- if (!referencedType)
+ if (!pte->referencesType())
continue;
+ const TypeEntry *referencedType = pte->basicReferencedTypeEntry();
QString converter = converterObject(referencedType);
QStringList cppSignature = pte->qualifiedCppName().split(QLatin1String("::"), Qt::SkipEmptyParts);
while (!cppSignature.isEmpty()) {
diff --git a/sources/shiboken6/generator/shiboken/overloaddata.cpp b/sources/shiboken6/generator/shiboken/overloaddata.cpp
index 5ad2087d3..103770396 100644
--- a/sources/shiboken6/generator/shiboken/overloaddata.cpp
+++ b/sources/shiboken6/generator/shiboken/overloaddata.cpp
@@ -45,25 +45,18 @@
#include <algorithm>
#include <utility>
-static const TypeEntry *getReferencedTypeEntry(const TypeEntry *typeEntry)
-{
- if (typeEntry->isPrimitive()) {
- auto pte = dynamic_cast<const PrimitiveTypeEntry *>(typeEntry);
- while (pte->referencedTypeEntry())
- pte = pte->referencedTypeEntry();
- typeEntry = pte;
- }
- return typeEntry;
-}
-
static QString getTypeName(const AbstractMetaType &type)
{
- const TypeEntry *typeEntry = getReferencedTypeEntry(type.typeEntry());
+ const TypeEntry *typeEntry = type.typeEntry();
+ if (typeEntry->isPrimitive())
+ typeEntry = typeEntry->asPrimitive()->basicReferencedTypeEntry();
QString typeName = typeEntry->name();
if (typeEntry->isContainer()) {
QStringList types;
for (const auto &cType : type.instantiations()) {
- const TypeEntry *typeEntry = getReferencedTypeEntry(cType.typeEntry());
+ const TypeEntry *typeEntry = cType.typeEntry();
+ if (typeEntry->isPrimitive())
+ typeEntry = typeEntry->asPrimitive()->basicReferencedTypeEntry();
types << typeEntry->name();
}
typeName += QLatin1Char('<') + types.join(QLatin1Char(',')) + QLatin1String(" >");
diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp
index 20ae7d76e..795dae03c 100644
--- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp
+++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp
@@ -656,10 +656,7 @@ QString ShibokenGenerator::getFormatUnitString(const AbstractMetaFunctionCPtr &f
|| type.referenceType() == LValueReference) {
result += QLatin1Char(objType);
} else if (type.isPrimitive()) {
- const auto *ptype =
- static_cast<const PrimitiveTypeEntry *>(type.typeEntry());
- if (ptype->basicReferencedTypeEntry())
- ptype = ptype->basicReferencedTypeEntry();
+ const auto *ptype = type.typeEntry()->asPrimitive()->basicReferencedTypeEntry();
const auto it = formatUnits().constFind(ptype->name());
if (it != formatUnits().cend())
result += it.value();
@@ -697,9 +694,7 @@ QString ShibokenGenerator::cpythonBaseName(const TypeEntry *type)
if (type->isWrapperType() || type->isNamespace()) { // && type->referenceType() == NoReference) {
baseName = QLatin1String("Sbk_") + type->name();
} else if (type->isPrimitive()) {
- const auto *ptype = static_cast<const PrimitiveTypeEntry *>(type);
- while (ptype->basicReferencedTypeEntry())
- ptype = ptype->basicReferencedTypeEntry();
+ const auto *ptype = type->asPrimitive()->basicReferencedTypeEntry();
if (ptype->targetLangApiName() == ptype->name())
baseName = pythonPrimitiveTypeName(ptype->name());
else
@@ -798,8 +793,7 @@ QString ShibokenGenerator::converterObject(const TypeEntry *type)
qDebug() << "Warning: the Qt5 primitive type is unknown" << type->qualifiedCppName();
return QString();
}
- if (pte->basicReferencedTypeEntry())
- pte = pte->basicReferencedTypeEntry();
+ pte = pte->basicReferencedTypeEntry();
if (pte->isPrimitive() && !pte->isCppPrimitive() && !pte->customConversion()) {
return u"Shiboken::Conversions::PrimitiveTypeConverter<"_qs
+ pte->qualifiedCppName() + u">()"_qs;
@@ -867,13 +861,6 @@ QString ShibokenGenerator::pythonPrimitiveTypeName(const QString &cppTypeName)
return rv;
}
-QString ShibokenGenerator::pythonPrimitiveTypeName(const PrimitiveTypeEntry *type)
-{
- while (type->basicReferencedTypeEntry())
- type = type->basicReferencedTypeEntry();
- return pythonPrimitiveTypeName(type->name());
-}
-
static const QHash<QString, QString> &pythonOperators()
{
static const QHash<QString, QString> result = {
@@ -966,7 +953,8 @@ bool ShibokenGenerator::isNumber(const TypeEntry *type)
{
if (!type->isPrimitive())
return false;
- return isNumber(pythonPrimitiveTypeName(static_cast<const PrimitiveTypeEntry *>(type)));
+ const auto *pte = type->asPrimitive()->basicReferencedTypeEntry();
+ return isNumber(pythonPrimitiveTypeName(pte->name()));
}
bool ShibokenGenerator::isNumber(const AbstractMetaType &type)
@@ -978,8 +966,8 @@ bool ShibokenGenerator::isPyInt(const TypeEntry *type)
{
if (!type->isPrimitive())
return false;
- return pythonPrimitiveTypeName(static_cast<const PrimitiveTypeEntry *>(type))
- == QLatin1String("PyLong");
+ const auto *pte = type->asPrimitive()->basicReferencedTypeEntry();
+ return pythonPrimitiveTypeName(pte->name()) == u"PyLong";
}
bool ShibokenGenerator::isPyInt(const AbstractMetaType &type)
@@ -1077,7 +1065,8 @@ QString ShibokenGenerator::cpythonCheckFunction(const TypeEntry *type) const
if (type->isEnum() || type->isFlags() || type->isWrapperType())
return u"SbkObject_TypeCheck("_qs + cpythonTypeNameExt(type) + u", "_qs;
if (type->isExtendedCppPrimitive()) {
- return pythonPrimitiveTypeName(static_cast<const PrimitiveTypeEntry *>(type))
+ const auto *pte = type->asPrimitive()->basicReferencedTypeEntry();
+ return pythonPrimitiveTypeName(pte->name())
+ QLatin1String("_Check");
}
QString typeCheck;
@@ -2567,11 +2556,8 @@ QString ShibokenGenerator::getTypeIndexVariableName(const AbstractMetaClass *met
}
QString ShibokenGenerator::getTypeIndexVariableName(const TypeEntry *type)
{
- if (type->isCppPrimitive()) {
- const auto *trueType = static_cast<const PrimitiveTypeEntry *>(type);
- if (trueType->basicReferencedTypeEntry())
- type = trueType->basicReferencedTypeEntry();
- }
+ if (type->isCppPrimitive())
+ type = type->asPrimitive()->basicReferencedTypeEntry();
QString result = QLatin1String("SBK_");
// Disambiguate namespaces per module to allow for extending them.
if (type->isNamespace()) {
diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h
index 03ebf2d2e..454083768 100644
--- a/sources/shiboken6/generator/shiboken/shibokengenerator.h
+++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h
@@ -208,7 +208,6 @@ protected:
static QString protectedEnumSurrogateName(const AbstractMetaEnum &metaEnum);
static QString pythonPrimitiveTypeName(const QString &cppTypeName);
- static QString pythonPrimitiveTypeName(const PrimitiveTypeEntry *type);
static QString pythonOperatorFunctionName(const QString &cppOpFuncName);
static QString pythonOperatorFunctionName(const AbstractMetaFunctionCPtr &func);