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 QQmlJSScope::ConstPtr &arrayType) |
36 | : m_types(types) |
37 | , m_context(context) |
38 | , m_arrayType(arrayType) |
39 | {} |
40 | |
41 | CompileContext context() const { return m_context; } |
42 | QQmlJSScope::ConstPtr arrayType() const { return m_arrayType; } |
43 | |
44 | bool hasType(const QString &name) const { return m_types.contains(key: name); } |
45 | |
46 | ImportedScope<QQmlJSScope::ConstPtr> type(const QString &name) const { return m_types[name]; } |
47 | QString name(const QQmlJSScope::ConstPtr &type) const { return m_names[type]; } |
48 | |
49 | void setType(const QString &name, const ImportedScope<QQmlJSScope::ConstPtr> &type) |
50 | { |
51 | if (!name.startsWith(c: u'$')) |
52 | m_names.insert(key: type.scope, value: name); |
53 | m_types.insert(key: name, value: type); |
54 | } |
55 | void clearType(const QString &name) |
56 | { |
57 | auto &scope = m_types[name].scope; |
58 | auto it = m_names.constFind(key: scope); |
59 | while (it != m_names.constEnd() && it.key() == scope) |
60 | it = m_names.erase(it); |
61 | scope = QQmlJSScope::ConstPtr(); |
62 | } |
63 | |
64 | bool isNullType(const QString &name) const |
65 | { |
66 | const auto it = m_types.constFind(key: name); |
67 | return it != m_types.constEnd() && it->scope.isNull(); |
68 | } |
69 | |
70 | void addTypes(ContextualTypes &&types) |
71 | { |
72 | Q_ASSERT(types.m_context == m_context); |
73 | insertNames(types); |
74 | m_types.insert(hash: std::move(types.m_types)); |
75 | } |
76 | |
77 | void addTypes(const ContextualTypes &types) |
78 | { |
79 | Q_ASSERT(types.m_context == m_context); |
80 | insertNames(types); |
81 | m_types.insert(hash: types.m_types); |
82 | } |
83 | |
84 | const QHash<QString, ImportedScope<QQmlJSScope::ConstPtr>> &types() const { return m_types; } |
85 | const auto &names() const { return m_names; } |
86 | |
87 | void clearTypes() |
88 | { |
89 | m_names.clear(); |
90 | m_types.clear(); |
91 | } |
92 | |
93 | private: |
94 | void insertNames(const ContextualTypes &types) { |
95 | for (auto it = types.m_types.constBegin(), end = types.m_types.constEnd(); |
96 | it != end; ++it) { |
97 | const QString &name = it.key(); |
98 | if (!name.startsWith(c: u'$')) |
99 | m_names.insert(key: it->scope, value: name); |
100 | } |
101 | } |
102 | |
103 | QHash<QString, ImportedScope<QQmlJSScope::ConstPtr>> m_types; |
104 | QMultiHash<QQmlJSScope::ConstPtr, QString> m_names; |
105 | CompileContext m_context; |
106 | |
107 | // For resolving sequence types |
108 | QQmlJSScope::ConstPtr m_arrayType; |
109 | }; |
110 | } |
111 | |
112 | QT_END_NAMESPACE |
113 | |
114 | #endif // QQMLJSCONTEXTUALTYPES_P_H |
115 | |