| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qmltctyperesolver.h" |
| 5 | |
| 6 | #include <private/qqmljsimporter_p.h> |
| 7 | #include <private/qqmljsliteralbindingcheck_p.h> |
| 8 | #include <private/qv4value_p.h> |
| 9 | |
| 10 | #include <QtCore/qqueue.h> |
| 11 | #include <QtCore/qloggingcategory.h> |
| 12 | #include <QtCore/qfileinfo.h> |
| 13 | #include <QtCore/qdiriterator.h> |
| 14 | |
| 15 | Q_STATIC_LOGGING_CATEGORY(lcTypeResolver2, "qml.qmltc.typeresolver" , QtInfoMsg); |
| 16 | |
| 17 | void QmltcTypeResolver::init(QmltcVisitor *visitor, QQmlJS::AST::Node *program) |
| 18 | { |
| 19 | QQmlJSTypeResolver::init(visitor, program); |
| 20 | |
| 21 | m_root = visitor->result(); |
| 22 | |
| 23 | QQueue<QQmlJSScope::Ptr> objects; |
| 24 | objects.enqueue(t: m_root); |
| 25 | while (!objects.isEmpty()) { |
| 26 | const QQmlJSScope::Ptr object = objects.dequeue(); |
| 27 | const QQmlJS::SourceLocation location = object->sourceLocation(); |
| 28 | qCDebug(lcTypeResolver2()).nospace() << "inserting " << object.data() << " at " |
| 29 | << location.startLine << ':' << location.startColumn; |
| 30 | m_objectsByLocationNonConst.insert(key: { location.startLine, location.startColumn }, value: object); |
| 31 | |
| 32 | const auto childScopes = object->childScopes(); |
| 33 | for (const auto &childScope : childScopes) |
| 34 | objects.enqueue(t: childScope); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | QQmlJSScope::Ptr |
| 39 | QmltcTypeResolver::scopeForLocation(const QV4::CompiledData::Location &location) const |
| 40 | { |
| 41 | qCDebug(lcTypeResolver2()).nospace() |
| 42 | << "looking for object at " << location.line() << ':' << location.column(); |
| 43 | return m_objectsByLocationNonConst.value(key: location); |
| 44 | } |
| 45 | |
| 46 | std::pair<QString, QQmlJSScope::Ptr> |
| 47 | QmltcTypeResolver::importedType(const QQmlJSScope::ConstPtr &type) const |
| 48 | { |
| 49 | const auto files = m_importer->importedFiles(); |
| 50 | auto it = std::find_if(first: files.cbegin(), last: files.cend(), pred: [&](const QQmlJSScope::Ptr &importedType) { |
| 51 | return importedType.data() == type.data(); |
| 52 | }); |
| 53 | if (it == files.cend()) |
| 54 | return {}; |
| 55 | return { it.key(), it.value() }; |
| 56 | } |
| 57 | |