aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qmljs/qmljsdocument.cpp
blob: e61552a6396b348c9ea5052c1b992fb036c66477 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qmljsdocument.h"
#include "qmljsbind.h"
#include "qmljsconstants.h"
#include "qmljsimportdependencies.h"
#include <qmljs/parser/qmljslexer_p.h>
#include <qmljs/parser/qmljsparser_p.h>

#include <utils/qtcassert.h>

#include <QCryptographicHash>
#include <QDir>
#include <QFileInfo>
#include <QRegularExpression>

#include <algorithm>

static constexpr int sizeofQChar = int(sizeof(QChar));

using namespace QmlJS;
using namespace QmlJS::AST;

/*!
    \class QmlJS::Document
    \brief The Document class creates a QML or JavaScript document.
    \sa Snapshot

    Documents are usually created by the ModelManagerInterface
    and stored in a Snapshot. They allow access to data such as
    the file path, source code, abstract syntax tree and the Bind
    instance for the document.

    To make sure unused and outdated documents are removed correctly, Document
    instances are usually accessed through a shared pointer, see Document::Ptr.

    Documents in a Snapshot are immutable: They, or anything reachable through them,
    must not be changed. This allows Documents to be shared freely among threads
    without extra synchronization.
*/

/*!
    \class QmlJS::LibraryInfo
    \brief The LibraryInfo class creates a QML library.
    \sa Snapshot

    A LibraryInfo is created when the ModelManagerInterface finds
    a QML library and parses the qmldir file. The instance holds information about
    which Components the library provides and which plugins to load.

    The ModelManager will try to extract detailed information about the types
    defined in the plugins this library loads. Once it is done, the data will
    be available through the metaObjects() function.
*/

/*!
    \class QmlJS::Snapshot
    \brief The Snapshot class holds and offers access to a set of
    Document::Ptr and LibraryInfo instances.
    \sa Document LibraryInfo

    Usually Snapshots are copies of the snapshot maintained and updated by the
    ModelManagerInterface that updates its instance as parsing
    threads finish and new information becomes available.
*/

Document::Document(const Utils::FilePath &fileName, Dialect language)
    : _engine(nullptr)
    , _ast(nullptr)
    , _bind(nullptr)
    , _fileName(fileName.cleanPath())
    , _editorRevision(0)
    , _language(language)
    , _parsedCorrectly(false)
{
    _path = fileName.absoluteFilePath().parentDir().cleanPath();

    if (language.isQmlLikeLanguage()) {
        _componentName = fileName.baseName();

        if (! _componentName.isEmpty()) {
            // ### TODO: check the component name.

            if (! _componentName.at(0).isUpper())
                _componentName.clear();
        }
    }
}

Document::~Document()
{
    if (_bind)
        delete _bind;

    if (_engine)
        delete _engine;
}

Document::MutablePtr Document::create(const Utils::FilePath &fileName, Dialect language)
{
    Document::MutablePtr doc(new Document(fileName, language));
    doc->_ptr = doc;
    return doc;
}

Document::Ptr Document::ptr() const
{
    return _ptr.toStrongRef();
}

bool Document::isQmlDocument() const
{
    return _language.isQmlLikeLanguage();
}

Dialect Document::language() const
{
    return _language;
}

void Document::setLanguage(Dialect l)
{
    _language = l;
}

QString Document::importId() const
{
    return _fileName.toString();
}

QByteArray Document::fingerprint() const
{
    return _fingerprint;
}

AST::UiProgram *Document::qmlProgram() const
{
    return cast<UiProgram *>(_ast);
}

AST::Program *Document::jsProgram() const
{
    return cast<Program *>(_ast);
}

AST::ExpressionNode *Document::expression() const
{
    if (_ast)
        return _ast->expressionCast();

    return nullptr;
}

AST::Node *Document::ast() const
{
    return _ast;
}

const QmlJS::Engine *Document::engine() const
{
    return _engine;
}

