aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmldom/qqmldomexternalitems_p.h
blob: 8072d2fa8acce33c79d766788e4217f404b00ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QQMLDOMEXTERNALITEMS_P_H
#define QQMLDOMEXTERNALITEMS_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qqmldomitem_p.h"
#include "qqmldomelements_p.h"
#include "qqmldommoduleindex_p.h"
#include "qqmldomcomments_p.h"

#include <QtQml/private/qqmljsast_p.h>
#include <QtQml/private/qqmljsengine_p.h>
#include <QtQml/private/qqmldirparser_p.h>
#include <QtCore/QMetaType>

#include <limits>

Q_DECLARE_METATYPE(QQmlDirParser::Plugin)

QT_BEGIN_NAMESPACE

namespace QQmlJS {
namespace Dom {

/*!
\internal
\class QQmlJS::Dom::ExternalOwningItem

\brief A OwningItem that refers to an external resource (file,...)

Every owning item has a file or directory it refers to.


*/
class QMLDOM_EXPORT ExternalOwningItem: public OwningItem {
public:
    ExternalOwningItem(QString filePath, QDateTime lastDataUpdateAt, Path pathFromTop,
                       int derivedFrom = 0, QString code = QString());
    ExternalOwningItem(const ExternalOwningItem &o) = default;
    QString canonicalFilePath(DomItem &) const override;
    QString canonicalFilePath() const;
    Path canonicalPath(DomItem &) const override;
    Path canonicalPath() const;
    bool iterateDirectSubpaths(DomItem &self, DirectVisitor visitor) override
    {
        bool cont = OwningItem::iterateDirectSubpaths(self, visitor);
        cont = cont && self.dvValueLazyField(visitor, Fields::canonicalFilePath, [this]() {
            return canonicalFilePath();
        });
        cont = cont
                && self.dvValueLazyField(visitor, Fields::isValid, [this]() { return isValid(); });
        if (!code().isNull())
            cont = cont
                    && self.dvValueLazyField(visitor, Fields::code, [this]() { return code(); });
        return cont;
    }

    bool iterateSubOwners(DomItem &self, function_ref<bool(DomItem &owner)> visitor) override
    {
        bool cont = OwningItem::iterateSubOwners(self, visitor);
        cont = cont && self.field(Fields::components).visitKeys([visitor](QString, DomItem &comps) {
            return comps.visitIndexes([visitor](DomItem &comp) {
                return comp.field(Fields::objects).visitIndexes([visitor](DomItem &qmlObj) {
                    if (const QmlObject *qmlObjPtr = qmlObj.as<QmlObject>())
                        return qmlObjPtr->iterateSubOwners(qmlObj, visitor);
                    Q_ASSERT(false);
                    return true;
                });
            });
        });
        return cont;
    }

    bool isValid() const {
        QMutexLocker l(mutex());
        return m_isValid;
    }
    void setIsValid(bool val) {
        QMutexLocker l(mutex());
        m_isValid = val;
    }
    // null code means invalid
    const QString &code() const { return m_code; }

protected:
    QString m_canonicalFilePath;
    QString m_code;
    Path m_path;
    bool m_isValid = false;
};

class QMLDOM_EXPORT QmlDirectory final : public ExternalOwningItem
{
protected:
    std::shared_ptr<OwningItem> doCopy(DomItem &) const override
    {
        return std::make_shared<QmlDirectory>(*this);
    }

public:
    constexpr static DomType kindValue = DomType::QmlDirectory;
    DomType kind() const override { return kindValue; }
    QmlDirectory(QString filePath = QString(), QStringList dirList = QStringList(),
                 QDateTime lastDataUpdateAt = QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC),
                 int derivedFrom = 0);
    QmlDirectory(const QmlDirectory &o) = default;

    std::shared_ptr<QmlDirectory> makeCopy(DomItem &self) const
    {
        return std::static_pointer_cast<QmlDirectory>(doCopy(self));
    }

    bool iterateDirectSubpaths(DomItem &self, DirectVisitor visitor) override;

    const QMultiMap<QString, Export> &exports() const & { return m_exports; }

