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