QList<DiagnosticMessage> Document::diagnosticMessages() const
{
    return _diagnosticMessages;
}

QString Document::source() const
{
    return _source;
}

void Document::setSource(const QString &source)
{
    _source = source;
    QCryptographicHash sha(QCryptographicHash::Sha1);
    sha.addData(source.toUtf8());
    _fingerprint = sha.result();
}

int Document::editorRevision() const
{
    return _editorRevision;
}

void Document::setEditorRevision(int revision)
{
    _editorRevision = revision;
}

Utils::FilePath Document::fileName() const
{
    return _fileName;

}

Utils::FilePath Document::path() const
{
    return _path;
}

QString Document::componentName() const
{
    return _componentName;
}

namespace {
class CollectDirectives : public Directives
{
    void addLocation(int line, int column) {
        const SourceLocation loc = SourceLocation(
                    0,  // placeholder
                    0,  // placeholder
                    static_cast<quint32>(line),
                    static_cast<quint32>(column));
        _locations += loc;
    }

    QList<SourceLocation> _locations;

public:
    CollectDirectives(const Utils::FilePath &documentPath)
        : documentPath(documentPath)
        , isLibrary(false)

    {}

    void importFile(const QString &jsfile, const QString &module,
                    int line, int column) override
    {
        imports += ImportInfo::pathImport(
                    documentPath, jsfile, LanguageUtils::ComponentVersion(), module);
        addLocation(line, column);
    }

    void importModule(const QString &uri, const QString &version, const QString &module,
                      int line, int column) override
    {
        imports += ImportInfo::moduleImport(uri, LanguageUtils::ComponentVersion(version), module);
        addLocation(line, column);
    }

    void pragmaLibrary() override
    {
        isLibrary = true;
    }

    virtual QList<SourceLocation> locations() { return _locations; }

    const Utils::FilePath documentPath;
    bool isLibrary;
    QList<ImportInfo> imports;
};

} // anonymous namespace


QList<SourceLocation> Document::jsDirectives() const
{
    return _jsdirectives;
}

bool Document::parse_helper(int startToken)
{
    Q_ASSERT(! _engine);
    Q_ASSERT(! _ast);
    Q_ASSERT(! _bind);

    _engine = new Engine();

    Lexer lexer(_engine);
    Parser parser(_engine);

    QString source = _source;
    lexer.setCode(source, /*line = */ 1, /*qmlMode = */_language.isQmlLikeLanguage());

    CollectDirectives directives = CollectDirectives(path());
    _engine->setDirectives(&directives);

    switch (startToken) {
    case QmlJSGrammar::T_FEED_UI_PROGRAM:
        _parsedCorrectly = parser.parse();
        break;
    case QmlJSGrammar::T_FEED_JS_SCRIPT:
    case QmlJSGrammar::T_FEED_JS_MODULE: {
        _parsedCorrectly = parser.parseProgram();
        const QList<SourceLocation> locations = directives.locations();
        for (const auto &d : locations) {
            _jsdirectives << d;
        }
    } break;

    case QmlJSGrammar::T_FEED_JS_EXPRESSION:
        _parsedCorrectly = parser.parseExpression();
        break;
    default:
        Q_ASSERT(0);
    }

    _ast = parser.rootNode();
    _diagnosticMessages = parser.diagnosticMessages();

    _bind = new Bind(this, &_diagnosticMessages, directives.isLibrary, directives.imports);

    return _parsedCorrectly;
}

bool Document::parse()
{
    if (isQmlDocument())
        return parseQml();

    return parseJavaScript();
}

bool Document::parseQml()
{
    return parse_helper(QmlJSGrammar::T_FEED_UI_PROGRAM);
}

bool Document::parseJavaScript()
{
    return parse_helper(QmlJSGrammar::T_FEED_JS_SCRIPT);
}

