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 QV4::Compiler::CodegenWarningInterface *wInterface =
514 QV4::Compiler::defaultCodegenWarningInterface());
515
516 using QQmlJS::AST::Visitor::visit;
517 using QQmlJS::AST::Visitor::endVisit;
518
519 bool visit(QQmlJS::AST::UiImport *ast) override;
520 bool visit(QQmlJS::AST::UiPragma *ast) override;
521 bool visit(QQmlJS::AST::UiProgram *) override;
522 bool visit(QQmlJS::AST::UiArrayBinding *ast) override;
523 bool visit(QQmlJS::AST::UiObjectBinding *ast) override;
524 bool visit(QQmlJS::AST::UiObjectDefinition *ast) override;
525 bool visit(QQmlJS::AST::UiInlineComponent *ast) override;
526 bool visit(QQmlJS::AST::UiEnumDeclaration *ast) override;
527 bool visit(QQmlJS::AST::UiPublicMember *ast) override;
528 bool visit(QQmlJS::AST::UiScriptBinding *ast) override;
529 bool visit(QQmlJS::AST::UiSourceElement *ast) override;
530 bool visit(QQmlJS::AST::UiRequired *ast) override;
531
532 void throwRecursionDepthError() override
533 {
534 recordError(QQmlJS::SourceLocation(),
535 QStringLiteral("Maximum statement or expression depth exceeded"));
536 }
537
538 void accept(QQmlJS::AST::Node *node);
539
540 // returns index in _objects
541 bool defineQMLObject(
542 int *objectIndex, QQmlJS::AST::UiQualifiedId *qualifiedTypeNameId,
543 const QV4::CompiledData::Location &location,
544 QQmlJS::AST::UiObjectInitializer *initializer, Object *declarationsOverride = nullptr);
545
546 bool defineQMLObject(
547 int *objectIndex, QQmlJS::AST::UiObjectDefinition *node,
548 Object *declarationsOverride = nullptr)
549 {
550 const QQmlJS::SourceLocation location = node->qualifiedTypeNameId->firstSourceLocation();
551 return defineQMLObject(
552 objectIndex, node->qualifiedTypeNameId,
553 { location.startLine, location.startColumn }, node->initializer,
554 declarationsOverride);
555 }
556
557 static QString asString(QQmlJS::AST::UiQualifiedId *node);
558 QStringView asStringRef(QQmlJS::AST::Node *node);
559 static QTypeRevision extractVersion(QStringView string);
560 QStringView textRefAt(const QQmlJS::SourceLocation &loc) const
561 { return QStringView(sourceCode).mid(loc.offset, loc.length); }
562 QStringView textRefAt(const QQmlJS::SourceLocation &first,
563 const QQmlJS::SourceLocation &last) const;
564
565 virtual void setBindingValue(QV4::CompiledData::Binding *binding,
566 QQmlJS::AST::Statement *statement, QQmlJS::AST::Node *parentNode);
567 void tryGeneratingTranslationBinding(QStringView base, QQmlJS::AST::ArgumentList *args, QV4::CompiledData::Binding *binding);
568
569 void appendBinding(QQmlJS::AST::UiQualifiedId *name, QQmlJS::AST::Statement *value,
570 QQmlJS::AST::Node *parentNode);
571 void appendBinding(QQmlJS::AST::UiQualifiedId *name, int objectIndex, bool isOnAssignment = false);
572 void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation,
573 const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex,
574 QQmlJS::AST::Statement *value, QQmlJS::AST::Node *parentNode);
575 void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation,
576 const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex,
577 int objectIndex, bool isListItem = false, bool isOnAssignment = false);
578
579 bool appendAlias(QQmlJS::AST::UiPublicMember *node);
580
581 enum class IsQmlFunction { Yes, No };
582 virtual void registerFunctionExpr(QQmlJS::AST::FunctionExpression *fexp, IsQmlFunction);
583
584 Object *bindingsTarget() const;
585
586 bool setId(const QQmlJS::SourceLocation &idLocation, QQmlJS::AST::Statement *value);
587
588 // resolves qualified name (font.pixelSize for example) and returns the last name along
589 // with the object any right-hand-side of a binding should apply to.
590 bool resolveQualifiedId(QQmlJS::AST::UiQualifiedId **nameToResolve, Object **object, bool onAssignment = false);
591
592 void recordError(const QQmlJS::SourceLocation &location, const QString &description);
593
594 quint32 registerString(const QString &str) const { return jsGenerator->registerString(str); }
595 template <typename _Tp> _Tp *New() { return pool->New<_Tp>(); }
596
597 QString stringAt(int index) const { return jsGenerator->stringForIndex(index); }
598
599 static bool isStatementNodeScript(QQmlJS::AST::Statement *statement);
600 static bool isRedundantNullInitializerForPropertyDeclaration(Property *property, QQmlJS::AST::Statement *statement);
601
602 QString sanityCheckFunctionNames(Object *obj, QQmlJS::SourceLocation *errorLocation);
603
604 QList<QQmlJS::DiagnosticMessage> errors;
605
606 QSet<QString> inlineComponentsNames;
607
608 QList<const QV4::CompiledData::Import *> _imports;
609 QList<Pragma*> _pragmas;
610 QList<Object*> _objects;
611
612 QV4::CompiledData::TypeReferenceMap _typeReferences;
613
614 Object *_object;
615 Property *_propertyDeclaration;
616
617 QQmlJS::MemoryPool *pool;
618 QString sourceCode;
619 QV4::Compiler::JSUnitGenerator *jsGenerator;
620
621 bool insideInlineComponent = false;
622};
623
624struct Q_QML_COMPILER_EXPORT QmlUnitGenerator
625{
626 void generate(Document &output, const QV4::CompiledData::DependentTypesHasher &dependencyHasher = QV4::CompiledData::DependentTypesHasher());
627
628private:
629 typedef bool (Binding::*BindingFilter)() const;
630 char *writeBindings(char *bindingPtr, const Object *o, BindingFilter filter) const;
631};
632
633struct Q_QML_COMPILER_EXPORT JSCodeGen : public QV4::Compiler::Codegen
634{
635 JSCodeGen(Document *document,
636 QV4::Compiler::CodegenWarningInterface *iface =
637 QV4::Compiler::defaultCodegenWarningInterface(),
638 bool storeSourceLocations = false);
639
640 // Returns mapping from input functions to index in IR::Module::functions / compiledData->runtimeFunctions
641 QList<int>
642 generateJSCodeForFunctionsAndBindings(const QList<CompiledFunctionOrExpression> &functions);
643
644 bool generateRuntimeFunctions(QmlIR::Object *object);
645
646private:
647 Document *document;
648};
649
650// RegisterStringN ~= std::function<int(QStringView)>
651// FinalizeTranlationData ~= std::function<void(QV4::CompiledData::Binding::ValueType, QV4::CompiledData::TranslationData)>
652/*
653 \internal
654 \a base: name of the potential translation function
655 \a args: arguments to the function call
656 \a registerMainString: Takes the first argument passed to the translation function, and it's
657 result will be stored in a TranslationData's stringIndex for translation bindings and in numbeIndex
658 for string bindings.
659 \a registerCommentString: Takes the comment argument passed to some of the translation functions.
660 Result will be stored in a TranslationData's commentIndex
661 \a finalizeTranslationData: Takes the type of the binding and the previously set up TranslationData
662 */
663template<
664 typename RegisterMainString,
665 typename RegisterCommentString,
666 typename RegisterContextString,
667 typename FinalizeTranslationData>
668void tryGeneratingTranslationBindingBase(QStringView base, QQmlJS::AST::ArgumentList *args,
669 RegisterMainString registerMainString,
670 RegisterCommentString registerCommentString,
671 RegisterContextString registerContextString,
672 FinalizeTranslationData finalizeTranslationData
673 )
674{
675 if (base == QLatin1String("qsTr")) {
676 QV4::CompiledData::TranslationData translationData;
677 translationData.number = -1;
678
679 // empty string
680 translationData.commentIndex = 0;
681
682 // No context (not empty string)
683 translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex;
684
685 if (!args || !args->expression)
686 return; // no arguments, stop
687
688 QStringView translation;
689 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
690 translation = arg1->value;
691 } else {
692 return; // first argument is not a string, stop
693 }
694
695 translationData.stringIndex = registerMainString(translation);
696
697 args = args->next;
698
699 if (args) {
700 QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
701 if (!arg2)
702 return; // second argument is not a string, stop
703 translationData.commentIndex = registerCommentString(arg2->value);
704
705 args = args->next;
706 if (args) {
707 if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
708 translationData.number = int(arg3->value);
709 args = args->next;
710 } else {
711 return; // third argument is not a translation number, stop
712 }
713 }
714 }
715
716 if (args)
717 return; // too many arguments, stop
718
719 finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData);
720 } else if (base == QLatin1String("qsTrId")) {
721 QV4::CompiledData::TranslationData translationData;
722 translationData.number = -1;
723
724 // empty string, but unused
725 translationData.commentIndex = 0;
726
727 // No context (not empty string)
728 translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex;
729
730 if (!args || !args->expression)
731 return; // no arguments, stop
732
733 QStringView id;
734 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
735 id = arg1->value;
736 } else {
737 return; // first argument is not a string, stop
738 }
739 translationData.stringIndex = registerMainString(id);
740
741 args = args->next;
742
743 if (args) {
744 if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
745 translationData.number = int(arg3->value);
746 args = args->next;
747 } else {
748 return; // third argument is not a translation number, stop
749 }
750 }
751
752 if (args)
753 return; // too many arguments, stop
754
755 finalizeTranslationData(QV4::CompiledData::Binding::Type_TranslationById, translationData);
756 } else if (base == QLatin1String("QT_TR_NOOP") || base == QLatin1String("QT_TRID_NOOP")) {
757 if (!args || !args->expression)
758 return; // no arguments, stop
759
760 QStringView str;
761 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
762 str = arg1->value;
763 } else {
764 return; // first argument is not a string, stop
765 }
766
767 args = args->next;
768 // QT_TR_NOOP can have a disambiguation string, QT_TRID_NOOP can't
769 if (args && base == QLatin1String("QT_TR_NOOP")) {
770 // we have a disambiguation string; we don't need to do anything with it
771 if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression))
772 args = args->next;
773 else // second argument is not a string, stop
774 return;
775 }
776
777 if (args)
778 return; // too many arguments, stop
779
780 QV4::CompiledData::TranslationData translationData;
781 translationData.number = registerMainString(str);
782 finalizeTranslationData(QV4::CompiledData::Binding::Type_String, translationData);
783 } else if (base == QLatin1String("QT_TRANSLATE_NOOP")) {
784 if (!args || !args->expression)
785 return; // no arguments, stop
786
787 args = args->next;
788 if (!args || !args->expression)
789 return; // no second arguments, stop
790
791 QStringView str;
792 if (QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
793 str = arg2->value;
794 } else {
795 return; // first argument is not a string, stop
796 }
797
798 args = args->next;
799 if (args) {
800 // we have a disambiguation string; we don't need to do anything with it
801 if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression))
802 args = args->next;
803 else // third argument is not a string, stop
804 return;
805 }
806
807 if (args)
808 return; // too many arguments, stop
809
810 QV4::CompiledData::TranslationData fakeTranslationData;
811 fakeTranslationData.number = registerMainString(str);
812 finalizeTranslationData(QV4::CompiledData::Binding::Type_String, fakeTranslationData);
813 } else if (base == QLatin1String("qsTranslate")) {
814 QV4::CompiledData::TranslationData translationData;
815 translationData.number = -1;
816 translationData.commentIndex = 0; // empty string
817
818 if (!args || !args->next)
819 return; // less than 2 arguments, stop
820
821 QStringView translation;
822 if (QQmlJS::AST::StringLiteral *arg1
823 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
824 translation = arg1->value;
825 } else {
826 return; // first argument is not a string, stop
827 }
828
829 translationData.contextIndex = registerContextString(translation);
830
831 args = args->next;
832 Q_ASSERT(args);
833
834 QQmlJS::AST::StringLiteral *arg2
835 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
836 if (!arg2)
837 return; // second argument is not a string, stop
838 translationData.stringIndex = registerMainString(arg2->value);
839
840 args = args->next;
841 if (args) {
842 QQmlJS::AST::StringLiteral *arg3
843 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
844 if (!arg3)
845 return; // third argument is not a string, stop
846 translationData.commentIndex = registerCommentString(arg3->value);
847
848 args = args->next;
849 if (args) {
850 if (QQmlJS::AST::NumericLiteral *arg4
851 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
852 translationData.number = int(arg4->value);
853 args = args->next;
854 } else {
855 return; // fourth argument is not a translation number, stop
856 }
857 }
858 }
859
860 if (args)
861 return; // too many arguments, stop
862
863 finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData);
864 }
865}
866
867} // namespace QmlIR
868
869QT_END_NAMESPACE
870
871#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