1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qqmltypenamecache_p.h" |
41 | |
42 | #include "qqmlengine_p.h" |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | QQmlTypeNameCache::QQmlTypeNameCache(const QQmlImports &importCache) |
47 | : m_imports(importCache) |
48 | { |
49 | } |
50 | |
51 | QQmlTypeNameCache::~QQmlTypeNameCache() |
52 | { |
53 | } |
54 | |
55 | void QQmlTypeNameCache::add(const QHashedString &name, const QUrl &url, const QHashedString &nameSpace) |
56 | { |
57 | if (nameSpace.length() != 0) { |
58 | QQmlImportRef *i = m_namedImports.value(key: nameSpace); |
59 | Q_ASSERT(i != nullptr); |
60 | i->compositeSingletons.insert(key: name, value: url); |
61 | return; |
62 | } |
63 | |
64 | if (m_anonymousCompositeSingletons.contains(key: name)) |
65 | return; |
66 | |
67 | m_anonymousCompositeSingletons.insert(key: name, value: url); |
68 | } |
69 | |
70 | void QQmlTypeNameCache::add(const QHashedString &name, int importedScriptIndex, const QHashedString &nameSpace) |
71 | { |
72 | QQmlImportRef import; |
73 | import.scriptIndex = importedScriptIndex; |
74 | import.m_qualifier = name; |
75 | |
76 | if (nameSpace.length() != 0) { |
77 | QQmlImportRef *i = m_namedImports.value(key: nameSpace); |
78 | Q_ASSERT(i != nullptr); |
79 | m_namespacedImports[i].insert(key: name, value: import); |
80 | return; |
81 | } |
82 | |
83 | if (m_namedImports.contains(key: name)) |
84 | return; |
85 | |
86 | m_namedImports.insert(key: name, value: import); |
87 | } |
88 | |
89 | QQmlTypeNameCache::Result QQmlTypeNameCache::query(const QHashedStringRef &name) const |
90 | { |
91 | Result result = query(imports: m_namedImports, key: name); |
92 | |
93 | if (!result.isValid()) |
94 | result = typeSearch(modules: m_anonymousImports, key: name); |
95 | |
96 | if (!result.isValid()) |
97 | result = query(urls: m_anonymousCompositeSingletons, key: name); |
98 | |
99 | if (!result.isValid()) { |
100 | // Look up anonymous types from the imports of this document |
101 | QQmlImportNamespace *typeNamespace = nullptr; |
102 | QList<QQmlError> errors; |
103 | QQmlType t; |
104 | bool typeFound = m_imports.resolveType(type: name, type_return: &t, version_major: nullptr, version_minor: nullptr, ns_return: &typeNamespace, errors: &errors); |
105 | if (typeFound) { |
106 | return Result(t); |
107 | } |
108 | |
109 | } |
110 | |
111 | return result; |
112 | } |
113 | |
114 | QQmlTypeNameCache::Result QQmlTypeNameCache::query(const QHashedStringRef &name, |
115 | const QQmlImportRef *importNamespace) const |
116 | { |
117 | Q_ASSERT(importNamespace && importNamespace->scriptIndex == -1); |
118 | |
119 | Result result = typeSearch(modules: importNamespace->modules, key: name); |
120 | |
121 | if (!result.isValid()) |
122 | result = query(urls: importNamespace->compositeSingletons, key: name); |
123 | |
124 | if (!result.isValid()) { |
125 | // Look up types from the imports of this document |
126 | // ### it would be nice if QQmlImports allowed us to resolve a namespace |
127 | // first, and then types on it. |
128 | QString qualifiedTypeName = importNamespace->m_qualifier + QLatin1Char('.') + name.toString(); |
129 | QQmlImportNamespace *typeNamespace = nullptr; |
130 | QList<QQmlError> errors; |
131 | QQmlType t; |
132 | bool typeFound = m_imports.resolveType(type: qualifiedTypeName, type_return: &t, version_major: nullptr, version_minor: nullptr, ns_return: &typeNamespace, errors: &errors); |
133 | if (typeFound) { |
134 | return Result(t); |
135 | } |
136 | } |
137 | |
138 | return result; |
139 | } |
140 | |
141 | QQmlTypeNameCache::Result QQmlTypeNameCache::query(const QV4::String *name, QQmlImport::RecursionRestriction recursionRestriction) const |
142 | { |
143 | Result result = query(imports: m_namedImports, key: name); |
144 | |
145 | if (!result.isValid()) |
146 | result = typeSearch(modules: m_anonymousImports, key: name); |
147 | |
148 | if (!result.isValid()) |
149 | result = query(urls: m_anonymousCompositeSingletons, key: name); |
150 | |
151 | if (!result.isValid()) { |
152 | // Look up anonymous types from the imports of this document |
153 | QString typeName = name->toQStringNoThrow(); |
154 | QQmlImportNamespace *typeNamespace = nullptr; |
155 | QList<QQmlError> errors; |
156 | QQmlType t; |
157 | bool typeRecursionDetected = false; |
158 | bool typeFound = m_imports.resolveType(type: typeName, type_return: &t, version_major: nullptr, version_minor: nullptr, ns_return: &typeNamespace, errors: &errors, |
159 | registrationType: QQmlType::AnyRegistrationType, |
160 | typeRecursionDetected: recursionRestriction == QQmlImport::AllowRecursion ? &typeRecursionDetected : nullptr); |
161 | if (typeFound) { |
162 | return Result(t); |
163 | } |
164 | |
165 | } |
166 | |
167 | return result; |
168 | } |
169 | |
170 | QQmlTypeNameCache::Result QQmlTypeNameCache::query(const QV4::String *name, const QQmlImportRef *importNamespace) const |
171 | { |
172 | Q_ASSERT(importNamespace && importNamespace->scriptIndex == -1); |
173 | |
174 | QMap<const QQmlImportRef *, QStringHash<QQmlImportRef> >::const_iterator it = m_namespacedImports.constFind(akey: importNamespace); |
175 | if (it != m_namespacedImports.constEnd()) { |
176 | Result r = query(imports: *it, key: name); |
177 | if (r.isValid()) |
178 | return r; |
179 | } |
180 | |
181 | Result r = typeSearch(modules: importNamespace->modules, key: name); |
182 | |
183 | if (!r.isValid()) |
184 | r = query(urls: importNamespace->compositeSingletons, key: name); |
185 | |
186 | if (!r.isValid()) { |
187 | // Look up types from the imports of this document |
188 | // ### it would be nice if QQmlImports allowed us to resolve a namespace |
189 | // first, and then types on it. |
190 | QString qualifiedTypeName = importNamespace->m_qualifier + QLatin1Char('.') + name->toQStringNoThrow(); |
191 | QQmlImportNamespace *typeNamespace = nullptr; |
192 | QList<QQmlError> errors; |
193 | QQmlType t; |
194 | bool typeFound = m_imports.resolveType(type: qualifiedTypeName, type_return: &t, version_major: nullptr, version_minor: nullptr, ns_return: &typeNamespace, errors: &errors); |
195 | if (typeFound) { |
196 | return Result(t); |
197 | } |
198 | } |
199 | |
200 | return r; |
201 | } |
202 | |
203 | QT_END_NAMESPACE |
204 | |
205 | |