    const QMultiMap<QString, QString> &qmlFiles() const & { return m_qmlFiles; }

    bool addQmlFilePath(QString relativePath);

private:
    QMultiMap<QString, Export> m_exports;
    QMultiMap<QString, QString> m_qmlFiles;
};

class QMLDOM_EXPORT QmldirFile final : public ExternalOwningItem
{
    Q_DECLARE_TR_FUNCTIONS(QmldirFile)
protected:
    std::shared_ptr<OwningItem> doCopy(DomItem &) const override
    {
        auto copy = std::make_shared<QmldirFile>(*this);
        return copy;
    }

public:
    constexpr static DomType kindValue = DomType::QmldirFile;
    DomType kind() const override { return kindValue; }

    static ErrorGroups myParsingErrors();

    QmldirFile(QString filePath = QString(), QString code = QString(),
               QDateTime lastDataUpdateAt = QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC),
               int derivedFrom = 0)
        : ExternalOwningItem(filePath, lastDataUpdateAt, Paths::qmldirFilePath(filePath),
                             derivedFrom, code)
    {
    }
    QmldirFile(const QmldirFile &o) = default;

    static std::shared_ptr<QmldirFile> fromPathAndCode(QString path, QString code);

    std::shared_ptr<QmldirFile> makeCopy(DomItem &self) const
    {
        return std::static_pointer_cast<QmldirFile>(doCopy(self));
    }

    bool iterateDirectSubpaths(DomItem &self, DirectVisitor visitor) override;

    QmlUri uri() const { return m_uri; }

    const QSet<int> &majorVersions() const & { return m_majorVersions; }

    const QMultiMap<QString, Export> &exports() const & { return m_exports; }

    const QList<Import> &imports() const & { return m_imports; }

    const QList<Path> &qmltypesFilePaths() const & { return m_qmltypesFilePaths; }

    QMap<QString, QString> qmlFiles() const;

    bool designerSupported() const { return m_qmldir.designerSupported(); }

    QStringList classNames() const { return m_qmldir.classNames(); }

    QList<ModuleAutoExport> autoExports() const;
    void setAutoExports(const QList<ModuleAutoExport> &autoExport);

    void ensureInModuleIndex(DomItem &self, QString uri);

private:
    void parse();
    void setFromQmldir();

    QmlUri m_uri;
    QSet<int> m_majorVersions;
    QQmlDirParser m_qmldir;
    QList<QQmlDirParser::Plugin> m_plugins;
    QList<Import> m_imports;
    QList<ModuleAutoExport> m_autoExports;
    QMultiMap<QString, Export> m_exports;
    QList<Path> m_qmltypesFilePaths;
};

class QMLDOM_EXPORT JsFile final : public ExternalOwningItem
{
protected:
    std::shared_ptr<OwningItem> doCopy(DomItem &) const override
    {
        auto copy = std::make_shared<JsFile>(*this);
        return copy;
    }

public:
    constexpr static DomType kindValue = DomType::JsFile;
    DomType kind() const override { return kindValue; }
    JsFile(QString filePath = QString(),
           QDateTime lastDataUpdateAt = QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC),
           Path pathFromTop = Path(), int derivedFrom = 0)
        : ExternalOwningItem(filePath, lastDataUpdateAt, pathFromTop, derivedFrom)
    {
    }
    JsFile(const JsFile &o) = default;

    std::shared_ptr<JsFile> makeCopy(DomItem &self) const
    {
        return std::static_pointer_cast<JsFile>(doCopy(self));
    }

    std::shared_ptr<QQmlJS::Engine> engine() const { return m_engine; }
    JsResource rootComponent() const { return m_rootComponent; }

private:
    std::shared_ptr<QQmlJS::Engine> m_engine;
    JsResource m_rootComponent;
};

class QMLDOM_EXPORT QmlFile final : public ExternalOwningItem
{
protected:
    std::shared_ptr<OwningItem> doCopy(DomItem &self) const override;

public:
    constexpr static DomType kindValue = DomType::QmlFile;
    DomType kind() const override { return kindValue; }

