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
qqmlirbuilder_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLIRBUILDER_P_H
6#define QQMLIRBUILDER_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmljsast_p.h>
20#include <private/qqmljsengine_p.h>
21#include <private/qv4compiler_p.h>
22#include <private/qv4compileddata_p.h>
23#include <private/qqmljsmemorypool_p.h>
24#include <private/qqmljsfixedpoolarray_p.h>
25#include <private/qv4codegen_p.h>
26#include <private/qv4compiler_p.h>
27#include <QTextStream>
28#include <QCoreApplication>
29
30QT_BEGIN_NAMESPACE
31
32class QQmlPropertyCache;
33class QQmlContextData;
34class QQmlTypeNameCache;
35struct QQmlIRLoader;
36class QTypeRevision;
37
38namespace QmlIR {
39
40struct Document;
41
42template <typename T>
44{
46 : first(nullptr)
47 , last(nullptr)
48 {}
49
51 T *last;
52 int count = 0;
53
54 int append(T *item) {
55 item->next = nullptr;
56 if (last)
57 last->next = item;
58 else
59 first = item;
60 last = item;
61 return count++;
62 }
63
64 void prepend(T *item) {
65 item->next = first;
66 first = item;
67 if (!last)
68 last = first;
69 ++count;
70 }
71
72 T *unlink(T *before, T *item) {
73 T * const newNext = item->next;
74
75 if (before)
76 before->next = newNext;
77 else
78 first = newNext;
79
80 if (item == last) {
81 if (newNext)
82 last = newNext;
83 else
84 last = first;
85 }
86
87 --count;
88 return newNext;
89 }
90
91 T *slowAt(int index) const
92 {
93 T *result = first;
94 while (index > 0 && result) {
95 result = result->next;
96 --index;
97 }
98 return result;
99 }
100
101 struct Iterator {
102 // turn Iterator into a proper iterator
104 using value_type = T;
106 using pointer = T *;
107 using reference = T &;
108
109 T *ptr;
110
111 explicit Iterator(T *p) : ptr(p) {}
112
113 T *operator->() {
114 return ptr;
115 }
116
117 const T *operator->() const {
118 return ptr;
119 }
120
121 T &operator*() {
122 return *ptr;
123 }
124
125 const T &operator*() const {
126 return *ptr;
127 }
128
130 ptr = ptr->next;
131 return *this;
132 }
133
135 Iterator that {ptr};
136 ptr = ptr->next;
137 return that;
138 }
139
140 bool operator==(const Iterator &rhs) const {
141 return ptr == rhs.ptr;
142 }
143
144 bool operator!=(const Iterator &rhs) const {
145 return ptr != rhs.ptr;
146 }
147
148 operator T *() { return ptr; }
149 operator const T *() const { return ptr; }
150 };
151
153 Iterator end() { return Iterator(nullptr); }
154
156};
157
158struct Object;
159
161{
163};
164
177
178
180{
182
183 template<typename IdGenerator>
184 static bool initType(
185 QV4::CompiledData::ParameterType *type, const IdGenerator &idGenerator,
186 const QQmlJS::AST::Type *annotation)
187 {
188 using Flag = QV4::CompiledData::ParameterType::Flag;
189
190 if (!annotation)
191 return initType(type, QString(), idGenerator(QString()), Flag::NoFlag);
192
193 const QString typeId = annotation->typeId->toString();
194 const QString typeArgument =
195 annotation->typeArgument ? annotation->typeArgument->toString() : QString();
196
197 if (typeArgument.isEmpty())
198 return initType(type, typeId, idGenerator(typeId), Flag::NoFlag);
199
200 if (typeId == QLatin1String("list"))
201 return initType(type, typeArgument, idGenerator(typeArgument), Flag::List);
202
203 const QString annotationString = annotation->toString();
204 return initType(type, annotationString, idGenerator(annotationString), Flag::NoFlag);
205 }
206
207 static QV4::CompiledData::CommonType stringToBuiltinType(const QString &typeName);
208
209private:
210 static bool initType(
211 QV4::CompiledData::ParameterType *paramType, const QString &typeName,
212 int typeNameIndex, QV4::CompiledData::ParameterType::Flag listFlag);
213};
214
215struct Signal
216{
220
221 QStringList parameterStringList(const QV4::Compiler::StringTableGenerator *stringPool) const;
222
223 int parameterCount() const { return parameters->count; }
224 PoolList<Parameter>::Iterator parametersBegin() const { return parameters->begin(); }
226
228};
229
231{
233};
234
236{
237 // The offset in the source file where the binding appeared. This is used for sorting to ensure
238 // that assignments to list properties are done in the correct order. We use the offset here instead
239 // of Binding::location as the latter has limited precision.
241 // Binding's compiledScriptIndex is index in object's functionsAndExpressions
243};
244
245// we support one or two '.' in the enum phrase:
246// * <TypeName>.<EnumValue>
247// * <TypeName>.<ScopedEnumName>.<EnumValue>
248inline int qualifiedEnumDot(QStringView source)
249{
250 if (source.isEmpty() || !source.front().isUpper())
251 return -1;
252
253 // reject any "complex" expression (even simple arithmetic) by excluding everything that is not
254 // part of a valid identifier or a dot
255 for (const QChar c : source) {
256 if (!(c.isLetterOrNumber() || c == u'.' || c == u'_' || c.isSpace()))
257 return -1;
258 }
259
260 const qsizetype dot = source.indexOf(u'.');
261 return (dot == -1 || dot == source.size() - 1) ? -1 : dot;
262}
263
268
269struct Alias : public QV4::CompiledData::Alias
270{
272};
273
278
280{
284 quint32 index = 0; // index in parsedQML::functions
287
288 // --- QQmlPropertyCacheCreator interface
289 const Parameter *formalsBegin() const { return formals.begin(); }
290 const Parameter *formalsEnd() const { return formals.end(); }
291 // ---
292
294};
295
296struct Q_QML_COMPILER_EXPORT CompiledFunctionOrExpression
297{
300
301 QQmlJS::AST::Node *parentNode = nullptr; // FunctionDeclaration, Statement or Expression
302 QQmlJS::AST::Node *node = nullptr; // FunctionDeclaration, Statement or Expression
305};
306
307struct Q_QML_COMPILER_EXPORT Object
308{
309 Q_DECLARE_TR_FUNCTIONS(Object)
310public:
311 quint32 inheritedTypeNameIndex;
313 int id;
317
320
321 const Property *firstProperty() const { return properties->first; }
322 int propertyCount() const { return properties->count; }
323 Alias *firstAlias() const { return aliases->first; }
324 int aliasCount() const { return aliases->count; }
325 const Enum *firstEnum() const { return qmlEnums->first; }
326 int enumCount() const { return qmlEnums->count; }
327 const Signal *firstSignal() const { return qmlSignals->first; }
328 int signalCount() const { return qmlSignals->count; }
329 Binding *firstBinding() const { return bindings->first; }
330 int bindingCount() const { return bindings->count; }
331 const Function *firstFunction() const { return functions->first; }
332 int functionCount() const { return functions->count; }
333 const InlineComponent *inlineComponent() const { return inlineComponents->first; }
334 int inlineComponentCount() const { return inlineComponents->count; }
335 const RequiredPropertyExtraData *requiredPropertyExtraData() const {return requiredPropertyExtraDatas->first; }
336 int requiredPropertyExtraDataCount() const { return requiredPropertyExtraDatas->count; }
338 void sortAliasDependencies(const Document *doc, QList<QQmlJS::DiagnosticMessage> *errors);
339
340 PoolList<Binding>::Iterator bindingsBegin() const { return bindings->begin(); }
341 PoolList<Binding>::Iterator bindingsEnd() const { return bindings->end(); }
342 PoolList<Property>::Iterator propertiesBegin() const { return properties->begin(); }
343 PoolList<Property>::Iterator propertiesEnd() const { return properties->end(); }
344 PoolList<Alias>::Iterator aliasesBegin() const { return aliases->begin(); }
345 PoolList<Alias>::Iterator aliasesEnd() const { return aliases->end(); }
346 PoolList<Enum>::Iterator enumsBegin() const { return qmlEnums->begin(); }
347 PoolList<Enum>::Iterator enumsEnd() const { return qmlEnums->end(); }
348 PoolList<Signal>::Iterator signalsBegin() const { return qmlSignals->begin(); }
349 PoolList<Signal>::Iterator signalsEnd() const { return qmlSignals->end(); }
350 PoolList<Function>::Iterator functionsBegin() const { return functions->begin(); }
351 PoolList<Function>::Iterator functionsEnd() const { return functions->end(); }
352 PoolList<InlineComponent>::Iterator inlineComponentsBegin() const { return inlineComponents->begin(); }
353 PoolList<InlineComponent>::Iterator inlineComponentsEnd() const { return inlineComponents->end(); }
354 PoolList<RequiredPropertyExtraData>::Iterator requiredPropertyExtraDataBegin() const {return requiredPropertyExtraDatas->begin(); }
355 PoolList<RequiredPropertyExtraData>::Iterator requiredPropertyExtraDataEnd() const {return requiredPropertyExtraDatas->end(); }
356
357 // If set, then declarations for this object (and init bindings for these) should go into the
358 // specified object. Used for declarations inside group properties.
360
361 void init(QQmlJS::MemoryPool *pool, int typeNameIndex, int idIndex, const QV4::CompiledData::Location &location);
362
363 QString appendEnum(Enum *enumeration);
364 QString appendSignal(Signal *signal);
365 QString appendProperty(Property *prop, const QString &propertyName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation);
366 QString appendAlias(Alias *prop, const QString &aliasName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation);
367 void setFirstAlias(Alias *alias) { aliases->first = alias; }
368
369 void appendFunction(QmlIR::Function *f);
370 void appendInlineComponent(InlineComponent *ic);
371 void appendRequiredPropertyExtraData(RequiredPropertyExtraData *extraData);
372
373 QString appendBinding(Binding *b, bool isListBinding);
374 Binding *findBinding(quint32 nameIndex) const;
375 Binding *unlinkBinding(Binding *before, Binding *binding) { return bindings->unlink(before, binding); }
376 QString bindingAsString(Document *doc, int scriptIndex) const;
377
380
382 int namedObjectsInComponentCount() const { return namedObjectsInComponent.size(); }
383 const quint32 *namedObjectsInComponentTable() const { return namedObjectsInComponent.begin(); }
384
385 bool hasFlag(QV4::CompiledData::Object::Flag flag) const { return flags & flag; }
386 qint32 objectId() const { return id; }
388
389private:
390 friend struct ::QQmlIRLoader;
391
392 PoolList<Property> *properties;
393 PoolList<Alias> *aliases;
394 PoolList<Enum> *qmlEnums;
395 PoolList<Signal> *qmlSignals;
396 PoolList<Binding> *bindings;
397 PoolList<Function> *functions;
398 PoolList<InlineComponent> *inlineComponents;
399 PoolList<RequiredPropertyExtraData> *requiredPropertyExtraDatas;
400};
401
402struct Q_QML_COMPILER_EXPORT Pragma
403{
404 enum PragmaType
405 {
406 Singleton,
407 Strict,
408 ListPropertyAssignBehavior,
409 ComponentBehavior,
410 FunctionSignatureBehavior,
411 NativeMethodBehavior,
412 ValueTypeBehavior,
413 Translator,
414 };
415
416 enum ListPropertyAssignBehaviorValue
417 {
418 Append,
419 Replace,
420 ReplaceIfNotDefault,
421 };
422
423 enum ComponentBehaviorValue
424 {
425 Unbound,
426 Bound
427 };
428
429 enum FunctionSignatureBehaviorValue
430 {
431 Ignored,
432 Enforced
433 };
434
435 enum NativeMethodBehaviorValue
436 {
437 AcceptThisObject,
438 RejectThisObject
439 };
440
441 enum ValueTypeBehaviorValue
442 {
443 Copy = 0x1,
444 Addressable = 0x2,
445 Assertable = 0x4,
446 };
447 Q_DECLARE_FLAGS(ValueTypeBehaviorValues, ValueTypeBehaviorValue);
448
449 PragmaType type;
450
451 union {
452 ListPropertyAssignBehaviorValue listPropertyAssignBehavior;
453 ComponentBehaviorValue componentBehavior;
454 FunctionSignatureBehaviorValue functionSignatureBehavior;
455 NativeMethodBehaviorValue nativeMethodBehavior;
456 ValueTypeBehaviorValues::Int valueTypeBehavior;
457 uint translationContextIndex;
458 };
459
460 QV4::CompiledData::Location location;
461};
462
463struct Q_QML_COMPILER_EXPORT Document
464{
465 // disable it explicitly, it's implicitly deleted because of the Engine::_pool
466 Q_DISABLE_COPY_MOVE(Document)
467
468 Document(const QString &fileName, const QString &finalUrl, bool debugMode);
477
479
480 bool isSingleton() const {
481 return std::any_of(pragmas.constBegin(), pragmas.constEnd(), [](const Pragma *pragma) {
482 return pragma->type == Pragma::Singleton;
483 });
484 }
485
486 int registerString(const QString &str) { return jsGenerator.registerString(str); }
487 QString stringAt(int index) const { return jsGenerator.stringForIndex(index); }
488
489 int objectCount() const {return objects.size();}
490 Object* objectAt(int i) const {return objects.at(i);}
491};
492
493class Q_QML_COMPILER_EXPORT ScriptDirectivesCollector : public QQmlJS::Directives
494{
495 QmlIR::Document *document;
496 QQmlJS::Engine *engine;
497 QV4::Compiler::JSUnitGenerator *jsGenerator;
498
499public:
500 ScriptDirectivesCollector(QmlIR::Document *doc);
501
502 void pragmaLibrary() override;
503 void importFile(const QString &jsfile, const QString &module, int lineNumber, int column) override;
504 void importModule(const QString &uri, const QString &version, const QString &module, int lineNumber, int column) override;
505};
506
507struct Q_QML_COMPILER_EXPORT IRBuilder : public QQmlJS::AST::Visitor
508{
509 Q_DECLARE_TR_FUNCTIONS(QQmlCodeGenerator)
510public:
511 IRBuilder();
512 bool generateFromQml(const QString &code, const QString &url, Document *output);
513
514 using QQmlJS::AST::Visitor::visit;
515 using QQmlJS::AST::Visitor::endVisit;
516
517 bool visit(QQmlJS::AST::UiImport *ast) override;
518 bool visit(QQmlJS::AST::UiPragma *ast) override;
519 bool visit(QQmlJS::AST::UiProgram *) override;
520 bool visit(QQmlJS::AST::UiArrayBinding *ast) override;
521 bool visit(QQmlJS::AST::UiObjectBinding *ast) override;
522 bool visit(QQmlJS::AST::UiObjectDefinition *ast) override;
523 bool visit(QQmlJS::AST::UiInlineComponent *ast) override;
524 bool visit(QQmlJS::AST::UiEnumDeclaration *ast) override;
525 bool visit(QQmlJS::AST::UiPublicMember *ast) override;
526 bool visit(QQmlJS::AST::UiScriptBinding *ast) override;
527 bool visit(QQmlJS::AST::UiSourceElement *ast) override;
528 bool visit(QQmlJS::AST::UiRequired *ast) override;
529
530 void throwRecursionDepthError() override
531 {
532 recordError(QQmlJS::SourceLocation(),
533 QStringLiteral("Maximum statement or expression depth exceeded"));
534 }
535
536 void accept(QQmlJS::AST::Node *node);
537
538 // returns index in _objects
539 bool defineQMLObject(
540 int *objectIndex, QQmlJS::AST::UiQualifiedId *qualifiedTypeNameId,
541 const QV4::CompiledData::Location &location,
542 QQmlJS::AST::UiObjectInitializer *initializer, Object *declarationsOverride = nullptr);
543
544 bool defineQMLObject(
545 int *objectIndex, QQmlJS::AST::UiObjectDefinition *node,
546 Object *declarationsOverride = nullptr)
547 {
548 const QQmlJS::SourceLocation location = node->qualifiedTypeNameId->firstSourceLocation();
549 return defineQMLObject(
550 objectIndex, node->qualifiedTypeNameId,
551 { location.startLine, location.startColumn }, node->initializer,
552 declarationsOverride);
553 }
554
555 static QString asString(QQmlJS::AST::UiQualifiedId *node);
556 QStringView asStringRef(QQmlJS::AST::Node *node);
557 static QTypeRevision extractVersion(QStringView string);
558 QStringView textRefAt(const QQmlJS::SourceLocation &loc) const
559 { return QStringView(sourceCode).mid(loc.offset, loc.length); }
560 QStringView textRefAt(const QQmlJS::SourceLocation &first,
561 const QQmlJS::SourceLocation &last) const;
562
563 virtual void setBindingValue(QV4::CompiledData::Binding *binding,
564 QQmlJS::AST::Statement *statement, QQmlJS::AST::Node *parentNode);
565 void tryGeneratingTranslationBinding(QStringView base, QQmlJS::AST::ArgumentList *args, QV4::CompiledData::Binding *binding);
566
567 void appendBinding(QQmlJS::AST::UiQualifiedId *name, QQmlJS::AST::Statement *value,
568 QQmlJS::AST::Node *parentNode);
569 void appendBinding(QQmlJS::AST::UiQualifiedId *name, int objectIndex, bool isOnAssignment = false);
570 void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation,
571 const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex,
572 QQmlJS::AST::Statement *value, QQmlJS::AST::Node *parentNode);
573 void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation,
574 const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex,
575 int objectIndex, bool isListItem = false, bool isOnAssignment = false);
576
577 bool appendAlias(QQmlJS::AST::UiPublicMember *node);
578
579 enum class IsQmlFunction { Yes, No };
580 virtual void registerFunctionExpr(QQmlJS::AST::FunctionExpression *fexp, IsQmlFunction);
581
582 Object *bindingsTarget() const;
583
584 bool setId(const QQmlJS::SourceLocation &idLocation, QQmlJS::AST::Statement *value);
585
586 // resolves qualified name (font.pixelSize for example) and returns the last name along
587 // with the object any right-hand-side of a binding should apply to.
588 bool resolveQualifiedId(QQmlJS::AST::UiQualifiedId **nameToResolve, Object **object, bool onAssignment = false);
589
590 void recordError(const QQmlJS::SourceLocation &location, const QString &description);
591
592 quint32 registerString(const QString &str) const { return jsGenerator->registerString(str); }
593 template <typename _Tp> _Tp *New() { return pool->New<_Tp>(); }
594
595 QString stringAt(int index) const { return jsGenerator->stringForIndex(index); }
596
597 static bool isStatementNodeScript(QQmlJS::AST::Statement *statement);
598 static bool isRedundantNullInitializerForPropertyDeclaration(Property *property, QQmlJS::AST::Statement *statement);
599
600 QString sanityCheckFunctionNames(Object *obj, QQmlJS::SourceLocation *errorLocation);
601
602 QList<QQmlJS::DiagnosticMessage> errors;
603
604 QSet<QString> inlineComponentsNames;
605
606 QList<const QV4::CompiledData::Import *> _imports;
607 QList<Pragma*> _pragmas;
608 QList<Object*> _objects;
609
610 QV4::CompiledData::TypeReferenceMap _typeReferences;
611
612 Object *_object;
613 Property *_propertyDeclaration;
614
615 QQmlJS::MemoryPool *pool;
616 QString sourceCode;
617 QV4::Compiler::JSUnitGenerator *jsGenerator;
618
619 bool insideInlineComponent = false;
620};
621
622struct Q_QML_COMPILER_EXPORT QmlUnitGenerator
623{
624 void generate(Document &output, const QV4::CompiledData::DependentTypesHasher &dependencyHasher = QV4::CompiledData::DependentTypesHasher());
625
626private:
627 typedef bool (Binding::*BindingFilter)() const;
628 char *writeBindings(char *bindingPtr, const Object *o, BindingFilter filter) const;
629};
630
631struct Q_QML_COMPILER_EXPORT JSCodeGen : public QV4::Compiler::Codegen
632{
633 JSCodeGen(Document *document,
634 QV4::Compiler::CodegenWarningInterface *iface =
635 QV4::Compiler::defaultCodegenWarningInterface(),
636 bool storeSourceLocations = false);
637
638 // Returns mapping from input functions to index in IR::Module::functions / compiledData->runtimeFunctions
639 QList<int>
640 generateJSCodeForFunctionsAndBindings(const QList<CompiledFunctionOrExpression> &functions);
641
642 bool generateRuntimeFunctions(QmlIR::Object *object);
643
644private:
645 Document *document;
646};
647
648// RegisterStringN ~= std::function<int(QStringView)>
649// FinalizeTranlationData ~= std::function<void(QV4::CompiledData::Binding::ValueType, QV4::CompiledData::TranslationData)>
650/*
651 \internal
652 \a base: name of the potential translation function
653 \a args: arguments to the function call
654 \a registerMainString: Takes the first argument passed to the translation function, and it's
655 result will be stored in a TranslationData's stringIndex for translation bindings and in numbeIndex
656 for string bindings.
657 \a registerCommentString: Takes the comment argument passed to some of the translation functions.
658 Result will be stored in a TranslationData's commentIndex
659 \a finalizeTranslationData: Takes the type of the binding and the previously set up TranslationData
660 */
661template<
662 typename RegisterMainString,
663 typename RegisterCommentString,
664 typename RegisterContextString,
665 typename FinalizeTranslationData>
666void tryGeneratingTranslationBindingBase(QStringView base, QQmlJS::AST::ArgumentList *args,
667 RegisterMainString registerMainString,
668 RegisterCommentString registerCommentString,
669 RegisterContextString registerContextString,
670 FinalizeTranslationData finalizeTranslationData
671 )
672{
673 if (base == QLatin1String("qsTr")) {
674 QV4::CompiledData::TranslationData translationData;
675 translationData.number = -1;
676
677 // empty string
678 translationData.commentIndex = 0;
679
680 // No context (not empty string)
681 translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex;
682
683 if (!args || !args->expression)
684 return; // no arguments, stop
685
686 QStringView translation;
687 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
688 translation = arg1->value;
689 } else {
690 return; // first argument is not a string, stop
691 }
692
693 translationData.stringIndex = registerMainString(translation);
694
695 args = args->next;
696
697 if (args) {
698 QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
699 if (!arg2)
700 return; // second argument is not a string, stop
701 translationData.commentIndex = registerCommentString(arg2->value);
702
703 args = args->next;
704 if (args) {
705 if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
706 translationData.number = int(arg3->value);
707 args = args->next;
708 } else {
709 return; // third argument is not a translation number, stop
710 }
711 }
712 }
713
714 if (args)
715 return; // too many arguments, stop
716
717 finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData);
718 } else if (base == QLatin1String("qsTrId")) {
719 QV4::CompiledData::TranslationData translationData;
720 translationData.number = -1;
721
722 // empty string, but unused
723 translationData.commentIndex = 0;
724
725 // No context (not empty string)
726 translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex;
727
728 if (!args || !args->expression)
729 return; // no arguments, stop
730
731 QStringView id;
732 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
733 id = arg1->value;
734 } else {
735 return; // first argument is not a string, stop
736 }
737 translationData.stringIndex = registerMainString(id);
738
739 args = args->next;
740
741 if (args) {
742 if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
743 translationData.number = int(arg3->value);
744 args = args->next;
745 } else {
746 return; // third argument is not a translation number, stop
747 }
748 }
749
750 if (args)
751 return; // too many arguments, stop
752
753 finalizeTranslationData(QV4::CompiledData::Binding::Type_TranslationById, translationData);
754 } else if (base == QLatin1String("QT_TR_NOOP") || base == QLatin1String("QT_TRID_NOOP")) {
755 if (!args || !args->expression)
756 return; // no arguments, stop
757
758 QStringView str;
759 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
760 str = arg1->value;
761 } else {
762 return; // first argument is not a string, stop
763 }
764
765 args = args->next;
766 // QT_TR_NOOP can have a disambiguation string, QT_TRID_NOOP can't
767 if (args && base == QLatin1String("QT_TR_NOOP")) {
768 // we have a disambiguation string; we don't need to do anything with it
769 if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression))
770 args = args->next;
771 else // second argument is not a string, stop
772 return;
773 }
774
775 if (args)
776 return; // too many arguments, stop
777
778 QV4::CompiledData::TranslationData translationData;
779 translationData.number = registerMainString(str);
780 finalizeTranslationData(QV4::CompiledData::Binding::Type_String, translationData);
781 } else if (base == QLatin1String("QT_TRANSLATE_NOOP")) {
782 if (!args || !args->expression)
783 return; // no arguments, stop
784
785 args = args->next;
786 if (!args || !args->expression)
787 return; // no second arguments, stop
788
789 QStringView str;
790 if (QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
791 str = arg2->value;
792 } else {
793 return; // first argument is not a string, stop
794 }
795
796 args = args->next;
797 if (args) {
798 // we have a disambiguation string; we don't need to do anything with it
799 if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression))
800 args = args->next;
801 else // third argument is not a string, stop
802 return;
803 }
804
805 if (args)
806 return; // too many arguments, stop
807
808 QV4::CompiledData::TranslationData fakeTranslationData;
809 fakeTranslationData.number = registerMainString(str);
810 finalizeTranslationData(QV4::CompiledData::Binding::Type_String, fakeTranslationData);
811 } else if (base == QLatin1String("qsTranslate")) {
812 QV4::CompiledData::TranslationData translationData;
813 translationData.number = -1;
814 translationData.commentIndex = 0; // empty string
815
816 if (!args || !args->next)
817 return; // less than 2 arguments, stop
818
819 QStringView translation;
820 if (QQmlJS::AST::StringLiteral *arg1
821 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
822 translation = arg1->value;
823 } else {
824 return; // first argument is not a string, stop
825 }
826
827 translationData.contextIndex = registerContextString(translation);
828
829 args = args->next;
830 Q_ASSERT(args);
831
832 QQmlJS::AST::StringLiteral *arg2
833 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
834 if (!arg2)
835 return; // second argument is not a string, stop
836 translationData.stringIndex = registerMainString(arg2->value);
837
838 args = args->next;
839 if (args) {
840 QQmlJS::AST::StringLiteral *arg3
841 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
842 if (!arg3)
843 return; // third argument is not a string, stop
844 translationData.commentIndex = registerCommentString(arg3->value);
845
846 args = args->next;
847 if (args) {
848 if (QQmlJS::AST::NumericLiteral *arg4
849 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
850 translationData.number = int(arg4->value);
851 args = args->next;
852 } else {
853 return; // fourth argument is not a translation number, stop
854 }
855 }
856 }
857
858 if (args)
859 return; // too many arguments, stop
860
861 finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData);
862 }
863}
864
865} // namespace QmlIR
866
867QT_END_NAMESPACE
868
869#endif // QQMLIRBUILDER_P_H
\inmodule QtCore
int qualifiedEnumDot(QStringView source)
void tryGeneratingTranslationBindingBase(QStringView base, QQmlJS::AST::ArgumentList *args, RegisterMainString registerMainString, RegisterCommentString registerCommentString, RegisterContextString registerContextString, FinalizeTranslationData finalizeTranslationData)
static const quint32 emptyStringIndex
#define COMPILE_EXCEPTION(location, desc)
static QStringList astNodeToStringList(QQmlJS::AST::Node *node)
static bool run(IRBuilder *builder, QQmlJS::AST::UiPragma *node, Pragma *pragma)
CompiledFunctionOrExpression * next
QQmlRefPointer< QV4::CompiledData::CompilationUnit > javaScriptCompilationUnit
Object * objectAt(int i) const
QQmlJS::AST::UiProgram * program
QString stringAt(int index) const
QV4::Compiler::Module jsModule
QV4::Compiler::JSUnitGenerator jsGenerator
int objectCount() const
int registerString(const QString &str)
bool isSingleton() const
QList< Pragma * > pragmas
QList< const QV4::CompiledData::Import * > imports
QQmlJS::Engine jsParserEngine
QList< Object * > objects
QV4::CompiledData::Location location
PoolList< EnumValue >::Iterator enumValuesEnd() const
PoolList< EnumValue > * enumValues
int enumValueCount() const
PoolList< EnumValue >::Iterator enumValuesBegin() const
QV4::CompiledData::Location location
const Parameter * formalsBegin() const
QV4::CompiledData::ParameterType returnType
QQmlJS::FixedPoolArray< Parameter > formals
const Parameter * formalsEnd() const
InlineComponent * next
bool hasFlag(QV4::CompiledData::Object::Flag flag) const
int indexOfDefaultPropertyOrAlias
PoolList< Property >::Iterator propertiesEnd() const
PoolList< Property >::Iterator propertiesBegin() const
int requiredPropertyExtraDataCount() const
QString appendEnum(Enum *enumeration)
PoolList< Signal >::Iterator signalsEnd() const
int inlineComponentCount() const
const quint32 * namedObjectsInComponentTable() const
void setFirstAlias(Alias *alias)
void appendRequiredPropertyExtraData(RequiredPropertyExtraData *extraData)
PoolList< RequiredPropertyExtraData >::Iterator requiredPropertyExtraDataEnd() const
PoolList< Binding >::Iterator bindingsEnd() const
int enumCount() const
PoolList< Function >::Iterator functionsBegin() const
void simplifyRequiredProperties()
QString appendBinding(Binding *b, bool isListBinding)
QV4::CompiledData::Location location
void appendInlineComponent(InlineComponent *ic)
Alias * firstAlias() const
int bindingCount() const
PoolList< Function >::Iterator functionsEnd() const
PoolList< Alias >::Iterator aliasesBegin() const
PoolList< RequiredPropertyExtraData >::Iterator requiredPropertyExtraDataBegin() const
PoolList< Enum >::Iterator enumsEnd() const
qint32 objectId() const
QString appendSignal(Signal *signal)
Binding * firstBinding() const
const Property * firstProperty() const
QString bindingAsString(Document *doc, int scriptIndex) const
const Enum * firstEnum() const
void init(QQmlJS::MemoryPool *pool, int typeNameIndex, int idIndex, const QV4::CompiledData::Location &location)
Object * declarationsOverride
QString appendAlias(Alias *prop, const QString &aliasName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation)
void sortAliasDependencies(const Document *doc, QList< QQmlJS::DiagnosticMessage > *errors)
int signalCount() const
int functionCount() const
QString appendProperty(Property *prop, const QString &propertyName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation)
PoolList< Enum >::Iterator enumsBegin() const
PoolList< CompiledFunctionOrExpression > * functionsAndExpressions
Binding * unlinkBinding(Binding *before, Binding *binding)
const RequiredPropertyExtraData * requiredPropertyExtraData() const
bool hasAliasAsDefaultProperty() const
PoolList< InlineComponent >::Iterator inlineComponentsEnd() const
PoolList< Signal >::Iterator signalsBegin() const
int aliasCount() const
void appendFunction(QmlIR::Function *f)
const Function * firstFunction() const
PoolList< Binding >::Iterator bindingsBegin() const
PoolList< InlineComponent >::Iterator inlineComponentsBegin() const
const InlineComponent * inlineComponent() const
QQmlJS::FixedPoolArray< int > runtimeFunctionIndices
const Signal * firstSignal() const
QV4::CompiledData::Location locationOfIdProperty
QQmlJS::FixedPoolArray< quint32 > namedObjectsInComponent
Binding * findBinding(quint32 nameIndex) const
int propertyCount() const
int namedObjectsInComponentCount() const
PoolList< Alias >::Iterator aliasesEnd() const
static bool initType(QV4::CompiledData::ParameterType *type, const IdGenerator &idGenerator, const QQmlJS::AST::Type *annotation)
static QV4::CompiledData::CommonType stringToBuiltinType(const QString &typeName)
bool operator!=(const Iterator &rhs) const
const T * operator->() const
bool operator==(const Iterator &rhs) const
const T & operator*() const
void prepend(T *item)
T * unlink(T *before, T *item)
T * slowAt(int index) const
int append(T *item)
RequiredPropertyExtraData * next
int parameterCount() const
PoolList< Parameter > * parameters
PoolList< Parameter >::Iterator parametersEnd() const
QStringList parameterStringList(const QV4::Compiler::StringTableGenerator *stringPool) const
PoolList< Parameter >::Iterator parametersBegin() const
QV4::CompiledData::Location location