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