| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QQMLDIRPARSER_P_H |
| 5 | #define QQMLDIRPARSER_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 | |
| 18 | #include <QtCore/QUrl> |
| 19 | #include <QtCore/QHash> |
| 20 | #include <QtCore/QDebug> |
| 21 | #include <QtCore/QTypeRevision> |
| 22 | #include <private/qtqmlcompilerglobal_p.h> |
| 23 | #include <private/qqmljsdiagnosticmessage_p.h> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | class QQmlEngine; |
| 28 | class Q_QML_COMPILER_EXPORT QQmlDirParser |
| 29 | { |
| 30 | public: |
| 31 | void clear(); |
| 32 | bool parse(const QString &source); |
| 33 | void disambiguateFileSelectors(); |
| 34 | |
| 35 | bool hasError() const { return !_errors.isEmpty(); } |
| 36 | void setError(const QQmlJS::DiagnosticMessage &); |
| 37 | QList<QQmlJS::DiagnosticMessage> errors(const QString &uri) const; |
| 38 | |
| 39 | QString typeNamespace() const { return _typeNamespace; } |
| 40 | void setTypeNamespace(const QString &s) { _typeNamespace = s; } |
| 41 | |
| 42 | static void checkNonRelative(const char *item, const QString &typeName, const QString &fileName) |
| 43 | { |
| 44 | if (fileName.startsWith(c: QLatin1Char('/'))) { |
| 45 | qWarning() << item << typeName |
| 46 | << "is specified with non-relative URL" << fileName << "in a qmldir file." |
| 47 | << "URLs in qmldir files should be relative to the qmldir file's directory." ; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | struct Plugin |
| 52 | { |
| 53 | Plugin() = default; |
| 54 | |
| 55 | Plugin(const QString &name, const QString &path, bool optional) |
| 56 | : name(name), path(path), optional(optional) |
| 57 | { |
| 58 | checkNonRelative(item: "Plugin" , typeName: name, fileName: path); |
| 59 | } |
| 60 | |
| 61 | QString name; |
| 62 | QString path; |
| 63 | bool optional = false; |
| 64 | }; |
| 65 | |
| 66 | struct Component |
| 67 | { |
| 68 | Component() = default; |
| 69 | |
| 70 | Component(const QString &typeName, const QString &fileName, QTypeRevision version) |
| 71 | : typeName(typeName), fileName(fileName), version(version), |
| 72 | internal(false), singleton(false) |
| 73 | { |
| 74 | checkNonRelative(item: "Component" , typeName, fileName); |
| 75 | } |
| 76 | |
| 77 | QString typeName; |
| 78 | QString fileName; |
| 79 | QTypeRevision version = QTypeRevision::zero(); |
| 80 | bool internal = false; |
| 81 | bool singleton = false; |
| 82 | }; |
| 83 | |
| 84 | struct Script |
| 85 | { |
| 86 | Script() = default; |
| 87 | |
| 88 | Script(const QString &nameSpace, const QString &fileName, QTypeRevision version) |
| 89 | : nameSpace(nameSpace), fileName(fileName), version(version) |
| 90 | { |
| 91 | checkNonRelative(item: "Script" , typeName: nameSpace, fileName); |
| 92 | } |
| 93 | |
| 94 | QString nameSpace; |
| 95 | QString fileName; |
| 96 | QTypeRevision version = QTypeRevision::zero(); |
| 97 | }; |
| 98 | |
| 99 | struct Import |
| 100 | { |
| 101 | enum Flag { |
| 102 | Default = 0x0, |
| 103 | Auto = 0x1, // forward the version of the importing module |
| 104 | Optional = 0x2, // is not automatically imported but only a tooling hint |
| 105 | OptionalDefault = |
| 106 | 0x4, // tooling hint only, denotes this entry should be imported by tooling |
| 107 | }; |
| 108 | Q_DECLARE_FLAGS(Flags, Flag) |
| 109 | |
| 110 | Import() = default; |
| 111 | Import(QString module, QTypeRevision version, Flags flags) |
| 112 | : module(module), version(version), flags(flags) |
| 113 | { |
| 114 | } |
| 115 | |
| 116 | QString module; |
| 117 | QTypeRevision version; // invalid version is latest version, unless Flag::Auto |
| 118 | Flags flags; |
| 119 | |
| 120 | friend bool operator==(const Import &a, const Import &b) |
| 121 | { |
| 122 | return a.module == b.module && a.version == b.version && a.flags == b.flags; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | QMultiHash<QString,Component> components() const { return _components; } |
| 127 | QList<Import> dependencies() const { return _dependencies; } |
| 128 | QList<Import> imports() const { return _imports; } |
| 129 | QList<Script> scripts() const { return _scripts; } |
| 130 | QList<Plugin> plugins() const { return _plugins; } |
| 131 | bool designerSupported() const { return _designerSupported; } |
| 132 | |
| 133 | // A static module has side effects outside the mere importing of types. We shall not warn |
| 134 | // about it being being "unused". The builtins are also a static module. |
| 135 | bool isStaticModule() const { return _isStaticModule; } |
| 136 | |
| 137 | // A system module includes the JavaScript root object |
| 138 | bool isSystemModule() const { return _isSystemModule; } |
| 139 | |
| 140 | QStringList typeInfos() const { return _typeInfos; } |
| 141 | QStringList classNames() const { return _classNames; } |
| 142 | QString preferredPath() const { return _preferredPath; } |
| 143 | QString linkTarget() const { return _linkTarget; } |
| 144 | |
| 145 | private: |
| 146 | bool maybeAddComponent(const QString &typeName, const QString &fileName, const QString &version, QHash<QString,Component> &hash, int lineNumber = -1, bool multi = true); |
| 147 | void reportError(quint16 line, quint16 column, const QString &message); |
| 148 | void insertComponentOrScript( |
| 149 | const QString &name, const QString &fileName, QTypeRevision version); |
| 150 | |
| 151 | private: |
| 152 | QList<QQmlJS::DiagnosticMessage> _errors; |
| 153 | QString _typeNamespace; |
| 154 | QString _preferredPath; |
| 155 | QMultiHash<QString,Component> _components; |
| 156 | QList<Import> _dependencies; |
| 157 | QList<Import> _imports; |
| 158 | QList<Script> _scripts; |
| 159 | QList<Plugin> _plugins; |
| 160 | bool _designerSupported = false; |
| 161 | bool _isStaticModule = false; |
| 162 | bool _isSystemModule = false; |
| 163 | QStringList _typeInfos; |
| 164 | QStringList _classNames; |
| 165 | QString _linkTarget; |
| 166 | }; |
| 167 | |
| 168 | using QQmlDirComponents = QMultiHash<QString,QQmlDirParser::Component>; |
| 169 | using QQmlDirScripts = QList<QQmlDirParser::Script>; |
| 170 | using QQmlDirPlugins = QList<QQmlDirParser::Plugin>; |
| 171 | using QQmlDirImports = QList<QQmlDirParser::Import>; |
| 172 | |
| 173 | QDebug &operator<< (QDebug &, const QQmlDirParser::Component &); |
| 174 | QDebug &operator<< (QDebug &, const QQmlDirParser::Script &); |
| 175 | |
| 176 | QT_END_NAMESPACE |
| 177 | |
| 178 | #endif // QQMLDIRPARSER_P_H |
| 179 | |