aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sources/shiboken2/generator/generator.cpp47
-rw-r--r--sources/shiboken2/generator/shiboken2/headergenerator.cpp12
2 files changed, 44 insertions, 15 deletions
diff --git a/sources/shiboken2/generator/generator.cpp b/sources/shiboken2/generator/generator.cpp
index b14edfc4c..585102b01 100644
--- a/sources/shiboken2/generator/generator.cpp
+++ b/sources/shiboken2/generator/generator.cpp
@@ -159,7 +159,6 @@ struct Generator::GeneratorPrivate
QString licenseComment;
QString moduleName;
QStringList instantiatedContainersNames;
- QStringList instantiatedSmartPointerNames;
QVector<const AbstractMetaType *> instantiatedContainers;
QVector<const AbstractMetaType *> instantiatedSmartPointers;
@@ -211,6 +210,31 @@ QString Generator::getSimplifiedContainerTypeName(const AbstractMetaType *type)
return typeName;
}
+// Strip a "const QSharedPtr<const Foo> &" or similar to "QSharedPtr<Foo>" (PYSIDE-1016/454)
+const AbstractMetaType *canonicalSmartPtrInstantiation(const AbstractMetaType *type)
+{
+ AbstractMetaTypeList instantiations = type->instantiations();
+ Q_ASSERT(instantiations.size() == 1);
+ const bool needsFix = type->isConstant() || type->referenceType() != NoReference;
+ const bool pointeeNeedsFix = instantiations.constFirst()->isConstant();
+ if (!needsFix && !pointeeNeedsFix)
+ return type;
+ auto fixedType = type->copy();
+ fixedType->setReferenceType(NoReference);
+ fixedType->setConstant(false);
+ if (pointeeNeedsFix) {
+ auto fixedPointeeType = instantiations.constFirst()->copy();
+ fixedPointeeType->setConstant(false);
+ fixedType->setInstantiations(AbstractMetaTypeList(1, fixedPointeeType));
+ }
+ return fixedType;
+}
+
+static inline const TypeEntry *pointeeTypeEntry(const AbstractMetaType *smartPtrType)
+{
+ return smartPtrType->instantiations().constFirst()->typeEntry();
+}
+
void Generator::addInstantiatedContainersAndSmartPointers(const AbstractMetaType *type,
const QString &context)
{
@@ -244,18 +268,15 @@ void Generator::addInstantiatedContainersAndSmartPointers(const AbstractMetaType
m_d->instantiatedContainers.append(type);
}
} else {
- // Is smart pointer.
- if (!m_d->instantiatedSmartPointerNames.contains(typeName)) {
- m_d->instantiatedSmartPointerNames.append(typeName);
- if (type->isConstant() || type->referenceType() != NoReference) {
- // Strip a "const QSharedPtr<Foo> &" or similar to "QSharedPtr<Foo>" (PYSIDE-1016)
- auto fixedType = type->copy();
- fixedType->setReferenceType(NoReference);
- fixedType->setConstant(false);
- type = fixedType;
- }
- m_d->instantiatedSmartPointers.append(type);
- }
+ // Is smart pointer. Check if the (const?) pointee is already known
+ auto pt = pointeeTypeEntry(type);
+ const bool present =
+ std::any_of(m_d->instantiatedSmartPointers.cbegin(), m_d->instantiatedSmartPointers.cend(),
+ [pt] (const AbstractMetaType *t) {
+ return pointeeTypeEntry(t) == pt;
+ });
+ if (!present)
+ m_d->instantiatedSmartPointers.append(canonicalSmartPtrInstantiation(type));
}
}
diff --git a/sources/shiboken2/generator/shiboken2/headergenerator.cpp b/sources/shiboken2/generator/shiboken2/headergenerator.cpp
index cf96c8d5c..a565659de 100644
--- a/sources/shiboken2/generator/shiboken2/headergenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/headergenerator.cpp
@@ -401,9 +401,17 @@ bool HeaderGenerator::finishGeneration()
int smartPointerCount = 0;
const QVector<const AbstractMetaType *> &instantiatedSmartPtrs = instantiatedSmartPointers();
for (const AbstractMetaType *metaType : instantiatedSmartPtrs) {
- _writeTypeIndexValue(macrosStream, getTypeIndexVariableName(metaType),
- smartPointerCountIndex);
+ QString indexName = getTypeIndexVariableName(metaType);
+ _writeTypeIndexValue(macrosStream, indexName, smartPointerCountIndex);
macrosStream << ", // " << metaType->cppSignature() << Qt::endl;
+ // Add a the same value for const pointees (shared_ptr<const Foo>).
+ const auto ptrName = metaType->typeEntry()->entryName();
+ int pos = indexName.indexOf(ptrName, 0, Qt::CaseInsensitive);
+ if (pos >= 0) {
+ indexName.insert(pos + ptrName.size() + 1, QLatin1String("CONST"));
+ _writeTypeIndexValue(macrosStream, indexName, smartPointerCountIndex);
+ macrosStream << ", // (const)\n";
+ }
++smartPointerCountIndex;
++smartPointerCount;
}