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_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 | QQmlJSLiteralBindingCheck literalCheck; |
22 | literalCheck.run(visitor, resolver: this); |
23 | |
24 | m_root = visitor->result(); |
25 | |
26 | QQueue<QQmlJSScope::Ptr> objects; |
27 | objects.enqueue(t: m_root); |
28 | while (!objects.isEmpty()) { |
29 | const QQmlJSScope::Ptr object = objects.dequeue(); |
30 | const QQmlJS::SourceLocation location = object->sourceLocation(); |
31 | qCDebug(lcTypeResolver2()).nospace() << "inserting " << object.data() << " at " |
32 | << location.startLine << ':' << location.startColumn; |
33 | m_objectsByLocationNonConst.insert(key: { location.startLine, location.startColumn }, value: object); |
34 | |
35 | const auto childScopes = object->childScopes(); |
36 | for (const auto &childScope : childScopes) |
37 | objects.enqueue(t: childScope); |
38 | } |
39 | } |
40 | |
41 | QQmlJSScope::Ptr |
42 | QmltcTypeResolver::scopeForLocation(const QV4::CompiledData::Location &location) const |
43 | { |
44 | qCDebug(lcTypeResolver2()).nospace() |
45 | << "looking for object at " << location.line() << ':' << location.column(); |
46 | return m_objectsByLocationNonConst.value(key: location); |
47 | } |
48 | |
49 | QPair<QString, QQmlJSScope::Ptr> |
50 | QmltcTypeResolver::importedType(const QQmlJSScope::ConstPtr &type) const |
51 | { |
52 | const auto files = m_importer->importedFiles(); |
53 | auto it = std::find_if(first: files.cbegin(), last: files.cend(), pred: [&](const QQmlJSScope::Ptr &importedType) { |
54 | return importedType.data() == type.data(); |
55 | }); |
56 | if (it == files.cend()) |
57 | return {}; |
58 | return { it.key(), it.value() }; |
59 | } |
60 | |