diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-01-22 08:10:13 +0100 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2021-01-23 16:30:03 +0100 |
commit | 3991fd14166fceec0a69dc20401e72f0ec0159bf (patch) | |
tree | b77f4ea4e6f749ea2add239096115bb65bfdaa74 | |
parent | daf348d3e961bcccc6a3757933305b6f105e82f2 (diff) |
shiboken6: Make cache of buildAbstractMetaTypeFromTypeEntry() static
The cache can then be used by all generators and
buildAbstractMetaTypeFromTypeEntry() and some dependent functions
can be made static.
Change-Id: Ied8e683f20fe80915d3c81fffb4b39a80c7cd0d5
Reviewed-by: Christian Tismer <tismer@stackless.com>
4 files changed, 37 insertions, 29 deletions
diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.cpp b/sources/shiboken6/generator/shiboken/cppgenerator.cpp index 179bfa2d5..d15434c50 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.cpp +++ b/sources/shiboken6/generator/shiboken/cppgenerator.cpp @@ -2448,7 +2448,7 @@ static const QStringList &knownPythonTypes() } std::optional<AbstractMetaType> - CppGenerator::getArgumentType(const AbstractMetaFunctionCPtr &func, int argPos) const + CppGenerator::getArgumentType(const AbstractMetaFunctionCPtr &func, int argPos) { if (argPos < 0 || argPos > func->arguments().size()) { qCWarning(lcShiboken).noquote().nospace() diff --git a/sources/shiboken6/generator/shiboken/cppgenerator.h b/sources/shiboken6/generator/shiboken/cppgenerator.h index b74681c88..d3482d385 100644 --- a/sources/shiboken6/generator/shiboken/cppgenerator.h +++ b/sources/shiboken6/generator/shiboken/cppgenerator.h @@ -163,8 +163,8 @@ private: * \param newType It is set to true if the type returned is a new object that must be deallocated. * \return The type of the argument indicated by \p argPos. */ - std::optional<AbstractMetaType> - getArgumentType(const AbstractMetaFunctionCPtr &func, int argPos) const; + static std::optional<AbstractMetaType> + getArgumentType(const AbstractMetaFunctionCPtr &func, int argPos); void writePythonToCppTypeConversion(TextStream &s, const AbstractMetaType &type, diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp index 0ac94beac..8130570e6 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.cpp +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.cpp @@ -125,6 +125,10 @@ using GeneratorClassInfoCache = QHash<const AbstractMetaClass *, GeneratorClassI Q_GLOBAL_STATIC(GeneratorClassInfoCache, generatorClassInfoCache) +using AbstractMetaTypeCache = QHash<QString, AbstractMetaType>; + +Q_GLOBAL_STATIC(AbstractMetaTypeCache, metaTypeFromStringCache) + static const char CHECKTYPE_REGEX[] = R"(%CHECKTYPE\[([^\[]*)\]\()"; static const char ISCONVERTIBLE_REGEX[] = R"(%ISCONVERTIBLE\[([^\[]*)\]\()"; static const char CONVERTTOPYTHON_REGEX[] = R"(%CONVERTTOPYTHON\[([^\[]*)\]\()"; @@ -1124,7 +1128,7 @@ QString ShibokenGenerator::cpythonCheckFunction(const TypeEntry *type, bool gene } ShibokenGenerator::CPythonCheckFunctionResult - ShibokenGenerator::guessCPythonCheckFunction(const QString &type) const + ShibokenGenerator::guessCPythonCheckFunction(const QString &type) { // PYSIDE-795: We abuse PySequence for iterables. // This part handles the overrides in the XML files. @@ -1527,7 +1531,7 @@ static QString getArgumentsFromMethodCall(const QString &str) QString ShibokenGenerator::getCodeSnippets(const CodeSnipList &codeSnips, TypeSystem::CodeSnipPosition position, - TypeSystem::Language language) const + TypeSystem::Language language) { QString code; for (const CodeSnip &snip : codeSnips) { @@ -1571,7 +1575,7 @@ void ShibokenGenerator::processCodeSnip(QString &code) const ShibokenGenerator::ArgumentVarReplacementList ShibokenGenerator::getArgumentReplacement(const AbstractMetaFunctionCPtr &func, bool usePyArgs, TypeSystem::Language language, - const AbstractMetaArgument *lastArg) const + const AbstractMetaArgument *lastArg) { ArgumentVarReplacementList argReplacements; TypeSystem::Language convLang = (language == TypeSystem::TargetLangCode) @@ -2154,14 +2158,15 @@ QString ShibokenGenerator::getModuleHeaderFileName(const QString &moduleName) co std::optional<AbstractMetaType> ShibokenGenerator::buildAbstractMetaTypeFromString(QString typeSignature, - QString *errorMessage) const + QString *errorMessage) { typeSignature = typeSignature.trimmed(); if (typeSignature.startsWith(QLatin1String("::"))) typeSignature.remove(0, 2); - auto it = m_metaTypeFromStringCache.find(typeSignature); - if (it == m_metaTypeFromStringCache.end()) { + 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())) { @@ -2169,29 +2174,32 @@ std::optional<AbstractMetaType> errorMessage->prepend(msgCannotBuildMetaType(typeSignature)); return {}; } - it = m_metaTypeFromStringCache.insert(typeSignature, metaType.value()); + it = cache.insert(typeSignature, metaType.value()); } return it.value(); } AbstractMetaType - ShibokenGenerator::buildAbstractMetaTypeFromTypeEntry(const TypeEntry *typeEntry) const + ShibokenGenerator::buildAbstractMetaTypeFromTypeEntry(const TypeEntry *typeEntry) { QString typeName = typeEntry->qualifiedCppName(); if (typeName.startsWith(QLatin1String("::"))) typeName.remove(0, 2); - if (m_metaTypeFromStringCache.contains(typeName)) - return m_metaTypeFromStringCache.value(typeName); + 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(); - m_metaTypeFromStringCache.insert(typeName, metaType); + cache.insert(typeName, metaType); return metaType; } + AbstractMetaType - ShibokenGenerator::buildAbstractMetaTypeFromAbstractMetaClass(const AbstractMetaClass *metaClass) const + ShibokenGenerator::buildAbstractMetaTypeFromAbstractMetaClass(const AbstractMetaClass *metaClass) { return ShibokenGenerator::buildAbstractMetaTypeFromTypeEntry(metaClass->typeEntry()); } diff --git a/sources/shiboken6/generator/shiboken/shibokengenerator.h b/sources/shiboken6/generator/shiboken/shibokengenerator.h index 3269f3c92..64096db52 100644 --- a/sources/shiboken6/generator/shiboken/shibokengenerator.h +++ b/sources/shiboken6/generator/shiboken/shibokengenerator.h @@ -248,7 +248,7 @@ protected: QString checkFunction; std::optional<AbstractMetaType> type; }; - CPythonCheckFunctionResult guessCPythonCheckFunction(const QString &type) const; + static CPythonCheckFunctionResult guessCPythonCheckFunction(const QString &type); static QString cpythonIsConvertibleFunction(const ApiExtractorResult &api, const TypeEntry *type, bool genericNumberType = false, @@ -337,14 +337,16 @@ protected: * \return A new AbstractMetaType object that must be deleted by the caller, * or a nullptr pointer in case of failure. */ - std::optional<AbstractMetaType> + static std::optional<AbstractMetaType> buildAbstractMetaTypeFromString(QString typeSignature, - QString *errorMessage = nullptr) const; + QString *errorMessage = nullptr); /// Creates an AbstractMetaType object from a TypeEntry. - AbstractMetaType buildAbstractMetaTypeFromTypeEntry(const TypeEntry *typeEntry) const; + static AbstractMetaType + buildAbstractMetaTypeFromTypeEntry(const TypeEntry *typeEntry); /// Creates an AbstractMetaType object from an AbstractMetaClass. - AbstractMetaType buildAbstractMetaTypeFromAbstractMetaClass(const AbstractMetaClass *metaClass) const; + static AbstractMetaType + buildAbstractMetaTypeFromAbstractMetaClass(const AbstractMetaClass *metaClass); static void writeMinimalConstructorExpression(TextStream &s, const ApiExtractorResult &api, const AbstractMetaType &type, @@ -439,14 +441,15 @@ private: /// Utility function for writeCodeSnips. using ArgumentVarReplacementPair = QPair<AbstractMetaArgument, QString>; using ArgumentVarReplacementList = QList<ArgumentVarReplacementPair>; - ArgumentVarReplacementList getArgumentReplacement(const AbstractMetaFunctionCPtr &func, - bool usePyArgs, TypeSystem::Language language, - const AbstractMetaArgument *lastArg) const; + static ArgumentVarReplacementList + getArgumentReplacement(const AbstractMetaFunctionCPtr &func, + bool usePyArgs, TypeSystem::Language language, + const AbstractMetaArgument *lastArg); /// Returns a string with the user's custom code snippets that comply with \p position and \p language. - QString getCodeSnippets(const CodeSnipList &codeSnips, - TypeSystem::CodeSnipPosition position, - TypeSystem::Language language) const; + static QString getCodeSnippets(const CodeSnipList &codeSnips, + TypeSystem::CodeSnipPosition position, + TypeSystem::Language language); enum TypeSystemConverterVariable { TypeSystemCheckFunction = 0, @@ -506,9 +509,6 @@ private: bool m_avoidProtectedHack = false; bool m_wrapperDiagnostics = false; - using AbstractMetaTypeCache = QHash<QString, AbstractMetaType>; - mutable AbstractMetaTypeCache m_metaTypeFromStringCache; - /// Type system converter variable replacement names and regular expressions. static const QHash<int, QString> &typeSystemConvName(); |