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
|
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#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 <algorithm>
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 QString &fileName, Dialect language)
: _engine(0)
, _ast(0)
, _bind(0)
, _fileName(QDir::cleanPath(fileName))
, _editorRevision(0)
, _language(language)
, _parsedCorrectly(false)
{
QFileInfo fileInfo(fileName);
_path = QDir::cleanPath(fileInfo.absolutePath());
if (language.isQmlLikeLanguage()) {
_componentName = fileInfo.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 QString &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;
}
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 0;
}
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;
}
QString Document::fileName() const
{
return _fileName;
}
QString Document::path() const
{
return _path;
}
QString Document::componentName() const
{
return _componentName;
}
namespace {
class CollectDirectives : public Directives
{
QString documentPath;
public:
CollectDirectives(const QString &documentPath)
: documentPath(documentPath)
, isLibrary(false)
{}
virtual void pragmaLibrary() { isLibrary = true; }
virtual void importFile(const QString &jsfile, const QString &module)
{
imports += ImportInfo::pathImport(
documentPath, jsfile, LanguageUtils::ComponentVersion(), module);
}
virtual void importModule(const QString &uri, const QString &version, const QString &module)
{
imports += ImportInfo::moduleImport(uri, LanguageUtils::ComponentVersion(version), module);
}
bool isLibrary;
QList<ImportInfo> imports;
};
} // anonymous namespace
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 collectDirectives(path());
_engine->setDirectives(&collectDirectives);
switch (startToken) {
case QmlJSGrammar::T_FEED_UI_PROGRAM:
_parsedCorrectly = parser.parse();
break;
case QmlJSGrammar::T_FEED_JS_PROGRAM:
_parsedCorrectly = parser.parseProgram();
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, collectDirectives.isLibrary, collectDirectives.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_PROGRAM);
}
bool Document::parseExpression()
{
return parse_helper(QmlJSGrammar::T_FEED_JS_EXPRESSION);
}
Bind *Document::bind() const
{
return _bind;
}
LibraryInfo::LibraryInfo(Status status)
: _status(status)
, _dumpStatus(NoTypeInfo)
{
updateFingerprint();
}
LibraryInfo::LibraryInfo(const QmlDirParser &parser, const QByteArray &fingerprint)
: _status(Found)
, _components(parser.components().values())
, _plugins(parser.plugins())
, _typeinfos(parser.typeInfos())
, _fingerprint(fingerprint)
, _dumpStatus(NoTypeInfo)
{
if (_fingerprint.isEmpty())
updateFingerprint();
}
LibraryInfo::~LibraryInfo()
{
}
QByteArray LibraryInfo::calculateFingerprint() const
{
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(reinterpret_cast<const char *>(&_status), sizeof(_status));
int len = _components.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
foreach (const QmlDirParser::Component &component, _components) {
len = component.fileName.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(component.fileName.constData()), len * sizeof(QChar));
hash.addData(reinterpret_cast<const char *>(&component.majorVersion), sizeof(component.majorVersion));
hash.addData(reinterpret_cast<const char *>(&component.minorVersion), sizeof(component.minorVersion));
len = component.typeName.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(component.typeName.constData()), component.typeName.size() * sizeof(QChar));
int flags = (component.singleton ? (1 << 0) : 0) + (component.internal ? (1 << 1) : 0);
hash.addData(reinterpret_cast<const char *>(&flags), sizeof(flags));
}
len = _plugins.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
foreach (const QmlDirParser::Plugin &plugin, _plugins) {
len = plugin.path.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(plugin.path.constData()), len * sizeof(QChar));
len = plugin.name.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(plugin.name.constData()), len * sizeof(QChar));
}
len = _typeinfos.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
foreach (const QmlDirParser::TypeInfo &typeinfo, _typeinfos) {
len = typeinfo.fileName.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(typeinfo.fileName.constData()), len * sizeof(QChar));
}
len = _metaObjects.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
QList<QByteArray> metaFingerprints;
foreach (const LanguageUtils::FakeMetaObject::ConstPtr &metaObject, _metaObjects)
metaFingerprints.append(metaObject->fingerprint());
std::sort(metaFingerprints.begin(), metaFingerprints.end());
foreach (const QByteArray &fp, metaFingerprints)
hash.addData(fp);
hash.addData(reinterpret_cast<const char *>(&_dumpStatus), sizeof(_dumpStatus));
len = _dumpError.size(); // localization dependent (avoid?)
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(_dumpError.constData()), len * sizeof(QChar));
len = _moduleApis.size();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
foreach (const ModuleApiInfo &moduleInfo, _moduleApis)
moduleInfo.addToHash(hash); // make it order independent?
QByteArray res(hash.result());
res.append('L');
return res;
}
void LibraryInfo::updateFingerprint()
{
_fingerprint = calculateFingerprint();
}
Snapshot::Snapshot()
{
}
Snapshot::~Snapshot()
{
}
Snapshot::Snapshot(const Snapshot &o)
: _documents(o._documents),
_documentsByPath(o._documentsByPath),
_libraries(o._libraries),
_dependencies(o._dependencies)
{
}
void Snapshot::insert(const Document::Ptr &document, bool allowInvalid)
{
if (document && (allowInvalid || document->qmlProgram() || document->jsProgram())) {
const QString fileName = document->fileName();
const QString path = document->path();
remove(fileName);
_documentsByPath[path].append(document);
_documents.insert(fileName, document);
CoreImport cImport;
cImport.importId = document->importId();
cImport.language = document->language();
cImport.possibleExports << Export(ImportKey(ImportType::File, fileName),
QString(), true, QFileInfo(fileName).baseName());
cImport.fingerprint = document->fingerprint();
_dependencies.addCoreImport(cImport);
}
}
void Snapshot::insertLibraryInfo(const QString &path, const LibraryInfo &info)
{
QTC_CHECK(!path.isEmpty());
QTC_CHECK(info.fingerprint() == info.calculateFingerprint());
_libraries.insert(QDir::cleanPath(path), info);
if (!info.wasFound()) return;
CoreImport cImport;
cImport.importId = path;
cImport.language = Dialect::AnyLanguage;
QSet<ImportKey> packages;
foreach (const ModuleApiInfo &moduleInfo, info.moduleApis()) {
ImportKey iKey(ImportType::Library, moduleInfo.uri, moduleInfo.version.majorVersion(),
moduleInfo.version.minorVersion());
packages.insert(iKey);
}
foreach (const LanguageUtils::FakeMetaObject::ConstPtr &metaO, info.metaObjects()) {
foreach (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.split(QLatin1Char('/'));
QRegExp vNr(QLatin1String("^(.+)\\.([0-9]+)(?:\\.([0-9]+))?$"));
QRegExp safeName(QLatin1String("^[a-zA-Z_][[a-zA-Z0-9_]*$"));
foreach (const ImportKey &importKey, packages) {
if (importKey.splitPath.size() == 1 && importKey.splitPath.at(0).isEmpty() && splitPath.length() > 0) {
// relocatable
QStringList myPath = splitPath;
if (vNr.indexIn(myPath.last()) == 0)
myPath.last() = vNr.cap(1);
for (int iPath = myPath.size(); iPath != 1; ) {
--iPath;
if (safeName.indexIn(myPath.at(iPath)) != 0)
break;
ImportKey iKey(ImportType::Library, QStringList(myPath.mid(iPath)).join(QLatin1Char('.')),
importKey.majorVersion, importKey.minorVersion);
cImport.possibleExports.append(Export(iKey, (iPath == 1) ? QLatin1String("/") :
QStringList(myPath.mid(0, iPath)).join(QLatin1Char('/')), true));
}
} else {
QString requiredPath = QStringList(splitPath.mid(0, splitPath.size() - importKey.splitPath.size()))
.join(QLatin1String("/"));
cImport.possibleExports << Export(importKey, requiredPath, true);
}
}
if (cImport.possibleExports.isEmpty() && splitPath.size() > 0) {
QRegExp vNr(QLatin1String("^(.+)\\.([0-9]+)(?:\\.([0-9]+))?$"));
QRegExp safeName(QLatin1String("^[a-zA-Z_][[a-zA-Z0-9_]*$"));
int majorVersion = LanguageUtils::ComponentVersion::NoVersion;
int minorVersion = LanguageUtils::ComponentVersion::NoVersion;
if (vNr.indexIn(splitPath.last()) == 0) {
splitPath.last() = vNr.cap(1);
bool ok;
majorVersion = vNr.cap(2).toInt(&ok);
if (!ok)
majorVersion = LanguageUtils::ComponentVersion::NoVersion;
minorVersion = vNr.cap(3).toInt(&ok);
if (vNr.cap(3).isEmpty() || !ok)
minorVersion = LanguageUtils::ComponentVersion::NoVersion;
}
for (int iPath = splitPath.size(); iPath != 1; ) {
--iPath;
if (safeName.indexIn(splitPath.at(iPath)) != 0)
break;
ImportKey iKey(ImportType::Library, QStringList(splitPath.mid(iPath)).join(QLatin1Char('.')),
majorVersion, minorVersion);
cImport.possibleExports.append(Export(iKey, (iPath == 1) ? QLatin1String("/") :
QStringList(splitPath.mid(0, iPath)).join(QLatin1Char('/')), true));
}
}
foreach (const QmlDirParser::Component &component, info.components()) {
foreach (const Export &e, cImport.possibleExports)
_dependencies.addExport(component.fileName, e.exportName, e.pathRequired, e.typeName);
}
cImport.fingerprint = info.fingerprint();
_dependencies.addCoreImport(cImport);
}
void Snapshot::remove(const QString &fileName)
{
Document::Ptr doc = _documents.value(fileName);
if (!doc.isNull()) {
const QString &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 QString &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 QString &fileName) const
{
return _documents.value(QDir::cleanPath(fileName));
}
QList<Document::Ptr> Snapshot::documentsInDirectory(const QString &path) const
{
return _documentsByPath.value(QDir::cleanPath(path));
}
LibraryInfo Snapshot::libraryInfo(const QString &path) const
{
return _libraries.value(QDir::cleanPath(path));
}
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 * sizeof(QChar));
version.addToHash(hash);
len = cppName.length();
hash.addData(reinterpret_cast<const char *>(&len), sizeof(len));
hash.addData(reinterpret_cast<const char *>(cppName.constData()), len * sizeof(QChar));
}
|