Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljstypedescriptionreader.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
7#include <QtQml/private/qqmljsparser_p.h>
8#include <QtQml/private/qqmljslexer_p.h>
9#include <QtQml/private/qqmljsengine_p.h>
10
11#include <QtCore/qdir.h>
12#include <QtCore/qstring.h>
13
15
16using namespace QQmlJS;
17using namespace QQmlJS::AST;
18using namespace Qt::StringLiterals;
19
20QString toString(const UiQualifiedId *qualifiedId, QChar delimiter = QLatin1Char('.'))
21{
22 QString result;
23
24 for (const UiQualifiedId *iter = qualifiedId; iter; iter = iter->next) {
25 if (iter != qualifiedId)
26 result += delimiter;
27
28 result += iter->name;
29 }
30
31 return result;
32}
33
34bool QQmlJSTypeDescriptionReader::operator()(
35 QList<QQmlJSExportedScope> *objects, QStringList *dependencies)
36{
37 Engine engine;
38
39 Lexer lexer(&engine);
40 Parser parser(&engine);
41
42 lexer.setCode(m_source, /*lineno = */ 1, /*qmlMode = */true);
43
44 if (!parser.parse()) {
45 m_errorMessage = QString::fromLatin1("%1:%2: %3").arg(
46 QString::number(parser.errorLineNumber()),
47 QString::number(parser.errorColumnNumber()),
48 parser.errorMessage());
49 return false;
50 }
51
52 m_objects = objects;
53 m_dependencies = dependencies;
54 readDocument(parser.ast());
55
56 return m_errorMessage.isEmpty();
57}
58
59void QQmlJSTypeDescriptionReader::readDocument(UiProgram *ast)
60{
61 if (!ast) {
62 addError(SourceLocation(), tr("Could not parse document."));
63 return;
64 }
65
66 if (!ast->headers || ast->headers->next || !cast<UiImport *>(ast->headers->headerItem)) {
67 addError(SourceLocation(), tr("Expected a single import."));
68 return;
69 }
70
71 auto *import = cast<UiImport *>(ast->headers->headerItem);
72 if (toString(import->importUri) != QLatin1String("QtQuick.tooling")) {
73 addError(import->importToken, tr("Expected import of QtQuick.tooling."));
74 return;
75 }
76
77 if (!import->version) {
78 addError(import->firstSourceLocation(), tr("Import statement without version."));
79 return;
80 }
81
82 if (import->version->version.majorVersion() != 1) {
83 addError(import->version->firstSourceLocation(),
84 tr("Major version different from 1 not supported."));
85 return;
86 }
87
88 if (!ast->members || !ast->members->member || ast->members->next) {
89 addError(SourceLocation(), tr("Expected document to contain a single object definition."));
90 return;
91 }
92
93 auto *module = cast<UiObjectDefinition *>(ast->members->member);
94 if (!module) {
95 addError(SourceLocation(), tr("Expected document to contain a single object definition."));
96 return;
97 }
98
99 if (toString(module->qualifiedTypeNameId) != QLatin1String("Module")) {
100 addError(SourceLocation(), tr("Expected document to contain a Module {} member."));
101 return;
102 }
103
104 readModule(module);
105}
106
107void QQmlJSTypeDescriptionReader::readModule(UiObjectDefinition *ast)
108{
109 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
110 UiObjectMember *member = it->member;
111 auto *component = cast<UiObjectDefinition *>(member);
112
113 auto *script = cast<UiScriptBinding *>(member);
114 if (script && (toString(script->qualifiedId) == QStringLiteral("dependencies"))) {
115 readDependencies(script);
116 continue;
117 }
118
119 QString typeName;
120 if (component)
121 typeName = toString(component->qualifiedTypeNameId);
122
123 if (!component || typeName != QLatin1String("Component")) {
124 continue;
125 }
126
127 if (typeName == QLatin1String("Component"))
128 readComponent(component);
129 }
130}
131
132void QQmlJSTypeDescriptionReader::addError(const SourceLocation &loc, const QString &message)
133{
134 m_errorMessage += QString::fromLatin1("%1:%2:%3: %4\n").arg(
135 QDir::toNativeSeparators(m_fileName),
136 QString::number(loc.startLine),
137 QString::number(loc.startColumn),
138 message);
139}
140
141void QQmlJSTypeDescriptionReader::addWarning(const SourceLocation &loc, const QString &message)
142{
143 m_warningMessage += QString::fromLatin1("%1:%2:%3: %4\n").arg(
144 QDir::toNativeSeparators(m_fileName),
145 QString::number(loc.startLine),
146 QString::number(loc.startColumn),
147 message);
148}
149
150void QQmlJSTypeDescriptionReader::readDependencies(UiScriptBinding *ast)
151{
152 auto *stmt = cast<ExpressionStatement*>(ast->statement);
153 if (!stmt) {
154 addError(ast->statement->firstSourceLocation(), tr("Expected dependency definitions"));
155 return;
156 }
157 auto *exp = cast<ArrayPattern *>(stmt->expression);
158 if (!exp) {
159 addError(stmt->expression->firstSourceLocation(), tr("Expected dependency definitions"));
160 return;
161 }
162 for (PatternElementList *l = exp->elements; l; l = l->next) {
163 auto *str = cast<StringLiteral *>(l->element->initializer);
164 *m_dependencies << str->value.toString();
165 }
166}
167
168void QQmlJSTypeDescriptionReader::readComponent(UiObjectDefinition *ast)
169{
170 m_currentCtorIndex = 0;
171 m_currentMethodIndex = 0;
172 QQmlJSScope::Ptr scope = QQmlJSScope::create();
173 QList<QQmlJSScope::Export> exports;
174
175 UiScriptBinding *metaObjectRevisions = nullptr;
176 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
177 UiObjectMember *member = it->member;
178 auto *component = cast<UiObjectDefinition *>(member);
179 auto *script = cast<UiScriptBinding *>(member);
180 if (component) {
181 QString name = toString(component->qualifiedTypeNameId);
182 if (name == QLatin1String("Property"))
183 readProperty(component, scope);
184 else if (name == QLatin1String("Method") || name == QLatin1String("Signal"))
185 readSignalOrMethod(component, name == QLatin1String("Method"), scope);
186 else if (name == QLatin1String("Enum"))
187 readEnum(component, scope);
188 else
189 addWarning(component->firstSourceLocation(),
190 tr("Expected only Property, Method, Signal and Enum object definitions, "
191 "not \"%1\".").arg(name));
192 } else if (script) {
193 QString name = toString(script->qualifiedId);
194 if (name == QLatin1String("file")) {
195 scope->setFilePath(readStringBinding(script));
196 } else if (name == QLatin1String("lineNumber")) {
197 scope->setLineNumber(readNumericBinding(script));
198 } else if (name == QLatin1String("name")) {
199 scope->setInternalName(readStringBinding(script));
200 } else if (name == QLatin1String("prototype")) {
201 scope->setBaseTypeName(readStringBinding(script));
202 } else if (name == QLatin1String("defaultProperty")) {
203 scope->setOwnDefaultPropertyName(readStringBinding(script));
204 } else if (name == QLatin1String("parentProperty")) {
205 scope->setOwnParentPropertyName(readStringBinding(script));
206 } else if (name == QLatin1String("exports")) {
207 exports = readExports(script);
208 } else if (name == QLatin1String("aliases")) {
209 readAliases(script, scope);
210 } else if (name == QLatin1String("interfaces")) {
211 readInterfaces(script, scope);
212 } else if (name == QLatin1String("exportMetaObjectRevisions")) {
213 metaObjectRevisions = script;
214 } else if (name == QLatin1String("attachedType")) {
215 scope->setOwnAttachedTypeName(readStringBinding(script));
216 } else if (name == QLatin1String("valueType")) {
217 scope->setElementTypeName(readStringBinding(script));
218 } else if (name == QLatin1String("isSingleton")) {
219 scope->setIsSingleton(readBoolBinding(script));
220 } else if (name == QLatin1String("isCreatable")) {
221 scope->setCreatableFlag(readBoolBinding(script));
222 } else if (name == QLatin1String("isStructured")) {
223 scope->setStructuredFlag(readBoolBinding(script));
224 } else if (name == QLatin1String("isComposite")) {
225 scope->setIsComposite(readBoolBinding(script));
226 } else if (name == QLatin1String("hasCustomParser")) {
227 scope->setHasCustomParser(readBoolBinding(script));
228 } else if (name == QLatin1String("enforcesScopedEnums")) {
229 scope->setEnforcesScopedEnumsFlag(readBoolBinding(script));
230 } else if (name == QLatin1String("accessSemantics")) {
231 const QString semantics = readStringBinding(script);
232 if (semantics == QLatin1String("reference")) {
233 scope->setAccessSemantics(QQmlJSScope::AccessSemantics::Reference);
234 } else if (semantics == QLatin1String("value")) {
235 scope->setAccessSemantics(QQmlJSScope::AccessSemantics::Value);
236 } else if (semantics == QLatin1String("none")) {
237 scope->setAccessSemantics(QQmlJSScope::AccessSemantics::None);
238 } else if (semantics == QLatin1String("sequence")) {
239 scope->setAccessSemantics(QQmlJSScope::AccessSemantics::Sequence);
240 } else {
241 addWarning(script->firstSourceLocation(),
242 tr("Unknown access semantics \"%1\".").arg(semantics));
243 }
244 } else if (name == QLatin1String("extension")) {
245 scope->setExtensionTypeName(readStringBinding(script));
246 } else if (name == QLatin1String("extensionIsJavaScript")) {
247 scope->setExtensionIsJavaScript(readBoolBinding(script));
248 } else if (name == QLatin1String("extensionIsNamespace")) {
249 scope->setExtensionIsNamespace(readBoolBinding(script));
250 } else if (name == QLatin1String("deferredNames")) {
251 readDeferredNames(script, scope);
252 } else if (name == QLatin1String("immediateNames")) {
253 readImmediateNames(script, scope);
254 } else if (name == QLatin1String("isJavaScriptBuiltin")) {
255 scope->setIsJavaScriptBuiltin(true);
256 } else {
257 addWarning(script->firstSourceLocation(),
258 tr("Expected only lineNumber, name, prototype, defaultProperty, "
259 "attachedType, "
260 "valueType, exports, interfaces, isSingleton, isCreatable, "
261 "isStructured, isComposite, hasCustomParser, enforcesScopedEnums, "
262 "aliases, exportMetaObjectRevisions, deferredNames, and "
263 "immediateNames in script bindings, not \"%1\".")
264 .arg(name));
265 }
266 } else {
267 addWarning(member->firstSourceLocation(),
268 tr("Expected only script bindings and object definitions."));
269 }
270 }
271
272 if (scope->internalName().isEmpty()) {
273 addError(ast->firstSourceLocation(), tr("Component definition is missing a name binding."));
274 return;
275 }
276
277 if (metaObjectRevisions)
278 checkMetaObjectRevisions(metaObjectRevisions, &exports);
279 m_objects->append({scope, exports});
280}
281
282void QQmlJSTypeDescriptionReader::readSignalOrMethod(
283 UiObjectDefinition *ast, bool isMethod, const QQmlJSScope::Ptr &scope)
284{
285 QQmlJSMetaMethod metaMethod;
286 // ### confusion between Method and Slot. Method should be removed.
287 if (isMethod)
288 metaMethod.setMethodType(QQmlJSMetaMethodType::Slot);
289 else
290 metaMethod.setMethodType(QQmlJSMetaMethodType::Signal);
291
292 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
293 UiObjectMember *member = it->member;
294 auto *component = cast<UiObjectDefinition *>(member);
295 auto *script = cast<UiScriptBinding *>(member);
296 if (component) {
297 QString name = toString(component->qualifiedTypeNameId);
298 if (name == QLatin1String("Parameter")) {
299 readParameter(component, &metaMethod);
300 } else {
301 addWarning(component->firstSourceLocation(),
302 tr("Expected only Parameter in object definitions."));
303 }
304 } else if (script) {
305 QString name = toString(script->qualifiedId);
306 if (name == QLatin1String("name")) {
307 metaMethod.setMethodName(readStringBinding(script));
308 } else if (name == QLatin1String("lineNumber")) {
309 metaMethod.setSourceLocation(
310 SourceLocation::fromQSizeType(0, 0, readIntBinding(script), 1));
311 } else if (name == QLatin1String("type")) {
312 metaMethod.setReturnTypeName(readStringBinding(script));
313 } else if (name == QLatin1String("revision")) {
314 metaMethod.setRevision(readIntBinding(script));
315 } else if (name == QLatin1String("isCloned")) {
316 metaMethod.setIsCloned(readBoolBinding(script));
317 } else if (name == QLatin1String("isConstructor")) {
318 // The constructors in the moc json output are ordered the same
319 // way as the ones in the metaobject. qmltyperegistrar moves them into
320 // the same list as the other members, but maintains their order.
321 if (readBoolBinding(script)) {
322 metaMethod.setIsConstructor(true);
323 metaMethod.setConstructorIndex(
324 QQmlJSMetaMethod::RelativeFunctionIndex(m_currentCtorIndex++));
325 }
326 } else if (name == QLatin1String("isJavaScriptFunction")) {
327 metaMethod.setIsJavaScriptFunction(readBoolBinding(script));
328 } else if (name == QLatin1String("isList")) {
329 auto metaReturnType = metaMethod.returnValue();
330 metaReturnType.setIsList(readBoolBinding(script));
331 metaMethod.setReturnValue(metaReturnType);
332 } else if (name == QLatin1String("isPointer")) {
333 // TODO: We don't need this information. We can probably drop all isPointer members
334 // once we make sure that the type information is always complete. The
335 // description of the type being referenced has access semantics after all.
336 auto metaReturnType = metaMethod.returnValue();
337 metaReturnType.setIsPointer(readBoolBinding(script));
338 metaMethod.setReturnValue(metaReturnType);
339 } else if (name == QLatin1String("isTypeConstant")
340 || name == QLatin1String("isConstant")) {
341 // note: isConstant is only read for backwards compatibility
342 auto metaReturnType = metaMethod.returnValue();
343 metaReturnType.setTypeQualifier(readBoolBinding(script)
344 ? QQmlJSMetaParameter::Const
345 : QQmlJSMetaParameter::NonConst);
346 metaMethod.setReturnValue(metaReturnType);
347 } else if (name == QLatin1String("isMethodConstant")) {
348 metaMethod.setIsConst(readBoolBinding(script));
349 } else {
350 addWarning(script->firstSourceLocation(),
351 tr("Expected only name, lineNumber, type, revision, isPointer, "
352 "isTypeConstant, "
353 "isList, isCloned, isConstructor, isMethodConstant, and "
354 "isJavaScriptFunction in script bindings."));
355 }
356 } else {
357 addWarning(member->firstSourceLocation(),
358 tr("Expected only script bindings and object definitions."));
359 }
360 }
361
362 if (metaMethod.methodName().isEmpty()) {
363 addError(ast->firstSourceLocation(),
364 tr("Method or signal is missing a name script binding."));
365 return;
366 }
367
368 // Signals, slots and method share one index space. Constructors are separate.
369 // We also assume that the order and therefore the indexing of all methods is retained from
370 // moc's JSON output.
371 if (!metaMethod.isConstructor())
372 metaMethod.setMethodIndex(QQmlJSMetaMethod::RelativeFunctionIndex(m_currentMethodIndex++));
373
374 if (metaMethod.returnTypeName().isEmpty())
375 metaMethod.setReturnTypeName(QLatin1String("void"));
376
377 scope->addOwnMethod(metaMethod);
378}
379
380void QQmlJSTypeDescriptionReader::readProperty(UiObjectDefinition *ast, const QQmlJSScope::Ptr &scope)
381{
382 QQmlJSMetaProperty property;
383 property.setIsWritable(true); // default is writable
384 bool isRequired = false;
385
386 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
387 UiObjectMember *member = it->member;
388 auto *script = cast<UiScriptBinding *>(member);
389 if (!script) {
390 addWarning(member->firstSourceLocation(), tr("Expected script binding."));
391 continue;
392 }
393
394 QString id = toString(script->qualifiedId);
395 if (id == QLatin1String("name")) {
396 property.setPropertyName(readStringBinding(script));
397 } else if (id == QLatin1String("lineNumber")) {
398 property.setSourceLocation(
399 SourceLocation::fromQSizeType(0, 0, readIntBinding(script), 1));
400 } else if (id == QLatin1String("type")) {
401 property.setTypeName(readStringBinding(script));
402 } else if (id == QLatin1String("isPointer")) {
403 property.setIsPointer(readBoolBinding(script));
404 } else if (id == QLatin1String("isReadonly")) {
405 property.setIsWritable(!readBoolBinding(script));
406 } else if (id == QLatin1String("isRequired")) {
407 isRequired = readBoolBinding(script);
408 } else if (id == QLatin1String("isList")) {
409 property.setIsList(readBoolBinding(script));
410 } else if (id == QLatin1String("isFinal")) {
411 property.setIsFinal(readBoolBinding(script));
412 } else if (id == QLatin1String("isTypeConstant")) {
413 property.setIsTypeConstant(readBoolBinding(script));
414 } else if (id == QLatin1String("isPropertyConstant")) {
415 property.setIsPropertyConstant(readBoolBinding(script));
416 } else if (id == QLatin1String("isConstant")) {
417 // support old "isConstant" for backwards compatibility
418 property.setIsPropertyConstant(readBoolBinding(script));
419 } else if (id == QLatin1String("revision")) {
420 property.setRevision(readIntBinding(script));
421 } else if (id == QLatin1String("bindable")) {
422 property.setBindable(readStringBinding(script));
423 } else if (id == QLatin1String("read")) {
424 property.setRead(readStringBinding(script));
425 } else if (id == QLatin1String("write")) {
426 property.setWrite(readStringBinding(script));
427 } else if (id == QLatin1String("reset")) {
428 property.setReset(readStringBinding(script));
429 } else if (id == QLatin1String("notify")) {
430 property.setNotify(readStringBinding(script));
431 } else if (id == QLatin1String("index")) {
432 property.setIndex(readIntBinding(script));
433 } else if (id == QLatin1String("privateClass")) {
434 property.setPrivateClass(readStringBinding(script));
435 } else {
436 addWarning(script->firstSourceLocation(),
437 tr("Expected only type, name, lineNumber, revision, isPointer, "
438 "isTypeConstant, isReadonly, isRequired, "
439 "isFinal, isList, bindable, read, write, isPropertyConstant, reset, "
440 "notify, index, and "
441 "privateClass and script bindings."));
442 }
443 }
444
445 if (property.propertyName().isEmpty()) {
446 addError(ast->firstSourceLocation(),
447 tr("Property object is missing a name script binding."));
448 return;
449 }
450
451 scope->addOwnProperty(property);
452 if (isRequired)
453 scope->setPropertyLocallyRequired(property.propertyName(), true);
454}
455
456void QQmlJSTypeDescriptionReader::readEnum(UiObjectDefinition *ast, const QQmlJSScope::Ptr &scope)
457{
458 QQmlJSMetaEnum metaEnum;
459
460 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
461 UiObjectMember *member = it->member;
462 auto *script = cast<UiScriptBinding *>(member);
463 if (!script) {
464 addWarning(member->firstSourceLocation(), tr("Expected script binding."));
465 continue;
466 }
467
468 QString name = toString(script->qualifiedId);
469 if (name == QLatin1String("name")) {
470 metaEnum.setName(readStringBinding(script));
471 } else if (name == QLatin1String("alias")) {
472 metaEnum.setAlias(readStringBinding(script));
473 } else if (name == QLatin1String("isFlag")) {
474 metaEnum.setIsFlag(readBoolBinding(script));
475 } else if (name == QLatin1String("values")) {
476 readEnumValues(script, &metaEnum);
477 } else if (name == QLatin1String("isScoped")) {
478 metaEnum.setIsScoped(readBoolBinding(script));
479 } else if (name == QLatin1String("type")) {
480 metaEnum.setTypeName(readStringBinding(script));
481 } else if (name == QLatin1String("lineNumber")) {
482 metaEnum.setLineNumber(readIntBinding(script));
483 } else {
484 addWarning(script->firstSourceLocation(),
485 tr("Expected only name, alias, isFlag, values, isScoped, type, or "
486 "lineNumber."));
487 }
488 }
489
490 scope->addOwnEnumeration(metaEnum);
491}
492
493void QQmlJSTypeDescriptionReader::readParameter(UiObjectDefinition *ast, QQmlJSMetaMethod *metaMethod)
494{
495 QString name;
496 QString type;
497 bool isConstant = false;
498 bool isPointer = false;
499 bool isList = false;
500
501 for (UiObjectMemberList *it = ast->initializer->members; it; it = it->next) {
502 UiObjectMember *member = it->member;
503 auto *script = cast<UiScriptBinding *>(member);
504 if (!script) {
505 addWarning(member->firstSourceLocation(), tr("Expected script binding."));
506 continue;
507 }
508
509 const QString id = toString(script->qualifiedId);
510 if (id == QLatin1String("name")) {
511 name = readStringBinding(script);
512 } else if (id == QLatin1String("type")) {
513 type = readStringBinding(script);
514 } else if (id == QLatin1String("isPointer")) {
515 isPointer = readBoolBinding(script);
516 } else if (id == QLatin1String("isTypeConstant") || id == QLatin1String("isConstant")) {
517 // note: isConstant is only read for backwards compatibility
518 isConstant = readBoolBinding(script);
519 } else if (id == QLatin1String("isReadonly")) {
520 // ### unhandled
521 } else if (id == QLatin1String("isList")) {
522 isList = readBoolBinding(script);
523 } else {
524 addWarning(script->firstSourceLocation(),
525 tr("Expected only name, type, isPointer, isTypeConstant, isReadonly, "
526 "or IsList script bindings."));
527 }
528 }
529
530 QQmlJSMetaParameter p(name, type);
531 p.setTypeQualifier(isConstant ? QQmlJSMetaParameter::Const : QQmlJSMetaParameter::NonConst);
532 p.setIsPointer(isPointer);
533 p.setIsList(isList);
534 metaMethod->addParameter(std::move(p));
535}
536
537QString QQmlJSTypeDescriptionReader::readStringBinding(UiScriptBinding *ast)
538{
539 Q_ASSERT(ast);
540
541 if (!ast->statement) {
542 addError(ast->colonToken, tr("Expected string after colon."));
543 return QString();
544 }
545
546 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
547 if (!expStmt) {
548 addError(ast->statement->firstSourceLocation(), tr("Expected string after colon."));
549 return QString();
550 }
551
552 auto *stringLit = cast<StringLiteral *>(expStmt->expression);
553 if (!stringLit) {
554 addError(expStmt->firstSourceLocation(), tr("Expected string after colon."));
555 return QString();
556 }
557
558 return stringLit->value.toString();
559}
560
561bool QQmlJSTypeDescriptionReader::readBoolBinding(UiScriptBinding *ast)
562{
563 Q_ASSERT(ast);
564
565 if (!ast->statement) {
566 addError(ast->colonToken, tr("Expected boolean after colon."));
567 return false;
568 }
569
570 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
571 if (!expStmt) {
572 addError(ast->statement->firstSourceLocation(), tr("Expected boolean after colon."));
573 return false;
574 }
575
576 auto *trueLit = cast<TrueLiteral *>(expStmt->expression);
577 auto *falseLit = cast<FalseLiteral *>(expStmt->expression);
578 if (!trueLit && !falseLit) {
579 addError(expStmt->firstSourceLocation(), tr("Expected true or false after colon."));
580 return false;
581 }
582
583 return trueLit;
584}
585
586double QQmlJSTypeDescriptionReader::readNumericBinding(UiScriptBinding *ast)
587{
588 Q_ASSERT(ast);
589
590 if (!ast->statement) {
591 addError(ast->colonToken, tr("Expected numeric literal after colon."));
592 return 0;
593 }
594
595 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
596 if (!expStmt) {
597 addError(ast->statement->firstSourceLocation(),
598 tr("Expected numeric literal after colon."));
599 return 0;
600 }
601
602 auto *numericLit = cast<NumericLiteral *>(expStmt->expression);
603 if (!numericLit) {
604 addError(expStmt->firstSourceLocation(), tr("Expected numeric literal after colon."));
605 return 0;
606 }
607
608 return numericLit->value;
609}
610
611static QTypeRevision parseVersion(const QString &versionString)
612{
613 const int dotIdx = versionString.indexOf(QLatin1Char('.'));
614 if (dotIdx == -1)
615 return QTypeRevision();
616 bool ok = false;
617 const int maybeMajor = QStringView{versionString}.left(dotIdx).toInt(&ok);
618 if (!ok)
619 return QTypeRevision();
620 const int maybeMinor = QStringView{versionString}.mid(dotIdx + 1).toInt(&ok);
621 if (!ok)
622 return QTypeRevision();
623 return QTypeRevision::fromVersion(maybeMajor, maybeMinor);
624}
625
626QTypeRevision QQmlJSTypeDescriptionReader::readNumericVersionBinding(UiScriptBinding *ast)
627{
628 QTypeRevision invalidVersion;
629
630 if (!ast || !ast->statement) {
631 addError((ast ? ast->colonToken : SourceLocation()),
632 tr("Expected numeric literal after colon."));
633 return invalidVersion;
634 }
635
636 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
637 if (!expStmt) {
638 addError(ast->statement->firstSourceLocation(),
639 tr("Expected numeric literal after colon."));
640 return invalidVersion;
641 }
642
643 auto *numericLit = cast<NumericLiteral *>(expStmt->expression);
644 if (!numericLit) {
645 addError(expStmt->firstSourceLocation(), tr("Expected numeric literal after colon."));
646 return invalidVersion;
647 }
648
649 return parseVersion(m_source.mid(numericLit->literalToken.begin(),
650 numericLit->literalToken.length));
651}
652
653int QQmlJSTypeDescriptionReader::readIntBinding(UiScriptBinding *ast)
654{
655 double v = readNumericBinding(ast);
656 int i = static_cast<int>(v);
657
658 if (i != v) {
659 addError(ast->firstSourceLocation(), tr("Expected integer after colon."));
660 return 0;
661 }
662
663 return i;
664}
665
666ArrayPattern* QQmlJSTypeDescriptionReader::getArray(UiScriptBinding *ast)
667{
668 Q_ASSERT(ast);
669
670 if (!ast->statement) {
671 addError(ast->colonToken, tr("Expected array of strings after colon."));
672 return nullptr;
673 }
674
675 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
676 if (!expStmt) {
677 addError(ast->statement->firstSourceLocation(),
678 tr("Expected array of strings after colon."));
679 return nullptr;
680 }
681
682 auto *arrayLit = cast<ArrayPattern *>(expStmt->expression);
683 if (!arrayLit) {
684 addError(expStmt->firstSourceLocation(), tr("Expected array of strings after colon."));
685 return nullptr;
686 }
687
688 return arrayLit;
689}
690
691QList<QQmlJSScope::Export> QQmlJSTypeDescriptionReader::readExports(UiScriptBinding *ast)
692{
693 QList<QQmlJSScope::Export> exports;
694 auto *arrayLit = getArray(ast);
695
696 if (!arrayLit)
697 return exports;
698
699 for (PatternElementList *it = arrayLit->elements; it; it = it->next) {
700 auto *stringLit = cast<StringLiteral *>(it->element->initializer);
701
702 if (!stringLit) {
703 addError(arrayLit->firstSourceLocation(),
704 tr("Expected array literal with only string literal members."));
705 return exports;
706 }
707
708 QString exp = stringLit->value.toString();
709 int slashIdx = exp.indexOf(QLatin1Char('/'));
710 int spaceIdx = exp.indexOf(QLatin1Char(' '));
711 const QTypeRevision version = parseVersion(exp.mid(spaceIdx + 1));
712
713 if (spaceIdx == -1 || !version.isValid()) {
714 addError(stringLit->firstSourceLocation(),
715 tr("Expected string literal to contain 'Package/Name major.minor' "
716 "or 'Name major.minor'."));
717 continue;
718 }
719 QString package;
720 if (slashIdx != -1)
721 package = exp.left(slashIdx);
722 QString name = exp.mid(slashIdx + 1, spaceIdx - (slashIdx+1));
723
724 // ### relocatable exports where package is empty?
725 exports.append(QQmlJSScope::Export(package, name, version, version));
726 }
727
728 return exports;
729}
730
731void QQmlJSTypeDescriptionReader::readAliases(
732 QQmlJS::AST::UiScriptBinding *ast, const QQmlJSScope::Ptr &scope)
733{
734 scope->setAliases(readStringList(ast));
735}
736
737void QQmlJSTypeDescriptionReader::readInterfaces(UiScriptBinding *ast, const QQmlJSScope::Ptr &scope)
738{
739 auto *arrayLit = getArray(ast);
740
741 if (!arrayLit)
742 return;
743
744 QStringList list;
745
746 for (PatternElementList *it = arrayLit->elements; it; it = it->next) {
747 auto *stringLit = cast<StringLiteral *>(it->element->initializer);
748 if (!stringLit) {
749 addError(arrayLit->firstSourceLocation(),
750 tr("Expected array literal with only string literal members."));
751 return;
752 }
753
754 list << stringLit->value.toString();
755 }
756
757 scope->setInterfaceNames(list);
758}
759
760void QQmlJSTypeDescriptionReader::checkMetaObjectRevisions(
761 UiScriptBinding *ast, QList<QQmlJSScope::Export> *exports)
762{
763 Q_ASSERT(ast);
764
765 if (!ast->statement) {
766 addError(ast->colonToken, tr("Expected array of numbers after colon."));
767 return;
768 }
769
770 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
771 if (!expStmt) {
772 addError(ast->statement->firstSourceLocation(),
773 tr("Expected array of numbers after colon."));
774 return;
775 }
776
777 auto *arrayLit = cast<ArrayPattern *>(expStmt->expression);
778 if (!arrayLit) {
779 addError(expStmt->firstSourceLocation(), tr("Expected array of numbers after colon."));
780 return;
781 }
782
783 int exportIndex = 0;
784 const int exportCount = exports->size();
785 for (PatternElementList *it = arrayLit->elements; it; it = it->next, ++exportIndex) {
786 auto *numberLit = cast<NumericLiteral *>(it->element->initializer);
787 if (!numberLit) {
788 addError(arrayLit->firstSourceLocation(),
789 tr("Expected array literal with only number literal members."));
790 return;
791 }
792
793 if (exportIndex >= exportCount) {
794 addError(numberLit->firstSourceLocation(),
795 tr("Meta object revision without matching export."));
796 return;
797 }
798
799 const double v = numberLit->value;
800 const int metaObjectRevision = static_cast<int>(v);
801 if (metaObjectRevision != v) {
802 addError(numberLit->firstSourceLocation(), tr("Expected integer."));
803 return;
804 }
805
806 const QTypeRevision metaObjectVersion
807 = QTypeRevision::fromEncodedVersion(metaObjectRevision);
808 const QQmlJSScope::Export &entry = exports->at(exportIndex);
809 const QTypeRevision exportVersion = entry.version();
810 if (metaObjectVersion != exportVersion) {
811 addWarning(numberLit->firstSourceLocation(),
812 tr("Meta object revision and export version differ.\n"
813 "Revision %1 corresponds to version %2.%3; it should be %4.%5.")
814 .arg(metaObjectRevision)
815 .arg(metaObjectVersion.majorVersion()).arg(metaObjectVersion.minorVersion())
816 .arg(exportVersion.majorVersion()).arg(exportVersion.minorVersion()));
817 (*exports)[exportIndex] = QQmlJSScope::Export(entry.package(), entry.type(),
818 exportVersion, metaObjectVersion);
819 }
820 }
821}
822
823QStringList QQmlJSTypeDescriptionReader::readStringList(UiScriptBinding *ast)
824{
825 auto *arrayLit = getArray(ast);
826 if (!arrayLit)
827 return {};
828
829 QStringList list;
830
831 for (PatternElementList *it = arrayLit->elements; it; it = it->next) {
832 auto *stringLit = cast<StringLiteral *>(it->element->initializer);
833 if (!stringLit) {
834 addError(arrayLit->firstSourceLocation(),
835 tr("Expected array literal with only string literal members."));
836 return {};
837 }
838
839 list << stringLit->value.toString();
840 }
841
842 return list;
843}
844
845void QQmlJSTypeDescriptionReader::readDeferredNames(UiScriptBinding *ast,
846 const QQmlJSScope::Ptr &scope)
847{
848 scope->setOwnDeferredNames(readStringList(ast));
849}
850
851void QQmlJSTypeDescriptionReader::readImmediateNames(UiScriptBinding *ast,
852 const QQmlJSScope::Ptr &scope)
853{
854 scope->setOwnImmediateNames(readStringList(ast));
855}
856
857void QQmlJSTypeDescriptionReader::readEnumValues(UiScriptBinding *ast, QQmlJSMetaEnum *metaEnum)
858{
859 if (!ast)
860 return;
861 if (!ast->statement) {
862 addError(ast->colonToken, tr("Expected object literal after colon."));
863 return;
864 }
865
866 auto *expStmt = cast<ExpressionStatement *>(ast->statement);
867 if (!expStmt) {
868 addError(ast->statement->firstSourceLocation(), tr("Expected expression after colon."));
869 return;
870 }
871
872 if (auto *objectLit = cast<ObjectPattern *>(expStmt->expression)) {
873 int currentValue = -1;
874 for (PatternPropertyList *it = objectLit->properties; it; it = it->next) {
875 if (PatternProperty *assignement = it->property) {
876 if (auto *name = cast<StringLiteralPropertyName *>(assignement->name)) {
877 metaEnum->addKey(name->id.toString());
878
879 if (auto *value = AST::cast<NumericLiteral *>(assignement->initializer)) {
880 currentValue = int(value->value);
881 } else if (auto *minus = AST::cast<UnaryMinusExpression *>(
882 assignement->initializer)) {
883 if (auto *value = AST::cast<NumericLiteral *>(minus->expression))
884 currentValue = -int(value->value);
885 else
886 ++currentValue;
887 } else {
888 ++currentValue;
889 }
890
891 metaEnum->addValue(currentValue);
892 continue;
893 }
894 }
895 addError(it->firstSourceLocation(), tr("Expected strings as enum keys."));
896 }
897 } else if (auto *arrayLit = cast<ArrayPattern *>(expStmt->expression)) {
898 for (PatternElementList *it = arrayLit->elements; it; it = it->next) {
899 if (PatternElement *element = it->element) {
900 if (auto *name = cast<StringLiteral *>(element->initializer)) {
901 metaEnum->addKey(name->value.toString());
902 continue;
903 }
904 }
905 addError(it->firstSourceLocation(), tr("Expected strings as enum keys."));
906 }
907 } else {
908 addError(ast->statement->firstSourceLocation(),
909 tr("Expected either array or object literal as enum definition."));
910 }
911}
912
913QT_END_NAMESPACE
static QTypeRevision parseVersion(const QString &versionString)
QString toString(const UiQualifiedId *qualifiedId, QChar delimiter=QLatin1Char('.'))