    QmlFile(const QmlFile &o);
    QmlFile(QString filePath = QString(), QString code = QString(),
            QDateTime lastDataUpdate = QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC),
            int derivedFrom = 0);
    static ErrorGroups myParsingErrors();
    bool iterateDirectSubpaths(DomItem &self, DirectVisitor)
            override; // iterates the *direct* subpaths, returns false if a quick end was requested
    DomItem field(DomItem &self, QStringView name) const override
    {
        return const_cast<QmlFile *>(this)->field(self, name);
    }
    DomItem field(DomItem &self, QStringView name);
    std::shared_ptr<QmlFile> makeCopy(DomItem &self) const
    {
        return std::static_pointer_cast<QmlFile>(doCopy(self));
    }
    void addError(DomItem &self, ErrorMessage msg) override;

    const QMultiMap<QString, QmlComponent> &components() const & { return m_components; }
    void setComponents(const QMultiMap<QString, QmlComponent> &components)
    {
        m_components = components;
    }
    Path addComponent(const QmlComponent &component, AddOption option = AddOption::Overwrite,
                      QmlComponent **cPtr = nullptr)
    {
        QStringList nameEls = component.name().split(QChar::fromLatin1('.'));
        QString key = nameEls.mid(1).join(QChar::fromLatin1('.'));
        return insertUpdatableElementInMultiMap(Path::Field(Fields::components), m_components, key,
                                                component, option, cPtr);
    }

    void writeOut(DomItem &self, OutWriter &lw) const override;

    AST::UiProgram *ast() const
    {
        return m_ast; // avoid making it public? would make moving away from it easier
    }
    const QList<Import> &imports() const & { return m_imports; }
    void setImports(const QList<Import> &imports) { m_imports = imports; }
    Path addImport(const Import &i)
    {
        index_type idx = index_type(m_imports.size());
        m_imports.append(i);
        if (i.uri.isModule()) {
            m_importScope.addImport((i.importId.isEmpty()
                                             ? QStringList()
                                             : i.importId.split(QChar::fromLatin1('.'))),
                                    i.importedPath());
        } else {
            QString path = i.uri.absoluteLocalPath(canonicalFilePath());
            if (!path.isEmpty())
                m_importScope.addImport((i.importId.isEmpty()
                                                 ? QStringList()
                                                 : i.importId.split(QChar::fromLatin1('.'))),
                                        Paths::qmlDirPath(path));
        }
        return Path::Field(Fields::imports).index(idx);
    }
    std::shared_ptr<QQmlJS::Engine> engine() const { return m_engine; }
    RegionComments &comments() { return m_comments; }
    std::shared_ptr<AstComments> astComments() const { return m_astComments; }
    void setAstComments(std::shared_ptr<AstComments> comm) { m_astComments = comm; }
    FileLocations::Tree fileLocationsTree() const { return m_fileLocationsTree; }
    void setFileLocationsTree(FileLocations::Tree v) { m_fileLocationsTree = v; }
    const QList<Pragma> &pragmas() const & { return m_pragmas; }
    void setPragmas(QList<Pragma> pragmas) { m_pragmas = pragmas; }
    Path addPragma(const Pragma &pragma)
    {
        int idx = m_pragmas.size();
        m_pragmas.append(pragma);
        return Path::Field(Fields::pragmas).index(idx);
    }
    ImportScope &importScope() { return m_importScope; }
    const ImportScope &importScope() const { return m_importScope; }

private:
    friend class QmlDomAstCreator;
    std::shared_ptr<Engine> m_engine;
    AST::UiProgram *m_ast; // avoid? would make moving away from it easier
    std::shared_ptr<AstComments> m_astComments;
    RegionComments m_comments;
    FileLocations::Tree m_fileLocationsTree;
    QMultiMap<QString, QmlComponent> m_components;
    QList<Pragma> m_pragmas;
    QList<Import> m_imports;
    ImportScope m_importScope;
};

class QMLDOM_EXPORT QmltypesFile final : public ExternalOwningItem
{
protected:
    std::shared_ptr<OwningItem> doCopy(DomItem &) const override
    {
        auto res = std::make_shared<QmltypesFile>(*this);
        return res;
    }

public:
    constexpr static DomType kindValue = DomType::QmltypesFile;
    DomType kind() const override { return kindValue; }

