diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-07-05 12:54:25 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2018-07-11 19:01:58 +0000 |
commit | 612bfd01e1e7698cb3a06d619cc7d7c7acd3d273 (patch) | |
tree | dd9aa2aacd81e9045ba6708a4117e503cf3f9bca /sources/shiboken2/ApiExtractor/parser | |
parent | f2443b02fce5dd4182e80ed0e5343586cbd72a31 (diff) |
shiboken: Move detection of template arguments to ClangBuilder
Obtain the template arguments from Clang and fall back to parsing
the type name where this is not possible (which may be the case
inside a template declaration).
The string-based formatting and re-parsing of the type in
AbstractMetaBuilder::translateType() can then be removed, opening
the way to passing up more complex types from Clang into the
MetaBuilder.
Task-number: PYSIDE-672
Change-Id: I43ff285c5f3720319bf40c65b1c27302ef1b934e
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2/ApiExtractor/parser')
-rw-r--r-- | sources/shiboken2/ApiExtractor/parser/codemodel.cpp | 49 | ||||
-rw-r--r-- | sources/shiboken2/ApiExtractor/parser/codemodel.h | 5 |
2 files changed, 54 insertions, 0 deletions
diff --git a/sources/shiboken2/ApiExtractor/parser/codemodel.cpp b/sources/shiboken2/ApiExtractor/parser/codemodel.cpp index d3ada9235..a560c0faf 100644 --- a/sources/shiboken2/ApiExtractor/parser/codemodel.cpp +++ b/sources/shiboken2/ApiExtractor/parser/codemodel.cpp @@ -29,11 +29,15 @@ #include "codemodel.h" + +#include <clangparser/clangutils.h> + #include <algorithm> #include <functional> #include <iostream> #include <QDebug> #include <QDir> +#include <QtCore/QStack> // Predicate to find an item by name in a list of QSharedPointer<Item> template <class T> class ModelItemNamePredicate : public std::unary_function<bool, QSharedPointer<T> > @@ -195,6 +199,51 @@ TypeInfo TypeInfo::resolveType(CodeModelItem __item, TypeInfo const &__type, Cod return otherType; } +// Handler for clang::parseTemplateArgumentList() that populates +// TypeInfo::m_instantiations +class TypeInfoTemplateArgumentHandler : + public std::binary_function<void, int, const QStringRef &> +{ +public: + explicit TypeInfoTemplateArgumentHandler(TypeInfo *t) + { + m_parseStack.append(t); + } + + void operator()(int level, const QStringRef &name) + { + if (level > m_parseStack.size()) { + Q_ASSERT(!top()->m_instantiations.isEmpty()); + m_parseStack.push(&top()->m_instantiations.back()); + } + while (level < m_parseStack.size()) + m_parseStack.pop(); + TypeInfo instantiation; + instantiation.setQualifiedName(qualifiedName(name)); + top()->addInstantiation(instantiation); + } + +private: + TypeInfo *top() const { return m_parseStack.back(); } + + static QStringList qualifiedName(const QStringRef &name) + { + QStringList result; + const QVector<QStringRef> nameParts = name.split(QLatin1String("::")); + result.reserve(nameParts.size()); + for (const QStringRef &p : nameParts) + result.append(p.toString()); + return result; + } + + QStack<TypeInfo *> m_parseStack; +}; + +QPair<int, int> TypeInfo::parseTemplateArgumentList(const QString &l, int from) +{ + return clang::parseTemplateArgumentList(l, clang::TemplateArgumentHandler(TypeInfoTemplateArgumentHandler(this)), from); +} + QString TypeInfo::toString() const { QString tmp; diff --git a/sources/shiboken2/ApiExtractor/parser/codemodel.h b/sources/shiboken2/ApiExtractor/parser/codemodel.h index d0381c4e3..c61f640e1 100644 --- a/sources/shiboken2/ApiExtractor/parser/codemodel.h +++ b/sources/shiboken2/ApiExtractor/parser/codemodel.h @@ -36,6 +36,7 @@ #include "enumvalue.h" #include <QtCore/QHash> +#include <QtCore/QPair> #include <QtCore/QSet> #include <QtCore/QString> #include <QtCore/QStringList> @@ -185,6 +186,8 @@ public: void addInstantiation(const TypeInfo &i) { m_instantiations.append(i); } void clearInstantiations() { m_instantiations.clear(); } + QPair<int, int> parseTemplateArgumentList(const QString &l, int from = 0); + bool operator==(const TypeInfo &other) const; bool operator!=(const TypeInfo &other) const @@ -210,6 +213,8 @@ public: static bool stripLeadingQualifier(const QString &qualifier, QString *s); private: + friend class TypeInfoTemplateArgumentHandler; + static TypeInfo resolveType(CodeModelItem item, TypeInfo const &__type, CodeModelItem __scope); QStringList m_qualifiedName; |