1 | // Copyright (C) 2020 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 QQMLJSIMPORTER_P_H |
5 | #define QQMLJSIMPORTER_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 <private/qtqmlcompilerexports_p.h> |
18 | |
19 | #include "qqmljsscope_p.h" |
20 | #include "qqmljsresourcefilemapper_p.h" |
21 | #include <QtQml/private/qqmldirparser_p.h> |
22 | |
23 | #include <memory> |
24 | |
25 | QT_BEGIN_NAMESPACE |
26 | |
27 | class QQmlJSImportVisitor; |
28 | class QQmlJSLogger; |
29 | class Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSImporter |
30 | { |
31 | public: |
32 | using ImportedTypes = QQmlJSScope::ContextualTypes; |
33 | |
34 | QQmlJSImporter(const QStringList &importPaths, QQmlJSResourceFileMapper *mapper, |
35 | bool useOptionalImports = false); |
36 | |
37 | QQmlJSResourceFileMapper *resourceFileMapper() const { return m_mapper; } |
38 | void setResourceFileMapper(QQmlJSResourceFileMapper *mapper) { m_mapper = mapper; } |
39 | |
40 | QQmlJSResourceFileMapper *metaDataMapper() const { return m_metaDataMapper; } |
41 | void setMetaDataMapper(QQmlJSResourceFileMapper *mapper) { m_metaDataMapper = mapper; } |
42 | |
43 | ImportedTypes importBuiltins(); |
44 | void importQmldirs(const QStringList &qmltypesFiles); |
45 | |
46 | QQmlJSScope::Ptr importFile(const QString &file); |
47 | ImportedTypes importDirectory(const QString &directory, const QString &prefix = QString()); |
48 | |
49 | // ### qmltc needs this. once re-written, we no longer need to expose this |
50 | QHash<QString, QQmlJSScope::Ptr> importedFiles() const { return m_importedFiles; } |
51 | |
52 | ImportedTypes importModule(const QString &module, const QString &prefix = QString(), |
53 | QTypeRevision version = QTypeRevision(), |
54 | QStringList *staticModuleList = nullptr); |
55 | |
56 | ImportedTypes builtinInternalNames(); |
57 | |
58 | QList<QQmlJS::DiagnosticMessage> takeWarnings() |
59 | { |
60 | const auto result = std::move(m_warnings); |
61 | m_warnings.clear(); |
62 | return result; |
63 | } |
64 | |
65 | QList<QQmlJS::DiagnosticMessage> takeGlobalWarnings() |
66 | { |
67 | const auto result = std::move(m_globalWarnings); |
68 | m_globalWarnings.clear(); |
69 | return result; |
70 | } |
71 | |
72 | QStringList importPaths() const { return m_importPaths; } |
73 | void setImportPaths(const QStringList &importPaths); |
74 | |
75 | void clearCache(); |
76 | |
77 | QQmlJSScope::ConstPtr jsGlobalObject() const; |
78 | |
79 | std::unique_ptr<QQmlJSImportVisitor> |
80 | makeImportVisitor(const QQmlJSScope::Ptr &target, QQmlJSImporter *importer, |
81 | QQmlJSLogger *logger, const QString &implicitImportDirectory, |
82 | const QStringList &qmldirFiles = QStringList()); |
83 | using ImportVisitorCreator = QQmlJSImportVisitor *(*)(const QQmlJSScope::Ptr &, |
84 | QQmlJSImporter *, QQmlJSLogger *, |
85 | const QString &, const QStringList &); |
86 | void setImportVisitorCreator(ImportVisitorCreator create) { m_createImportVisitor = create; } |
87 | |
88 | private: |
89 | friend class QDeferredFactory<QQmlJSScope>; |
90 | |
91 | struct AvailableTypes |
92 | { |
93 | AvailableTypes(ImportedTypes builtins) |
94 | : cppNames(std::move(builtins)) |
95 | , qmlNames(QQmlJSScope::ContextualTypes::QML, {}, cppNames.arrayType()) |
96 | { |
97 | } |
98 | |
99 | // C++ names used in qmltypes files for non-composite types |
100 | ImportedTypes cppNames; |
101 | |
102 | // Names the importing component sees, including any prefixes |
103 | ImportedTypes qmlNames; |
104 | |
105 | // Static modules included here |
106 | QStringList staticModules; |
107 | |
108 | // Whether a system module has been imported |
109 | bool hasSystemModule = false; |
110 | }; |
111 | |
112 | struct Import { |
113 | QString name; |
114 | bool isStaticModule = false; |
115 | bool isSystemModule = false; |
116 | |
117 | QList<QQmlJSExportedScope> objects; |
118 | QHash<QString, QQmlJSExportedScope> scripts; |
119 | QList<QQmlDirParser::Import> imports; |
120 | QList<QQmlDirParser::Import> dependencies; |
121 | }; |
122 | |
123 | AvailableTypes builtinImportHelper(); |
124 | bool importHelper(const QString &module, AvailableTypes *types, |
125 | const QString &prefix = QString(), QTypeRevision version = QTypeRevision(), |
126 | bool isDependency = false, bool isFile = false); |
127 | void processImport(const QQmlJSScope::Import &importDescription, const Import &import, |
128 | AvailableTypes *types); |
129 | void importDependencies(const QQmlJSImporter::Import &import, AvailableTypes *types, |
130 | const QString &prefix = QString(), |
131 | QTypeRevision version = QTypeRevision(), bool isDependency = false); |
132 | void readQmltypes(const QString &filename, QList<QQmlJSExportedScope> *objects, |
133 | QList<QQmlDirParser::Import> *dependencies); |
134 | Import readQmldir(const QString &dirname); |
135 | Import readDirectory(const QString &directory); |
136 | |
137 | QQmlJSScope::Ptr localFile2ScopeTree(const QString &filePath); |
138 | static void setQualifiedNamesOn(const Import &import); |
139 | |
140 | QStringList m_importPaths; |
141 | |
142 | QHash<QPair<QString, QTypeRevision>, QString> m_seenImports; |
143 | QHash<QQmlJSScope::Import, QSharedPointer<AvailableTypes>> m_cachedImportTypes; |
144 | QHash<QString, Import> m_seenQmldirFiles; |
145 | |
146 | QHash<QString, QQmlJSScope::Ptr> m_importedFiles; |
147 | QList<QQmlJS::DiagnosticMessage> m_globalWarnings; |
148 | QList<QQmlJS::DiagnosticMessage> m_warnings; |
149 | std::optional<AvailableTypes> m_builtins; |
150 | |
151 | QQmlJSResourceFileMapper *m_mapper = nullptr; |
152 | QQmlJSResourceFileMapper *m_metaDataMapper = nullptr; |
153 | bool m_useOptionalImports; |
154 | |
155 | ImportVisitorCreator m_createImportVisitor = nullptr; |
156 | }; |
157 | |
158 | QT_END_NAMESPACE |
159 | |
160 | #endif // QQMLJSIMPORTER_P_H |
161 | |