bool Document::parseExpression()
{
    return parse_helper(QmlJSGrammar::T_FEED_JS_EXPRESSION);
}

Bind *Document::bind() const
{
    return _bind;
}

LibraryInfo::LibraryInfo()
{
    static const QByteArray emptyFingerprint = calculateFingerprint();
    _fingerprint = emptyFingerprint;
}

LibraryInfo::LibraryInfo(Status status)
    : _status(status)
{
    updateFingerprint();
}

LibraryInfo::LibraryInfo(const QString &typeInfo)
    : _status(Found)
{
    _typeinfos.append(typeInfo);
    updateFingerprint();
}

LibraryInfo::LibraryInfo(const QmlDirParser &parser, const QByteArray &fingerprint)
    : _status(Found)
    , _components(parser.components().values())
    , _plugins(parser.plugins())
    , _typeinfos(parser.typeInfos())
    , _imports(parser.imports())
    , _fingerprint(fingerprint)
{
    if (_fingerprint.isEmpty())
        updateFingerprint();
}

QByteArray LibraryInfo::calculateFingerprint() const
{
    QCryptographicHash hash(QCryptographicHash::Sha1);
    auto addData = [&hash](auto p, size_t len) {
        hash.addData(reinterpret_cast<const char *>(p), len);
    };

    addData(&_status, sizeof(_status));
    int len = _components.size();
    addData(&len, sizeof(len));
    for (const QmlDirParser::Component &component : _components) {
        len = component.fileName.size();
        addData(&len, sizeof(len));
        addData(component.fileName.constData(), len * sizeofQChar);
        addData(&component.majorVersion, sizeof(component.majorVersion));
        addData(&component.minorVersion, sizeof(component.minorVersion));
        len = component.typeName.size();
        addData(&len, sizeof(len));
        addData(component.typeName.constData(), component.typeName.size() * sizeofQChar);
        int flags = (component.singleton ?  (1 << 0) : 0) + (component.internal ? (1 << 1) : 0);
        addData(&flags, sizeof(flags));
    }
    len = _plugins.size();
    addData(&len, sizeof(len));
    for (const QmlDirParser::Plugin &plugin : _plugins) {
        len = plugin.path.size();
        addData(&len, sizeof(len));
        addData(plugin.path.constData(), len * sizeofQChar);
        len = plugin.name.size();
        addData(&len, sizeof(len));
        addData(plugin.name.constData(), len * sizeofQChar);
    }
    len = _typeinfos.size();
    addData(&len, sizeof(len));
    for (const QString &typeinfo : _typeinfos) {
        len = typeinfo.size();
        addData(&len, sizeof(len));
        addData(typeinfo.constData(), len * sizeofQChar);
    }
    len = _metaObjects.size();
    addData(&len, sizeof(len));
    QList<QByteArray> metaFingerprints;
    for (const LanguageUtils::FakeMetaObject::ConstPtr &metaObject : _metaObjects)
        metaFingerprints.append(metaObject->fingerprint());
    std::sort(metaFingerprints.begin(), metaFingerprints.end());
    for (const QByteArray &fp : std::as_const(metaFingerprints))
        hash.addData(fp);
    addData(&_dumpStatus, sizeof(_dumpStatus));
    len = _dumpError.size(); // localization dependent (avoid?)
    addData(&len, sizeof(len));
    addData(_dumpError.constData(), len * sizeofQChar);

    len = _moduleApis.size();
    addData(&len, sizeof(len));
    for (const ModuleApiInfo &moduleInfo : _moduleApis)
        moduleInfo.addToHash(hash); // make it order independent?

    len = _imports.size();
    addData(&len, sizeof(len));
    for (const QmlDirParser::Import &import : _imports)
        hash.addData(import.module.toUtf8()); // import order matters, keep order-dependent

    QByteArray res(hash.result());
    res.append('L');
    return res;
}

void LibraryInfo::updateFingerprint()
{
    _fingerprint = calculateFingerprint();
}

