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