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