| 1 | // Copyright (C) 2023 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #ifndef QQMLJSCONTEXTUALTYPES_P_H |
| 5 | #define QQMLJSCONTEXTUALTYPES_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | |
| 17 | #include <QtCore/qstring.h> |
| 18 | #include <QtCore/qhash.h> |
| 19 | #include <private/qqmljsscope_p.h> |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | namespace QQmlJS { |
| 24 | /*! \internal |
| 25 | * Maps type names to types and the compile context of the types. The context can be |
| 26 | * INTERNAL (for c++ and synthetic jsrootgen types) or QML (for qml types). |
| 27 | */ |
| 28 | struct ContextualTypes |
| 29 | { |
| 30 | enum CompileContext { INTERNAL, QML }; |
| 31 | |
| 32 | ContextualTypes( |
| 33 | CompileContext context, |
| 34 | const QHash<QString, ImportedScope<QQmlJSScope::ConstPtr>> &types, |
| 35 | const QMultiHash<QQmlJSScope::ConstPtr, QString> &names, |
| 36 | const QQmlJSScope::ConstPtr &arrayType) |
| 37 | : m_types(types) |
| 38 | , m_names(names) |
| 39 | , m_context(context) |
| 40 | , m_arrayType(arrayType) |
| 41 | {} |
| 42 | |
| 43 | CompileContext context() const { return m_context; } |
| 44 | QQmlJSScope::ConstPtr arrayType() const { return m_arrayType; } |
| 45 | |
| 46 | bool hasType(const QString &name) const { return m_types.contains(key: name); } |
| 47 | |
| 48 | ImportedScope<QQmlJSScope::ConstPtr> type(const QString &name) const { return m_types[name]; } |
| 49 | QString name(const QQmlJSScope::ConstPtr &type) const { return m_names[type]; } |
| 50 | |
| 51 | void setType(const QString &name, const ImportedScope<QQmlJSScope::ConstPtr> &type) |
| 52 | { |
| 53 | if (!name.startsWith(c: u'$')) |
| 54 | m_names.insert(key: type.scope, value: name); |
| 55 | m_types.insert(key: name, value: type); |
| 56 | } |
| 57 | void clearType(const QString &name) |
| 58 | { |
| 59 | auto &scope = m_types[name].scope; |
| 60 | auto it = m_names.constFind(key: scope); |
| 61 | while (it != m_names.constEnd() && it.key() == scope) |
| 62 | it = m_names.erase(it); |
| 63 | scope = QQmlJSScope::ConstPtr(); |
| 64 | } |
| 65 | |
| 66 | bool isNullType(const QString &name) const |
| 67 | { |
| 68 | const auto it = m_types.constFind(key: name); |
| 69 | return it != m_types.constEnd() && it->scope.isNull(); |
| 70 | } |
| 71 | |
| 72 | void addTypes(ContextualTypes &&types) |
| 73 | { |
| 74 | Q_ASSERT(types.m_context == m_context); |
| 75 | insertNames(types); |
| 76 | m_types.insert(hash: std::move(types.m_types)); |
| 77 | } |
| 78 | |
| 79 | void addTypes(const ContextualTypes &types) |
| 80 | { |
| 81 | Q_ASSERT(types.m_context == m_context); |
| 82 | insertNames(types); |
| 83 | m_types.insert(hash: types.m_types); |
| 84 | } |
| 85 | |
| 86 | const QHash<QString, ImportedScope<QQmlJSScope::ConstPtr>> &types() const { return m_types; } |
| 87 | const auto &names() const { return m_names; } |
| 88 | |
| 89 | void clearTypes() |
| 90 | { |
| 91 | m_names.clear(); |
| 92 | m_types.clear(); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | void insertNames(const ContextualTypes &types) { |
| 97 | for (auto it = types.m_types.constBegin(), end = types.m_types.constEnd(); |
| 98 | it != end; ++it) { |
| 99 | const QString &name = it.key(); |
| 100 | if (!name.startsWith(c: u'$')) |
| 101 | m_names.insert(key: it->scope, value: name); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | QHash<QString, ImportedScope<QQmlJSScope::ConstPtr>> m_types; |
| 106 | QMultiHash<QQmlJSScope::ConstPtr, QString> m_names; |
| 107 | CompileContext m_context; |
| 108 | |
| 109 | // For resolving sequence types |
| 110 | QQmlJSScope::ConstPtr m_arrayType; |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | QT_END_NAMESPACE |
| 115 | |
| 116 | #endif // QQMLJSCONTEXTUALTYPES_P_H |
| 117 | |