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