    QmltypesFile(QString filePath = QString(), QString code = QString(),
                 QDateTime lastDataUpdateAt = QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC),
                 int derivedFrom = 0)
        : ExternalOwningItem(filePath, lastDataUpdateAt, Paths::qmltypesFilePath(filePath),
                             derivedFrom, code)
    {
    }

    QmltypesFile(const QmltypesFile &o) = default;

    void ensureInModuleIndex(DomItem &self);

    bool iterateDirectSubpaths(DomItem &self, DirectVisitor) override;
    std::shared_ptr<QmltypesFile> makeCopy(DomItem &self) const
    {
        return std::static_pointer_cast<QmltypesFile>(doCopy(self));
    }

    void addImport(const Import i)
    { // builder only: not threadsafe...
        m_imports.append(i);
    }
    const QList<Import> &imports() const & { return m_imports; }
    const QMultiMap<QString, QmltypesComponent> &components() const & { return m_components; }
    void setComponents(QMultiMap<QString, QmltypesComponent> c) { m_components = std::move(c); }
    Path addComponent(const QmltypesComponent &comp, AddOption option = AddOption::Overwrite,
                      QmltypesComponent **cPtr = nullptr)
    {
        for (const Export &e : comp.exports())
            addExport(e);
        return insertUpdatableElementInMultiMap(Path::Field(u"components"), m_components,
                                                comp.name(), comp, option, cPtr);
    }
    const QMultiMap<QString, Export> &exports() const & { return m_exports; }
    void setExports(QMultiMap<QString, Export> e) { m_exports = e; }
    Path addExport(const Export &e)
    {
        index_type i = m_exports.values(e.typeName).size();
        m_exports.insert(e.typeName, e);
        addUri(e.uri, e.version.majorVersion);
        return canonicalPath().field(Fields::exports).index(i);
    }

    const QMap<QString, QSet<int>> &uris() const & { return m_uris; }
    void addUri(QString uri, int majorVersion)
    {
        QSet<int> &v = m_uris[uri];
        if (!v.contains(majorVersion)) {
            v.insert(majorVersion);
        }
    }

private:
    QList<Import> m_imports;
    QMultiMap<QString, QmltypesComponent> m_components;
    QMultiMap<QString, Export> m_exports;
    QMap<QString, QSet<int>> m_uris;
};

class QMLDOM_EXPORT GlobalScope final : public ExternalOwningItem
{
protected:
    std::shared_ptr<OwningItem> doCopy(DomItem &) const override;

public:
    constexpr static DomType kindValue = DomType::GlobalScope;
    DomType kind() const override { return kindValue; }

    GlobalScope(QString filePath = QString(),
                QDateTime lastDataUpdateAt = QDateTime::fromMSecsSinceEpoch(0, QTimeZone::UTC),
                int derivedFrom = 0)
        : ExternalOwningItem(filePath, lastDataUpdateAt, Paths::globalScopePath(filePath),
                             derivedFrom)
    {
        setIsValid(true);
    }

    bool iterateDirectSubpaths(DomItem &self, DirectVisitor visitor) override;
    std::shared_ptr<GlobalScope> makeCopy(DomItem &self) const
    {
        return std::static_pointer_cast<GlobalScope>(doCopy(self));
    }
    QString name() const { return m_name; }
    Language language() const { return m_language; }
    GlobalComponent rootComponent() const { return m_rootComponent; }
    void setName(QString name) { m_name = name; }
    void setLanguage(Language language) { m_language = language; }
    void setRootComponent(const GlobalComponent &ob)
    {
        m_rootComponent = ob;
        m_rootComponent.updatePathFromOwner(Path::Field(Fields::rootComponent));
    }

private:
    QString m_name;
    Language m_language;
    GlobalComponent m_rootComponent;
};

} // end namespace Dom
} // end namespace QQmlJS
QT_END_NAMESPACE
#endif // QQMLDOMEXTERNALITEMS_P_H