Snapshot::Snapshot()
{
}

Snapshot::~Snapshot()
{
}

void Snapshot::insert(const Document::Ptr &document, bool allowInvalid)
{
    if (document && (allowInvalid || document->qmlProgram() || document->jsProgram())) {
        const Utils::FilePath fileName = document->fileName();
        const Utils::FilePath path = document->path();
        remove(fileName);
        _documentsByPath[path].append(document);
        _documents.insert(fileName, document);
        CoreImport cImport;
        cImport.importId = document->importId();
        cImport.language = document->language();
        cImport.addPossibleExport(
            Export(ImportKey(ImportType::File, fileName.toString()), {}, true, fileName.baseName()));
        cImport.fingerprint = document->fingerprint();
        _dependencies.addCoreImport(cImport);
    }
}

void Snapshot::insertLibraryInfo(const Utils::FilePath &path, const LibraryInfo &info)
{
    QTC_CHECK(!path.isEmpty());
    QTC_CHECK(info.fingerprint() == info.calculateFingerprint());
    _libraries.insert(path.cleanPath(), info);
    if (!info.wasFound()) return;
    CoreImport cImport;
    cImport.importId = path.toString();
    cImport.language = Dialect::AnyLanguage;
    QSet<ImportKey> packages;
    for (const ModuleApiInfo &moduleInfo : info.moduleApis()) {
        ImportKey iKey(ImportType::Library, moduleInfo.uri, moduleInfo.version.majorVersion(),
                       moduleInfo.version.minorVersion());
        packages.insert(iKey);
    }
    const QList<LanguageUtils::FakeMetaObject::ConstPtr> metaObjects = info.metaObjects();
    for (const LanguageUtils::FakeMetaObject::ConstPtr &metaO : metaObjects) {
        for (const LanguageUtils::FakeMetaObject::Export &e : metaO->exports()) {
            ImportKey iKey(ImportType::Library, e.package, e.version.majorVersion(),
                           e.version.minorVersion());
            packages.insert(iKey);
        }
    }

    QStringList splitPath = path.path().split(QLatin1Char('/'));
    const QRegularExpression vNr(QLatin1String("^(.+)\\.([0-9]+)(?:\\.([0-9]+))?$"));
    const QRegularExpression safeName(QLatin1String("^[a-zA-Z_][[a-zA-Z0-9_]*$"));
    for (const ImportKey &importKey : std::as_const(packages)) {
        if (importKey.splitPath.size() == 1 && importKey.splitPath.at(0).isEmpty() && splitPath.length() > 0) {
            // relocatable
            QStringList myPath = splitPath;
            QRegularExpressionMatch match = vNr.match(myPath.last());
            if (match.hasMatch())
                myPath.last() = match.captured(1);
            for (int iPath = myPath.size(); iPath != 1; ) {
                --iPath;
                match = safeName.match(myPath.at(iPath));
                if (!match.hasMatch())
                    break;
                ImportKey iKey(ImportType::Library, QStringList(myPath.mid(iPath)).join(QLatin1Char('.')),
                               importKey.majorVersion, importKey.minorVersion);
                Utils::FilePath newP = path.withNewPath(
                            (iPath == 1)
                                 ? QLatin1String("/")
                                 : QStringList(myPath.mid(0, iPath)).join(QLatin1Char('/')));
                cImport.addPossibleExport(Export(iKey, newP, true));
            }
        } else {
            Utils::FilePath requiredPath = path.withNewPath(
                QStringList(splitPath.mid(0, splitPath.size() - importKey.splitPath.size()))
                    .join(QLatin1String("/")));
            cImport.addPossibleExport(Export(importKey, requiredPath, true));
        }
    }
    if (cImport.possibleExports.isEmpty() && splitPath.size() > 0) {
        const QRegularExpression vNr(QLatin1String("^(.+)\\.([0-9]+)(?:\\.([0-9]+))?$"));
        const QRegularExpression safeName(QLatin1String("^[a-zA-Z_][[a-zA-Z0-9_]*$"));
        int majorVersion = LanguageUtils::ComponentVersion::NoVersion;
        int minorVersion = LanguageUtils::ComponentVersion::NoVersion;

        for (const QmlDirParser::Component &component : info.components()) {
            if (component.majorVersion > majorVersion)
                majorVersion = component.majorVersion;
            if (component.minorVersion > minorVersion)
                minorVersion = component.minorVersion;
        }

        QRegularExpressionMatch match = vNr.match(splitPath.last());
        if (match.hasMatch()) {
            splitPath.last() = match.captured(1);
            bool ok;
            majorVersion = match.captured(2).toInt(&ok);
            if (!ok)
                majorVersion = LanguageUtils::ComponentVersion::NoVersion;
            minorVersion = match.captured(3).toInt(&ok);
            if (match.captured(3).isEmpty() || !ok)
                minorVersion = LanguageUtils::ComponentVersion::NoVersion;
        }

        for (int iPath = splitPath.size(); iPath != 1; ) {
            --iPath;
            match = safeName.match(splitPath.at(iPath));
            if (!match.hasMatch())
                break;
            ImportKey iKey(ImportType::Library, QStringList(splitPath.mid(iPath)).join(QLatin1Char('.')),
                           majorVersion, minorVersion);
            Utils::FilePath newP = path.withNewPath(
                        iPath == 1
                             ? QLatin1String("/")
                             : QStringList(splitPath.mid(0, iPath)).join(QLatin1Char('/')));
            cImport.addPossibleExport(Export(iKey, newP, true));
        }
    }
    for (const QmlDirParser::Component &component : info.components()) {
        for (const Export &e : std::as_const(cImport.possibleExports))
            _dependencies.addExport(component.fileName, e.exportName, e.pathRequired, e.typeName);
    }

    cImport.fingerprint = info.fingerprint();
    _dependencies.addCoreImport(cImport);
}

