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
25QT_BEGIN_NAMESPACE
26
27class QQmlEngine;
28class Q_QML_COMPILER_PRIVATE_EXPORT QQmlDirParser
29{
30public:
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 bool isStaticModule() const { return _isStaticModule; }
133 bool isSystemModule() const { return _isSystemModule; }
134
135 QStringList typeInfos() const { return _typeInfos; }
136 QStringList classNames() const { return _classNames; }
137 QString preferredPath() const { return _preferredPath; }
138 QString linkTarget() const { return _linkTarget; }
139
140private:
141 bool maybeAddComponent(const QString &typeName, const QString &fileName, const QString &version, QHash<QString,Component> &hash, int lineNumber = -1, bool multi = true);
142 void reportError(quint16 line, quint16 column, const QString &message);
143
144private:
145 QList<QQmlJS::DiagnosticMessage> _errors;
146 QString _typeNamespace;
147 QString _preferredPath;
148 QMultiHash<QString,Component> _components;
149 QList<Import> _dependencies;
150 QList<Import> _imports;
151 QList<Script> _scripts;
152 QList<Plugin> _plugins;
153 bool _designerSupported = false;
154 bool _isStaticModule = false;
155 bool _isSystemModule = false;
156 QStringList _typeInfos;
157 QStringList _classNames;
158 QString _linkTarget;
159};
160
161using QQmlDirComponents = QMultiHash<QString,QQmlDirParser::Component>;
162using QQmlDirScripts = QList<QQmlDirParser::Script>;
163using QQmlDirPlugins = QList<QQmlDirParser::Plugin>;
164using QQmlDirImports = QList<QQmlDirParser::Import>;
165
166QDebug &operator<< (QDebug &, const QQmlDirParser::Component &);
167QDebug &operator<< (QDebug &, const QQmlDirParser::Script &);
168
169QT_END_NAMESPACE
170
171#endif // QQMLDIRPARSER_P_H
172

source code of qtdeclarative/src/qml/qmldirparser/qqmldirparser_p.h