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