aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken6/ApiExtractor
diff options
context:
space:
mode:
Diffstat (limited to 'sources/shiboken6/ApiExtractor')
-rw-r--r--sources/shiboken6/ApiExtractor/abstractmetatype.cpp53
-rw-r--r--sources/shiboken6/ApiExtractor/abstractmetatype.h13
2 files changed, 66 insertions, 0 deletions
diff --git a/sources/shiboken6/ApiExtractor/abstractmetatype.cpp b/sources/shiboken6/ApiExtractor/abstractmetatype.cpp
index 3401a61c9..f099bba08 100644
--- a/sources/shiboken6/ApiExtractor/abstractmetatype.cpp
+++ b/sources/shiboken6/ApiExtractor/abstractmetatype.cpp
@@ -27,6 +27,9 @@
****************************************************************************/
#include "abstractmetatype.h"
+#include "abstractmetabuilder.h"
+#include "abstractmetalang.h"
+#include "messages.h"
#include "typedatabase.h"
#include "typesystem.h"
#include "parser/codemodel.h"
@@ -35,6 +38,7 @@
# include <QtCore/QDebug>
#endif
+#include <QtCore/QHash>
#include <QtCore/QSharedData>
#include <QtCore/QSharedPointer>
#include <QtCore/QStack>
@@ -797,6 +801,55 @@ bool AbstractMetaType::valueTypeWithCopyConstructorOnlyPassed() const
&& isValueTypeWithCopyConstructorOnly();
}
+using AbstractMetaTypeCache = QHash<QString, AbstractMetaType>;
+
+Q_GLOBAL_STATIC(AbstractMetaTypeCache, metaTypeFromStringCache)
+
+std::optional<AbstractMetaType>
+AbstractMetaType::fromString(QString typeSignature, QString *errorMessage)
+{
+ typeSignature = typeSignature.trimmed();
+ if (typeSignature.startsWith(QLatin1String("::")))
+ typeSignature.remove(0, 2);
+
+ auto &cache = *metaTypeFromStringCache();
+ auto it = cache.find(typeSignature);
+ if (it == cache.end()) {
+ auto metaType =
+ AbstractMetaBuilder::translateType(typeSignature, nullptr, {}, errorMessage);
+ if (Q_UNLIKELY(!metaType.has_value())) {
+ if (errorMessage)
+ errorMessage->prepend(msgCannotBuildMetaType(typeSignature));
+ return {};
+ }
+ it = cache.insert(typeSignature, metaType.value());
+ }
+ return it.value();
+}
+
+AbstractMetaType AbstractMetaType::fromTypeEntry(const TypeEntry *typeEntry)
+{
+ QString typeName = typeEntry->qualifiedCppName();
+ if (typeName.startsWith(QLatin1String("::")))
+ typeName.remove(0, 2);
+ auto &cache = *metaTypeFromStringCache();
+ auto it = cache.find(typeName);
+ if (it != cache.end())
+ return it.value();
+ AbstractMetaType metaType(typeEntry);
+ metaType.clearIndirections();
+ metaType.setReferenceType(NoReference);
+ metaType.setConstant(false);
+ metaType.decideUsagePattern();
+ cache.insert(typeName, metaType);
+ return metaType;
+}
+
+AbstractMetaType AbstractMetaType::fromAbstractMetaClass(const AbstractMetaClass *metaClass)
+{
+ return fromTypeEntry(metaClass->typeEntry());
+}
+
#ifndef QT_NO_DEBUG_STREAM
void AbstractMetaType::formatDebug(QDebug &debug) const
{
diff --git a/sources/shiboken6/ApiExtractor/abstractmetatype.h b/sources/shiboken6/ApiExtractor/abstractmetatype.h
index 6aa682e04..79523efef 100644
--- a/sources/shiboken6/ApiExtractor/abstractmetatype.h
+++ b/sources/shiboken6/ApiExtractor/abstractmetatype.h
@@ -36,6 +36,8 @@
#include <QtCore/QSharedDataPointer>
#include <QtCore/QList>
+#include <optional>
+
QT_FORWARD_DECLARE_CLASS(QDebug)
class AbstractMetaTypeData;
@@ -194,6 +196,17 @@ public:
static AbstractMetaType createVoid();
+ /// Builds an AbstractMetaType object from a QString.
+ /// Returns nullopt if no type could be built from the string.
+ /// \param typeSignature The string describing the type to be built.
+ /// \return A new AbstractMetaType object or nullopt in case of failure.
+ static std::optional<AbstractMetaType>
+ fromString(QString typeSignature, QString *errorMessage = nullptr);
+ /// Creates an AbstractMetaType object from a TypeEntry.
+ static AbstractMetaType fromTypeEntry(const TypeEntry *typeEntry);
+ /// Creates an AbstractMetaType object from an AbstractMetaClass.
+ static AbstractMetaType fromAbstractMetaClass(const AbstractMetaClass *metaClass);
+
static void dereference(QString *type); // "foo" -> "(*foo)"
static bool stripDereference(QString *type); // "(*foo)" -> "foo"