void Snapshot::remove(const Utils::FilePath &fileName)
{
    Document::Ptr doc = _documents.value(fileName);
    if (!doc.isNull()) {
        const Utils::FilePath &path = doc->path();

        QList<Document::Ptr> docs = _documentsByPath.value(path);
        docs.removeAll(doc);
        _documentsByPath[path] = docs;

        _documents.remove(fileName);
    }
}

const QmlJS::ImportDependencies *Snapshot::importDependencies() const
{
    return &_dependencies;
}

QmlJS::ImportDependencies *Snapshot::importDependencies()
{
    return &_dependencies;
}

Document::MutablePtr Snapshot::documentFromSource(const QString &code,
                                                  const Utils::FilePath &fileName,
                                                  Dialect language) const
{
    Document::MutablePtr newDoc = Document::create(fileName, language);

    if (Document::Ptr thisDocument = document(fileName))
        newDoc->_editorRevision = thisDocument->_editorRevision;

    newDoc->setSource(code);
    return newDoc;
}

Document::Ptr Snapshot::document(const Utils::FilePath &fileName) const
{
    return _documents.value(fileName.cleanPath());
}

QList<Document::Ptr> Snapshot::documentsInDirectory(const Utils::FilePath &path) const
{
    return _documentsByPath.value(path.cleanPath());
}

LibraryInfo Snapshot::libraryInfo(const Utils::FilePath &path) const
{
    return _libraries.value(path.cleanPath());
}

void ModuleApiInfo::addToHash(QCryptographicHash &hash) const
{
    int len = uri.length();
    hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
    hash.addData(reinterpret_cast<const char *>(uri.constData()), len * sizeofQChar);
    version.addToHash(hash);
    len = cppName.length();
    hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
    hash.addData(reinterpret_cast<const char *>(cppName.constData()), len * sizeofQChar);
}