From 28f76672a9d7bb38fd596ea27d07396dcedfc6e7 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 10 Apr 2019 14:40:40 +0200 Subject: shiboken: Refactor CodeModel::findItem() Rewrite it to use a recursion, preparing for having several namespaces of the same name in the scope. Change the scope parameter to be of the ScopeModelItem type to avoid unnessary pointer casts. Change-Id: I9a06ff49d19f93fbc0aefc573544debeb813ef20 Reviewed-by: Cristian Maureira-Fredes --- .../shiboken2/ApiExtractor/parser/codemodel.cpp | 56 +++++++++++----------- sources/shiboken2/ApiExtractor/parser/codemodel.h | 6 +-- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/sources/shiboken2/ApiExtractor/parser/codemodel.cpp b/sources/shiboken2/ApiExtractor/parser/codemodel.cpp index 8bc9b24ac..9a845b04a 100644 --- a/sources/shiboken2/ApiExtractor/parser/codemodel.cpp +++ b/sources/shiboken2/ApiExtractor/parser/codemodel.cpp @@ -83,37 +83,35 @@ FileModelItem CodeModel::findFile(const QString &name) const return findModelItem(m_files, name); } -CodeModelItem CodeModel::findItem(const QStringList &qualifiedName, CodeModelItem scope) const -{ - for (int i = 0; i < qualifiedName.size(); ++i) { - // ### Extend to look for members etc too. - const QString &name = qualifiedName.at(i); - - if (NamespaceModelItem ns = qSharedPointerDynamicCast<_NamespaceModelItem>(scope)) { - if (NamespaceModelItem tmp_ns = ns->findNamespace(name)) { - scope = tmp_ns; - continue; - } - } - - if (ScopeModelItem ss = qSharedPointerDynamicCast<_ScopeModelItem>(scope)) { - if (ClassModelItem cs = ss->findClass(name)) { - scope = cs; - } else if (EnumModelItem es = ss->findEnum(name)) { - if (i == qualifiedName.size() - 1) - return es; - } else if (TypeDefModelItem tp = ss->findTypeDef(name)) { - if (i == qualifiedName.size() - 1) - return tp; - } else { - // If we don't find the name in the scope chain we - // need to return an empty item to indicate failure... - return CodeModelItem(); +static CodeModelItem findRecursion(const ScopeModelItem &scope, + const QStringList &qualifiedName, int segment = 0) +{ + const QString &nameSegment = qualifiedName.at(segment); + if (segment == qualifiedName.size() - 1) { // Leaf item + if (ClassModelItem cs = scope->findClass(nameSegment)) + return cs; + if (EnumModelItem es = scope->findEnum(nameSegment)) + return es; + if (TypeDefModelItem tp = scope->findTypeDef(nameSegment)) + return tp; + return CodeModelItem(); + } + if (auto nestedClass = scope->findClass(nameSegment)) + return findRecursion(nestedClass, qualifiedName, segment + 1); + if (auto namespaceItem = qSharedPointerDynamicCast<_NamespaceModelItem>(scope)) { + for (const auto &nestedNamespace : namespaceItem->namespaces()) { + if (nestedNamespace->name() == nameSegment) { + if (auto item = findRecursion(nestedNamespace, qualifiedName, segment + 1)) + return item; } } } + return CodeModelItem(); +} - return scope; +CodeModelItem CodeModel::findItem(const QStringList &qualifiedName, const ScopeModelItem &scope) const +{ + return findRecursion(scope, qualifiedName); } #ifndef QT_NO_DEBUG_STREAM @@ -160,7 +158,7 @@ bool TypeInfo::isVoid() const && m_qualifiedName.constFirst() == QLatin1String("void"); } -TypeInfo TypeInfo::resolveType(TypeInfo const &__type, CodeModelItem __scope) +TypeInfo TypeInfo::resolveType(TypeInfo const &__type, const ScopeModelItem &__scope) { CodeModel *__model = __scope->model(); Q_ASSERT(__model != 0); @@ -168,7 +166,7 @@ TypeInfo TypeInfo::resolveType(TypeInfo const &__type, CodeModelItem __scope) return TypeInfo::resolveType(__model->findItem(__type.qualifiedName(), __scope), __type, __scope); } -TypeInfo TypeInfo::resolveType(CodeModelItem __item, TypeInfo const &__type, CodeModelItem __scope) +TypeInfo TypeInfo::resolveType(CodeModelItem __item, TypeInfo const &__type, const ScopeModelItem &__scope) { // Copy the type and replace with the proper qualified name. This // only makes sence to do if we're actually getting a resolved diff --git a/sources/shiboken2/ApiExtractor/parser/codemodel.h b/sources/shiboken2/ApiExtractor/parser/codemodel.h index 0296a8cb2..64415e07b 100644 --- a/sources/shiboken2/ApiExtractor/parser/codemodel.h +++ b/sources/shiboken2/ApiExtractor/parser/codemodel.h @@ -82,7 +82,7 @@ public: void addFile(FileModelItem item); FileModelItem findFile(const QString &name) const; - CodeModelItem findItem(const QStringList &qualifiedName, CodeModelItem scope) const; + CodeModelItem findItem(const QStringList &qualifiedName, const ScopeModelItem &scope) const; private: FileList m_files; @@ -202,7 +202,7 @@ public: QString toString() const; static TypeInfo combine(const TypeInfo &__lhs, const TypeInfo &__rhs); - static TypeInfo resolveType(TypeInfo const &__type, CodeModelItem __scope); + static TypeInfo resolveType(TypeInfo const &__type, const ScopeModelItem &__scope); #ifndef QT_NO_DEBUG_STREAM void formatDebug(QDebug &d) const; @@ -219,7 +219,7 @@ public: private: friend class TypeInfoTemplateArgumentHandler; - static TypeInfo resolveType(CodeModelItem item, TypeInfo const &__type, CodeModelItem __scope); + static TypeInfo resolveType(CodeModelItem item, TypeInfo const &__type, const ScopeModelItem &__scope); QStringList m_qualifiedName; QStringList m_arrayElements; -- cgit v1.2.3