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;
35class QTypeRevision;
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
244// we support one or two '.' in the enum phrase:
245// * <TypeName>.<EnumValue>
246// * <TypeName>.<ScopedEnumName>.<EnumValue>
247inline int qualifiedEnumDot(QStringView source)
248{
249 if (source.isEmpty() || !source.front().isUpper())
250 return -1;
251
252 // reject any "complex" expression (even simple arithmetic) by excluding everything that is not
253 // part of a valid identifier or a dot
254 for (const QChar c : source) {
255 if (!(c.isLetterOrNumber() || c == u'.' || c == u'_' || c.isSpace()))
256 return -1;
257 }
258
259 const qsizetype dot = source.indexOf(u'.');
260 return (dot == -1 || dot == source.size() - 1) ? -1 : dot;
261}
262
267
268struct Alias : public QV4::CompiledData::Alias
269{
271};
272
277
279{
283 quint32 index = 0; // index in parsedQML::functions
286
287 // --- QQmlPropertyCacheCreator interface
288 const Parameter *formalsBegin() const { return formals.begin(); }
289 const Parameter *formalsEnd() const { return formals.end(); }
290 // ---
291
293};
294
295struct Q_QML_COMPILER_EXPORT CompiledFunctionOrExpression
296{
299
300 QQmlJS::AST::Node *parentNode = nullptr; // FunctionDeclaration, Statement or Expression
301 QQmlJS::AST::Node *node = nullptr; // FunctionDeclaration, Statement or Expression
304};
305
306struct Q_QML_COMPILER_EXPORT Object
307{
308 Q_DECLARE_TR_FUNCTIONS(Object)
309public:
310 quint32 inheritedTypeNameIndex;
312 int id;
315 bool isComponent; // object was identified to be an explicit or implicit component boundary
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 grouped 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,
366 const QQmlJS::SourceLocation &defaultToken,
367 QQmlJS::SourceLocation *errorLocation,
368 QQmlJS::DiagnosticCategory *errorCategory);
369 QString appendAlias(Alias *prop, const QString &aliasName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation);
370 void setFirstAlias(Alias *alias) { aliases->first = alias; }
371
372 void appendFunction(QmlIR::Function *f);
373 void appendInlineComponent(InlineComponent *ic);
374 void appendRequiredPropertyExtraData(RequiredPropertyExtraData *extraData);
375
376 QString appendBinding(Binding *b, bool isListBinding);
377 Binding *findBinding(quint32 nameIndex) const;
378 Binding *unlinkBinding(Binding *before, Binding *binding) { return bindings->unlink(before, binding); }
379 QString bindingAsString(Document *doc, int scriptIndex) const;
380
383
385 int namedObjectsInComponentCount() const { return namedObjectsInComponent.size(); }
386 const quint32 *namedObjectsInComponentTable() const { return namedObjectsInComponent.begin(); }
387
388 bool hasFlag(QV4::CompiledData::Object::Flag flag) const { return flags & flag; }
389 qint32 objectId() const { return id; }
391
392private:
393 PoolList<Property> *properties;
394 PoolList<Alias> *aliases;
395 PoolList<Enum> *qmlEnums;
396 PoolList<Signal> *qmlSignals;
397 PoolList<Binding> *bindings;
398 PoolList<Function> *functions;
399 PoolList<InlineComponent> *inlineComponents;
400 PoolList<RequiredPropertyExtraData> *requiredPropertyExtraDatas;
401};
402
403struct Q_QML_COMPILER_EXPORT Pragma
404{
405 enum PragmaType
406 {
407 Singleton,
408 Strict,
409 ListPropertyAssignBehavior,
410 ComponentBehavior,
411 FunctionSignatureBehavior,
412 NativeMethodBehavior,
413 ValueTypeBehavior,
414 Translator,
415 };
416
417 enum ListPropertyAssignBehaviorValue
418 {
419 Append,
420 Replace,
421 ReplaceIfNotDefault,
422 };
423
424 enum ComponentBehaviorValue
425 {
426 Unbound,
427 Bound
428 };
429
430 enum FunctionSignatureBehaviorValue
431 {
432 Ignored,
433 Enforced
434 };
435
436 enum NativeMethodBehaviorValue
437 {
438 AcceptThisObject,
439 RejectThisObject
440 };
441
442 enum ValueTypeBehaviorValue
443 {
444 Copy = 0x1,
445 Addressable = 0x2,
446 Assertable = 0x4,
447 };
448 Q_DECLARE_FLAGS(ValueTypeBehaviorValues, ValueTypeBehaviorValue);
449
450 PragmaType type;
451
452 union {
453 ListPropertyAssignBehaviorValue listPropertyAssignBehavior;
454 ComponentBehaviorValue componentBehavior;
455 FunctionSignatureBehaviorValue functionSignatureBehavior;
456 NativeMethodBehaviorValue nativeMethodBehavior;
457 ValueTypeBehaviorValues::Int valueTypeBehavior;
458 uint translationContextIndex;
459 };
460
461 QV4::CompiledData::Location location;
462};
463
464struct Q_QML_COMPILER_EXPORT Document
465{
466 // disable it explicitly, it's implicitly deleted because of the Engine::_pool
467 Q_DISABLE_COPY_MOVE(Document)
468
469 Document(const QString &fileName, const QString &finalUrl, bool debugMode);
478
480
481 bool isSingleton() const {
482 return std::any_of(pragmas.constBegin(), pragmas.constEnd(), [](const Pragma *pragma) {
483 return pragma->type == Pragma::Singleton;
484 });
485 }
486
487 int registerString(const QString &str) { return jsGenerator.registerString(str); }
488 QString stringAt(int index) const { return jsGenerator.stringForIndex(index); }
489
490 int objectCount() const {return objects.size();}
491 Object* objectAt(int i) const {return objects.at(i);}
492};
493
494class Q_QML_COMPILER_EXPORT ScriptDirectivesCollector : public QQmlJS::Directives
495{
496 QmlIR::Document *document;
497 QQmlJS::Engine *engine;
498 QV4::Compiler::JSUnitGenerator *jsGenerator;
499
500public:
501 ScriptDirectivesCollector(QmlIR::Document *doc);
502
503 void pragmaLibrary() override;
504 void importFile(const QString &jsfile, const QString &module, int lineNumber, int column) override;
505 void importModule(const QString &uri, const QString &version, const QString &module, int lineNumber, int column) override;
506};
507
508struct Q_QML_COMPILER_EXPORT IRBuilder : public QQmlJS::AST::Visitor
509{
510 Q_DECLARE_TR_FUNCTIONS(QQmlCodeGenerator)
511public:
512 enum FailureMode {
513 StopAfterFirstFailure,
514 ContinueAfterFailure,
515 };
516
517 explicit IRBuilder(FailureMode failureMode = StopAfterFirstFailure);
518 bool generateFromQml(const QString &code, const QString &url, Document *output,
519 QV4::Compiler::CodegenWarningInterface *wInterface =
520 QV4::Compiler::defaultCodegenWarningInterface());
521
522 using QQmlJS::AST::Visitor::visit;
523 using QQmlJS::AST::Visitor::endVisit;
524
525 bool visit(QQmlJS::AST::UiImport *ast) override;
526 bool visit(QQmlJS::AST::UiPragma *ast) override;
527 bool visit(QQmlJS::AST::UiProgram *) override;
528 bool visit(QQmlJS::AST::UiArrayBinding *ast) override;
529 bool visit(QQmlJS::AST::UiObjectBinding *ast) override;
530 bool visit(QQmlJS::AST::UiObjectDefinition *ast) override;
531 bool visit(QQmlJS::AST::UiInlineComponent *ast) override;
532 bool visit(QQmlJS::AST::UiEnumDeclaration *ast) override;
533 bool visit(QQmlJS::AST::UiPublicMember *ast) override;
534 bool visit(QQmlJS::AST::UiScriptBinding *ast) override;
535 bool visit(QQmlJS::AST::UiSourceElement *ast) override;
536 bool visit(QQmlJS::AST::UiRequired *ast) override;
537
538 void throwRecursionDepthError() override
539 {
540 recordError(QQmlJS::SourceLocation(),
541 QStringLiteral("Maximum statement or expression depth exceeded"));
542 }
543
544 void accept(QQmlJS::AST::Node *node);
545
546 // returns index in _objects
547 bool defineQMLObject(
548 int *objectIndex, QQmlJS::AST::UiQualifiedId *qualifiedTypeNameId,
549 const QV4::CompiledData::Location &location,
550 QQmlJS::AST::UiObjectInitializer *initializer, Object *declarationsOverride = nullptr);
551
552 bool defineQMLObject(
553 int *objectIndex, QQmlJS::AST::UiObjectDefinition *node,
554 Object *declarationsOverride = nullptr)
555 {
556 const QQmlJS::SourceLocation location = node->qualifiedTypeNameId->firstSourceLocation();
557 return defineQMLObject(
558 objectIndex, node->qualifiedTypeNameId,
559 { location.startLine, location.startColumn }, node->initializer,
560 declarationsOverride);
561 }
562
563 static QString asString(QQmlJS::AST::UiQualifiedId *node);
564 QStringView asStringRef(QQmlJS::AST::Node *node);
565 static QTypeRevision extractVersion(QStringView string);
566 QStringView textRefAt(const QQmlJS::SourceLocation &loc) const
567 { return QStringView(sourceCode).mid(loc.offset, loc.length); }
568 QStringView textRefAt(const QQmlJS::SourceLocation &first,
569 const QQmlJS::SourceLocation &last) const;
570
571 virtual void setBindingValue(QV4::CompiledData::Binding *binding,
572 QQmlJS::AST::Statement *statement, QQmlJS::AST::Node *parentNode);
573 void tryGeneratingTranslationBinding(QStringView base, QQmlJS::AST::ArgumentList *args, QV4::CompiledData::Binding *binding);
574
575 void appendBinding(QQmlJS::AST::UiQualifiedId *name, QQmlJS::AST::Statement *value,
576 QQmlJS::AST::Node *parentNode);
577 void appendBinding(QQmlJS::AST::UiQualifiedId *name, int objectIndex, bool isOnAssignment = false);
578 void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation,
579 const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex,
580 QQmlJS::AST::Statement *value, QQmlJS::AST::Node *parentNode);
581 void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation,
582 const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex,
583 int objectIndex, bool isListItem = false, bool isOnAssignment = false);
584
585 bool appendAlias(QQmlJS::AST::UiPublicMember *node);
586
587 enum class IsQmlFunction { Yes, No };
588 virtual void registerFunctionExpr(QQmlJS::AST::FunctionExpression *fexp, IsQmlFunction);
589
590 Object *bindingsTarget() const;
591
592 bool setId(const QQmlJS::SourceLocation &idLocation, QQmlJS::AST::Statement *value);
593
594 // resolves qualified name (font.pixelSize for example) and returns the last name along
595 // with the object any right-hand-side of a binding should apply to.
596 bool resolveQualifiedId(QQmlJS::AST::UiQualifiedId **nameToResolve, Object **object, bool onAssignment = false);
597
598 void recordError(const QQmlJS::SourceLocation &location, const QString &description);
599 void recordError(const QQmlJS::SourceLocation &location, const QString &description,
600 QQmlJS::DiagnosticCategory category);
601 bool shouldContinue() const { return m_failureMode == ContinueAfterFailure; }
602
603 quint32 registerString(const QString &str) const { return jsGenerator->registerString(str); }
604 template <typename _Tp> _Tp *New() { return pool->New<_Tp>(); }
605
606 QString stringAt(int index) const { return jsGenerator->stringForIndex(index); }
607
608 static bool isStatementNodeScript(QQmlJS::AST::Statement *statement);
609 static bool isRedundantNullInitializerForPropertyDeclaration(Property *property, QQmlJS::AST::Statement *statement);
610
611 bool sanityCheckFunctionNames(Object *obj);
612
613 QList<QQmlJS::DiagnosticMessage> errors;
614
615 QSet<QString> inlineComponentsNames;
616
617 QList<const QV4::CompiledData::Import *> _imports;
618 QList<Pragma*> _pragmas;
619 QList<Object*> _objects;
620
621 QV4::CompiledData::TypeReferenceMap _typeReferences;
622
623 Object *_object;
624 Property *_propertyDeclaration;
625
626 QQmlJS::MemoryPool *pool;
627 QString sourceCode;
628 QV4::Compiler::JSUnitGenerator *jsGenerator;
629
630 bool insideInlineComponent = false;
631
632private:
633 FailureMode m_failureMode = StopAfterFirstFailure;
634};
635
636struct Q_QML_COMPILER_EXPORT QmlUnitGenerator
637{
638 void generate(Document &output, const QV4::CompiledData::DependentTypesHasher &dependencyHasher = QV4::CompiledData::DependentTypesHasher());
639
640private:
641 char *writeBindings(char *bindingPtr, const Object *o) const;
642};
643
644struct Q_QML_COMPILER_EXPORT JSCodeGen : public QV4::Compiler::Codegen
645{
646 JSCodeGen(Document *document,
647 QV4::Compiler::CodegenWarningInterface *iface =
648 QV4::Compiler::defaultCodegenWarningInterface(),
649 bool storeSourceLocations = false);
650
651 // Returns mapping from input functions to index in IR::Module::functions / compiledData->runtimeFunctions
652 QList<int>
653 generateJSCodeForFunctionsAndBindings(const QList<CompiledFunctionOrExpression> &functions);
654
655 bool generateRuntimeFunctions(QmlIR::Object *object);
656
657private:
658 Document *document;
659};
660
661// RegisterStringN ~= std::function<int(QStringView)>
662// FinalizeTranlationData ~= std::function<void(QV4::CompiledData::Binding::ValueType, QV4::CompiledData::TranslationData)>
663/*
664 \internal
665 \a base: name of the potential translation function
666 \a args: arguments to the function call
667 \a registerMainString: Takes the first argument passed to the translation function, and it's
668 result will be stored in a TranslationData's stringIndex for translation bindings and in numbeIndex
669 for string bindings.
670 \a registerCommentString: Takes the comment argument passed to some of the translation functions.
671 Result will be stored in a TranslationData's commentIndex
672 \a finalizeTranslationData: Takes the type of the binding and the previously set up TranslationData
673 */
674template<
675 typename RegisterMainString,
676 typename RegisterCommentString,
677 typename RegisterContextString,
678 typename FinalizeTranslationData>
679void tryGeneratingTranslationBindingBase(QStringView base, QQmlJS::AST::ArgumentList *args,
680 RegisterMainString registerMainString,
681 RegisterCommentString registerCommentString,
682 RegisterContextString registerContextString,
683 FinalizeTranslationData finalizeTranslationData
684 )
685{
686 if (base == QLatin1String("qsTr")) {
687 QV4::CompiledData::TranslationData translationData;
688 translationData.number = -1;
689
690 // empty string
691 translationData.commentIndex = 0;
692
693 // No context (not empty string)
694 translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex;
695
696 if (!args || !args->expression)
697 return; // no arguments, stop
698
699 QStringView translation;
700 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
701 translation = arg1->value;
702 } else {
703 return; // first argument is not a string, stop
704 }
705
706 translationData.stringIndex = registerMainString(translation);
707
708 args = args->next;
709
710 if (args) {
711 QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
712 if (!arg2)
713 return; // second argument is not a string, stop
714 translationData.commentIndex = registerCommentString(arg2->value);
715
716 args = args->next;
717 if (args) {
718 if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
719 translationData.number = int(arg3->value);
720 args = args->next;
721 } else {
722 return; // third argument is not a translation number, stop
723 }
724 }
725 }
726
727 if (args)
728 return; // too many arguments, stop
729
730 finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData);
731 } else if (base == QLatin1String("qsTrId")) {
732 QV4::CompiledData::TranslationData translationData;
733 translationData.number = -1;
734
735 // empty string, but unused
736 translationData.commentIndex = 0;
737
738 // No context (not empty string)
739 translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex;
740
741 if (!args || !args->expression)
742 return; // no arguments, stop
743
744 QStringView id;
745 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
746 id = arg1->value;
747 } else {
748 return; // first argument is not a string, stop
749 }
750 translationData.stringIndex = registerMainString(id);
751
752 args = args->next;
753
754 if (args) {
755 if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
756 translationData.number = int(arg3->value);
757 args = args->next;
758 } else {
759 return; // third argument is not a translation number, stop
760 }
761 }
762
763 if (args)
764 return; // too many arguments, stop
765
766 finalizeTranslationData(QV4::CompiledData::Binding::Type_TranslationById, translationData);
767 } else if (base == QLatin1String("QT_TR_NOOP") || base == QLatin1String("QT_TRID_NOOP")) {
768 if (!args || !args->expression)
769 return; // no arguments, stop
770
771 QStringView str;
772 if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
773 str = arg1->value;
774 } else {
775 return; // first argument is not a string, stop
776 }
777
778 args = args->next;
779 // QT_TR_NOOP can have a disambiguation string, QT_TRID_NOOP can't
780 if (args && base == QLatin1String("QT_TR_NOOP")) {
781 // we have a disambiguation string; we don't need to do anything with it
782 if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression))
783 args = args->next;
784 else // second argument is not a string, stop
785 return;
786 }
787
788 if (args)
789 return; // too many arguments, stop
790
791 QV4::CompiledData::TranslationData translationData;
792 translationData.number = registerMainString(str);
793 finalizeTranslationData(QV4::CompiledData::Binding::Type_String, translationData);
794 } else if (base == QLatin1String("QT_TRANSLATE_NOOP")) {
795 if (!args || !args->expression)
796 return; // no arguments, stop
797
798 args = args->next;
799 if (!args || !args->expression)
800 return; // no second arguments, stop
801
802 QStringView str;
803 if (QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
804 str = arg2->value;
805 } else {
806 return; // first argument is not a string, stop
807 }
808
809 args = args->next;
810 if (args) {
811 // we have a disambiguation string; we don't need to do anything with it
812 if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression))
813 args = args->next;
814 else // third argument is not a string, stop
815 return;
816 }
817
818 if (args)
819 return; // too many arguments, stop
820
821 QV4::CompiledData::TranslationData fakeTranslationData;
822 fakeTranslationData.number = registerMainString(str);
823 finalizeTranslationData(QV4::CompiledData::Binding::Type_String, fakeTranslationData);
824 } else if (base == QLatin1String("qsTranslate")) {
825 QV4::CompiledData::TranslationData translationData;
826 translationData.number = -1;
827 translationData.commentIndex = 0; // empty string
828
829 if (!args || !args->next)
830 return; // less than 2 arguments, stop
831
832 QStringView translation;
833 if (QQmlJS::AST::StringLiteral *arg1
834 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression)) {
835 translation = arg1->value;
836 } else {
837 return; // first argument is not a string, stop
838 }
839
840 translationData.contextIndex = registerContextString(translation);
841
842 args = args->next;
843 Q_ASSERT(args);
844
845 QQmlJS::AST::StringLiteral *arg2
846 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
847 if (!arg2)
848 return; // second argument is not a string, stop
849 translationData.stringIndex = registerMainString(arg2->value);
850
851 args = args->next;
852 if (args) {
853 QQmlJS::AST::StringLiteral *arg3
854 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(args->expression);
855 if (!arg3)
856 return; // third argument is not a string, stop
857 translationData.commentIndex = registerCommentString(arg3->value);
858
859 args = args->next;
860 if (args) {
861 if (QQmlJS::AST::NumericLiteral *arg4
862 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(args->expression)) {
863 translationData.number = int(arg4->value);
864 args = args->next;
865 } else {
866 return; // fourth argument is not a translation number, stop
867 }
868 }
869 }
870
871 if (args)
872 return; // too many arguments, stop
873
874 finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData);
875 }
876}
877
878} // namespace QmlIR
879
880QT_END_NAMESPACE
881
882#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)
#define COMPILE_EXCEPTION(...)
static const quint32 emptyStringIndex
static QStringList astNodeToStringList(QQmlJS::AST::Node *node)
#define EMIT_COMPILE_EXCEPTION(...)
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
QString appendProperty(Property *prop, const QString &propertyName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation, QQmlJS::DiagnosticCategory *errorCategory)
int functionCount() const
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