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 | #ifndef QQMLIRBUILDER_P_H |
4 | #define QQMLIRBUILDER_P_H |
5 | |
6 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <private/qqmljsast_p.h> |
18 | #include <private/qqmljsengine_p.h> |
19 | #include <private/qv4compiler_p.h> |
20 | #include <private/qv4compileddata_p.h> |
21 | #include <private/qqmljsmemorypool_p.h> |
22 | #include <private/qqmljsfixedpoolarray_p.h> |
23 | #include <private/qv4codegen_p.h> |
24 | #include <private/qv4compiler_p.h> |
25 | #include <QTextStream> |
26 | #include <QCoreApplication> |
27 | |
28 | QT_BEGIN_NAMESPACE |
29 | |
30 | class QQmlPropertyCache; |
31 | class QQmlContextData; |
32 | class QQmlTypeNameCache; |
33 | struct QQmlIRLoader; |
34 | |
35 | namespace QmlIR { |
36 | |
37 | struct Document; |
38 | |
39 | template <typename T> |
40 | struct PoolList |
41 | { |
42 | PoolList() |
43 | : first(nullptr) |
44 | , last(nullptr) |
45 | {} |
46 | |
47 | T *first; |
48 | T *last; |
49 | int count = 0; |
50 | |
51 | int append(T *item) { |
52 | item->next = nullptr; |
53 | if (last) |
54 | last->next = item; |
55 | else |
56 | first = item; |
57 | last = item; |
58 | return count++; |
59 | } |
60 | |
61 | void prepend(T *item) { |
62 | item->next = first; |
63 | first = item; |
64 | if (!last) |
65 | last = first; |
66 | ++count; |
67 | } |
68 | |
69 | template <typename Sortable, typename Base, Sortable Base::*sortMember> |
70 | T *findSortedInsertionPoint(T *item) const |
71 | { |
72 | T *insertPos = nullptr; |
73 | |
74 | for (T *it = first; it; it = it->next) { |
75 | if (!(it->*sortMember <= item->*sortMember)) |
76 | break; |
77 | insertPos = it; |
78 | } |
79 | |
80 | return insertPos; |
81 | } |
82 | |
83 | void insertAfter(T *insertionPoint, T *item) { |
84 | if (!insertionPoint) { |
85 | prepend(item); |
86 | } else if (insertionPoint == last) { |
87 | append(item); |
88 | } else { |
89 | item->next = insertionPoint->next; |
90 | insertionPoint->next = item; |
91 | ++count; |
92 | } |
93 | } |
94 | |
95 | T *unlink(T *before, T *item) { |
96 | T * const newNext = item->next; |
97 | |
98 | if (before) |
99 | before->next = newNext; |
100 | else |
101 | first = newNext; |
102 | |
103 | if (item == last) { |
104 | if (newNext) |
105 | last = newNext; |
106 | else |
107 | last = first; |
108 | } |
109 | |
110 | --count; |
111 | return newNext; |
112 | } |
113 | |
114 | T *slowAt(int index) const |
115 | { |
116 | T *result = first; |
117 | while (index > 0 && result) { |
118 | result = result->next; |
119 | --index; |
120 | } |
121 | return result; |
122 | } |
123 | |
124 | struct Iterator { |
125 | // turn Iterator into a proper iterator |
126 | using iterator_category = std::forward_iterator_tag; |
127 | using value_type = T; |
128 | using difference_type = ptrdiff_t; |
129 | using pointer = T *; |
130 | using reference = T &; |
131 | |
132 | T *ptr; |
133 | |
134 | explicit Iterator(T *p) : ptr(p) {} |
135 | |
136 | T *operator->() { |
137 | return ptr; |
138 | } |
139 | |
140 | const T *operator->() const { |
141 | return ptr; |
142 | } |
143 | |
144 | T &operator*() { |
145 | return *ptr; |
146 | } |
147 | |
148 | const T &operator*() const { |
149 | return *ptr; |
150 | } |
151 | |
152 | Iterator& operator++() { |
153 | ptr = ptr->next; |
154 | return *this; |
155 | } |
156 | |
157 | Iterator operator++(int) { |
158 | Iterator that {ptr}; |
159 | ptr = ptr->next; |
160 | return that; |
161 | } |
162 | |
163 | bool operator==(const Iterator &rhs) const { |
164 | return ptr == rhs.ptr; |
165 | } |
166 | |
167 | bool operator!=(const Iterator &rhs) const { |
168 | return ptr != rhs.ptr; |
169 | } |
170 | |
171 | operator T *() { return ptr; } |
172 | operator const T *() const { return ptr; } |
173 | }; |
174 | |
175 | Iterator begin() { return Iterator(first); } |
176 | Iterator end() { return Iterator(nullptr); } |
177 | |
178 | using iterator = Iterator; |
179 | }; |
180 | |
181 | struct Object; |
182 | |
183 | struct EnumValue : public QV4::CompiledData::EnumValue |
184 | { |
185 | EnumValue *next; |
186 | }; |
187 | |
188 | struct Enum |
189 | { |
190 | int nameIndex; |
191 | QV4::CompiledData::Location location; |
192 | PoolList<EnumValue> *enumValues; |
193 | |
194 | int enumValueCount() const { return enumValues->count; } |
195 | PoolList<EnumValue>::Iterator enumValuesBegin() const { return enumValues->begin(); } |
196 | PoolList<EnumValue>::Iterator enumValuesEnd() const { return enumValues->end(); } |
197 | |
198 | Enum *next; |
199 | }; |
200 | |
201 | |
202 | struct Parameter : public QV4::CompiledData::Parameter |
203 | { |
204 | Parameter *next; |
205 | |
206 | template<typename IdGenerator> |
207 | static bool initType( |
208 | QV4::CompiledData::ParameterType *type, const IdGenerator &idGenerator, |
209 | const QQmlJS::AST::Type *annotation) |
210 | { |
211 | using Flag = QV4::CompiledData::ParameterType::Flag; |
212 | |
213 | if (!annotation) |
214 | return initType(type, QString(), idGenerator(QString()), Flag::NoFlag); |
215 | |
216 | const QString typeId = annotation->typeId->toString(); |
217 | const QString typeArgument = |
218 | annotation->typeArgument ? annotation->typeArgument->toString() : QString(); |
219 | |
220 | if (typeArgument.isEmpty()) |
221 | return initType(type, typeId, idGenerator(typeId), Flag::NoFlag); |
222 | |
223 | if (typeId == QLatin1String("list" )) |
224 | return initType(type, typeArgument, idGenerator(typeArgument), Flag::List); |
225 | |
226 | const QString annotationString = annotation->toString(); |
227 | return initType(type, annotationString, idGenerator(annotationString), Flag::NoFlag); |
228 | } |
229 | |
230 | static QV4::CompiledData::CommonType stringToBuiltinType(const QString &typeName); |
231 | |
232 | private: |
233 | static bool initType( |
234 | QV4::CompiledData::ParameterType *paramType, const QString &typeName, |
235 | int typeNameIndex, QV4::CompiledData::ParameterType::Flag listFlag); |
236 | }; |
237 | |
238 | struct Signal |
239 | { |
240 | int nameIndex; |
241 | QV4::CompiledData::Location location; |
242 | PoolList<Parameter> *parameters; |
243 | |
244 | QStringList parameterStringList(const QV4::Compiler::StringTableGenerator *stringPool) const; |
245 | |
246 | int parameterCount() const { return parameters->count; } |
247 | PoolList<Parameter>::Iterator parametersBegin() const { return parameters->begin(); } |
248 | PoolList<Parameter>::Iterator parametersEnd() const { return parameters->end(); } |
249 | |
250 | Signal *next; |
251 | }; |
252 | |
253 | struct Property : public QV4::CompiledData::Property |
254 | { |
255 | Property *next; |
256 | }; |
257 | |
258 | struct Binding : public QV4::CompiledData::Binding |
259 | { |
260 | // The offset in the source file where the binding appeared. This is used for sorting to ensure |
261 | // that assignments to list properties are done in the correct order. We use the offset here instead |
262 | // of Binding::location as the latter has limited precision. |
263 | quint32 offset; |
264 | // Binding's compiledScriptIndex is index in object's functionsAndExpressions |
265 | Binding *next; |
266 | }; |
267 | |
268 | struct InlineComponent : public QV4::CompiledData::InlineComponent |
269 | { |
270 | InlineComponent *next; |
271 | }; |
272 | |
273 | struct Alias : public QV4::CompiledData::Alias |
274 | { |
275 | Alias *next; |
276 | }; |
277 | |
278 | struct : public QV4::CompiledData::RequiredPropertyExtraData |
279 | { |
280 | RequiredPropertyExtraData *; |
281 | }; |
282 | |
283 | struct Function |
284 | { |
285 | QV4::CompiledData::Location location; |
286 | int nameIndex; |
287 | quint32 index; // index in parsedQML::functions |
288 | QQmlJS::FixedPoolArray<Parameter> formals; |
289 | QV4::CompiledData::ParameterType returnType; |
290 | |
291 | // --- QQmlPropertyCacheCreator interface |
292 | const Parameter *formalsBegin() const { return formals.begin(); } |
293 | const Parameter *formalsEnd() const { return formals.end(); } |
294 | // --- |
295 | |
296 | Function *next; |
297 | }; |
298 | |
299 | struct Q_QML_COMPILER_PRIVATE_EXPORT CompiledFunctionOrExpression |
300 | { |
301 | CompiledFunctionOrExpression() |
302 | {} |
303 | |
304 | QQmlJS::AST::Node *parentNode = nullptr; // FunctionDeclaration, Statement or Expression |
305 | QQmlJS::AST::Node *node = nullptr; // FunctionDeclaration, Statement or Expression |
306 | quint32 nameIndex = 0; |
307 | CompiledFunctionOrExpression *next = nullptr; |
308 | }; |
309 | |
310 | struct Q_QML_COMPILER_PRIVATE_EXPORT Object |
311 | { |
312 | Q_DECLARE_TR_FUNCTIONS(Object) |
313 | public: |
314 | quint32 inheritedTypeNameIndex; |
315 | quint32 idNameIndex; |
316 | int id; |
317 | int indexOfDefaultPropertyOrAlias; |
318 | bool defaultPropertyIsAlias; |
319 | quint32 flags; |
320 | |
321 | QV4::CompiledData::Location location; |
322 | QV4::CompiledData::Location locationOfIdProperty; |
323 | |
324 | const Property *firstProperty() const { return properties->first; } |
325 | int propertyCount() const { return properties->count; } |
326 | Alias *firstAlias() const { return aliases->first; } |
327 | int aliasCount() const { return aliases->count; } |
328 | const Enum *firstEnum() const { return qmlEnums->first; } |
329 | int enumCount() const { return qmlEnums->count; } |
330 | const Signal *firstSignal() const { return qmlSignals->first; } |
331 | int signalCount() const { return qmlSignals->count; } |
332 | Binding *firstBinding() const { return bindings->first; } |
333 | int bindingCount() const { return bindings->count; } |
334 | const Function *firstFunction() const { return functions->first; } |
335 | int functionCount() const { return functions->count; } |
336 | const InlineComponent *inlineComponent() const { return inlineComponents->first; } |
337 | int inlineComponentCount() const { return inlineComponents->count; } |
338 | const RequiredPropertyExtraData *() const {return requiredPropertyExtraDatas->first; } |
339 | int () const { return requiredPropertyExtraDatas->count; } |
340 | void simplifyRequiredProperties(); |
341 | |
342 | PoolList<Binding>::Iterator bindingsBegin() const { return bindings->begin(); } |
343 | PoolList<Binding>::Iterator bindingsEnd() const { return bindings->end(); } |
344 | PoolList<Property>::Iterator propertiesBegin() const { return properties->begin(); } |
345 | PoolList<Property>::Iterator propertiesEnd() const { return properties->end(); } |
346 | PoolList<Alias>::Iterator aliasesBegin() const { return aliases->begin(); } |
347 | PoolList<Alias>::Iterator aliasesEnd() const { return aliases->end(); } |
348 | PoolList<Enum>::Iterator enumsBegin() const { return qmlEnums->begin(); } |
349 | PoolList<Enum>::Iterator enumsEnd() const { return qmlEnums->end(); } |
350 | PoolList<Signal>::Iterator signalsBegin() const { return qmlSignals->begin(); } |
351 | PoolList<Signal>::Iterator signalsEnd() const { return qmlSignals->end(); } |
352 | PoolList<Function>::Iterator functionsBegin() const { return functions->begin(); } |
353 | PoolList<Function>::Iterator functionsEnd() const { return functions->end(); } |
354 | PoolList<InlineComponent>::Iterator inlineComponentsBegin() const { return inlineComponents->begin(); } |
355 | PoolList<InlineComponent>::Iterator inlineComponentsEnd() const { return inlineComponents->end(); } |
356 | PoolList<RequiredPropertyExtraData>::Iterator () const {return requiredPropertyExtraDatas->begin(); } |
357 | PoolList<RequiredPropertyExtraData>::Iterator () const {return requiredPropertyExtraDatas->end(); } |
358 | |
359 | // If set, then declarations for this object (and init bindings for these) should go into the |
360 | // specified object. Used for declarations inside group properties. |
361 | Object *declarationsOverride; |
362 | |
363 | void init(QQmlJS::MemoryPool *pool, int typeNameIndex, int idIndex, const QV4::CompiledData::Location &location); |
364 | |
365 | QString appendEnum(Enum *enumeration); |
366 | QString appendSignal(Signal *signal); |
367 | QString appendProperty(Property *prop, const QString &propertyName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation); |
368 | QString appendAlias(Alias *prop, const QString &aliasName, bool isDefaultProperty, const QQmlJS::SourceLocation &defaultToken, QQmlJS::SourceLocation *errorLocation); |
369 | void appendFunction(QmlIR::Function *f); |
370 | void appendInlineComponent(InlineComponent *ic); |
371 | void (RequiredPropertyExtraData *); |
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, item: binding); } |
376 | void insertSorted(Binding *b); |
377 | QString bindingAsString(Document *doc, int scriptIndex) const; |
378 | |
379 | PoolList<CompiledFunctionOrExpression> *functionsAndExpressions; |
380 | QQmlJS::FixedPoolArray<int> runtimeFunctionIndices; |
381 | |
382 | QQmlJS::FixedPoolArray<quint32> namedObjectsInComponent; |
383 | int namedObjectsInComponentCount() const { return namedObjectsInComponent.size(); } |
384 | const quint32 *namedObjectsInComponentTable() const { return namedObjectsInComponent.begin(); } |
385 | |
386 | bool hasFlag(QV4::CompiledData::Object::Flag flag) const { return flags & flag; } |
387 | qint32 objectId() const { return id; } |
388 | bool hasAliasAsDefaultProperty() const { return defaultPropertyIsAlias; } |
389 | |
390 | private: |
391 | friend struct ::QQmlIRLoader; |
392 | |
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> *; |
401 | }; |
402 | |
403 | struct Q_QML_COMPILER_PRIVATE_EXPORT Pragma |
404 | { |
405 | enum PragmaType |
406 | { |
407 | Singleton, |
408 | Strict, |
409 | ListPropertyAssignBehavior, |
410 | ComponentBehavior, |
411 | FunctionSignatureBehavior, |
412 | NativeMethodBehavior, |
413 | ValueTypeBehavior, |
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 | }; |
446 | Q_DECLARE_FLAGS(ValueTypeBehaviorValues, ValueTypeBehaviorValue); |
447 | |
448 | PragmaType type; |
449 | |
450 | union { |
451 | ListPropertyAssignBehaviorValue listPropertyAssignBehavior; |
452 | ComponentBehaviorValue componentBehavior; |
453 | FunctionSignatureBehaviorValue functionSignatureBehavior; |
454 | NativeMethodBehaviorValue nativeMethodBehavior; |
455 | ValueTypeBehaviorValues::Int valueTypeBehavior; |
456 | }; |
457 | |
458 | QV4::CompiledData::Location location; |
459 | }; |
460 | |
461 | struct Q_QML_COMPILER_PRIVATE_EXPORT Document |
462 | { |
463 | Document(bool debugMode); |
464 | QString code; |
465 | QQmlJS::Engine jsParserEngine; |
466 | QV4::Compiler::Module jsModule; |
467 | QList<const QV4::CompiledData::Import *> imports; |
468 | QList<Pragma*> pragmas; |
469 | QQmlJS::AST::UiProgram *program; |
470 | QVector<Object*> objects; |
471 | QV4::Compiler::JSUnitGenerator jsGenerator; |
472 | |
473 | QV4::CompiledData::CompilationUnit javaScriptCompilationUnit; |
474 | |
475 | int registerString(const QString &str) { return jsGenerator.registerString(str); } |
476 | QString stringAt(int index) const { return jsGenerator.stringForIndex(index); } |
477 | |
478 | int objectCount() const {return objects.size();} |
479 | Object* objectAt(int i) const {return objects.at(i);} |
480 | }; |
481 | |
482 | class Q_QML_COMPILER_PRIVATE_EXPORT ScriptDirectivesCollector : public QQmlJS::Directives |
483 | { |
484 | QmlIR::Document *document; |
485 | QQmlJS::Engine *engine; |
486 | QV4::Compiler::JSUnitGenerator *jsGenerator; |
487 | |
488 | public: |
489 | ScriptDirectivesCollector(QmlIR::Document *doc); |
490 | |
491 | void pragmaLibrary() override; |
492 | void importFile(const QString &jsfile, const QString &module, int lineNumber, int column) override; |
493 | void importModule(const QString &uri, const QString &version, const QString &module, int lineNumber, int column) override; |
494 | }; |
495 | |
496 | struct Q_QML_COMPILER_PRIVATE_EXPORT IRBuilder : public QQmlJS::AST::Visitor |
497 | { |
498 | Q_DECLARE_TR_FUNCTIONS(QQmlCodeGenerator) |
499 | public: |
500 | IRBuilder(const QSet<QString> &illegalNames); |
501 | bool generateFromQml(const QString &code, const QString &url, Document *output); |
502 | |
503 | static bool isSignalPropertyName(const QString &name); |
504 | static QString signalNameFromSignalPropertyName(const QString &signalPropertyName); |
505 | |
506 | using QQmlJS::AST::Visitor::visit; |
507 | using QQmlJS::AST::Visitor::endVisit; |
508 | |
509 | bool visit(QQmlJS::AST::UiArrayMemberList *ast) override; |
510 | bool visit(QQmlJS::AST::UiImport *ast) override; |
511 | bool visit(QQmlJS::AST::UiPragma *ast) override; |
512 | bool (QQmlJS::AST::UiHeaderItemList *ast) override; |
513 | bool visit(QQmlJS::AST::UiObjectInitializer *ast) override; |
514 | bool visit(QQmlJS::AST::UiObjectMemberList *ast) override; |
515 | bool visit(QQmlJS::AST::UiParameterList *ast) override; |
516 | bool visit(QQmlJS::AST::UiProgram *) override; |
517 | bool visit(QQmlJS::AST::UiQualifiedId *ast) override; |
518 | bool visit(QQmlJS::AST::UiArrayBinding *ast) override; |
519 | bool visit(QQmlJS::AST::UiObjectBinding *ast) override; |
520 | bool visit(QQmlJS::AST::UiObjectDefinition *ast) override; |
521 | bool visit(QQmlJS::AST::UiInlineComponent *ast) override; |
522 | bool visit(QQmlJS::AST::UiEnumDeclaration *ast) override; |
523 | bool visit(QQmlJS::AST::UiPublicMember *ast) override; |
524 | bool visit(QQmlJS::AST::UiScriptBinding *ast) override; |
525 | bool visit(QQmlJS::AST::UiSourceElement *ast) override; |
526 | bool visit(QQmlJS::AST::UiRequired *ast) override; |
527 | |
528 | void throwRecursionDepthError() override |
529 | { |
530 | recordError(location: QQmlJS::SourceLocation(), |
531 | QStringLiteral("Maximum statement or expression depth exceeded" )); |
532 | } |
533 | |
534 | void accept(QQmlJS::AST::Node *node); |
535 | |
536 | // returns index in _objects |
537 | bool defineQMLObject( |
538 | int *objectIndex, QQmlJS::AST::UiQualifiedId *qualifiedTypeNameId, |
539 | const QV4::CompiledData::Location &location, |
540 | QQmlJS::AST::UiObjectInitializer *initializer, Object *declarationsOverride = nullptr); |
541 | |
542 | bool defineQMLObject( |
543 | int *objectIndex, QQmlJS::AST::UiObjectDefinition *node, |
544 | Object *declarationsOverride = nullptr) |
545 | { |
546 | const QQmlJS::SourceLocation location = node->qualifiedTypeNameId->firstSourceLocation(); |
547 | return defineQMLObject( |
548 | objectIndex, qualifiedTypeNameId: node->qualifiedTypeNameId, |
549 | location: { location.startLine, location.startColumn }, initializer: node->initializer, |
550 | declarationsOverride); |
551 | } |
552 | |
553 | static QString asString(QQmlJS::AST::UiQualifiedId *node); |
554 | QStringView asStringRef(QQmlJS::AST::Node *node); |
555 | static QTypeRevision (QStringView string); |
556 | QStringView textRefAt(const QQmlJS::SourceLocation &loc) const |
557 | { return QStringView(sourceCode).mid(pos: loc.offset, n: loc.length); } |
558 | QStringView textRefAt(const QQmlJS::SourceLocation &first, |
559 | const QQmlJS::SourceLocation &last) const; |
560 | |
561 | void setBindingValue(QV4::CompiledData::Binding *binding, QQmlJS::AST::Statement *statement, |
562 | QQmlJS::AST::Node *parentNode); |
563 | void tryGeneratingTranslationBinding(QStringView base, QQmlJS::AST::ArgumentList *args, QV4::CompiledData::Binding *binding); |
564 | |
565 | void appendBinding(QQmlJS::AST::UiQualifiedId *name, QQmlJS::AST::Statement *value, |
566 | QQmlJS::AST::Node *parentNode); |
567 | void appendBinding(QQmlJS::AST::UiQualifiedId *name, int objectIndex, bool isOnAssignment = false); |
568 | void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation, |
569 | const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex, |
570 | QQmlJS::AST::Statement *value, QQmlJS::AST::Node *parentNode); |
571 | void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation, |
572 | const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex, |
573 | int objectIndex, bool isListItem = false, bool isOnAssignment = false); |
574 | |
575 | bool appendAlias(QQmlJS::AST::UiPublicMember *node); |
576 | |
577 | Object *bindingsTarget() const; |
578 | |
579 | bool setId(const QQmlJS::SourceLocation &idLocation, QQmlJS::AST::Statement *value); |
580 | |
581 | // resolves qualified name (font.pixelSize for example) and returns the last name along |
582 | // with the object any right-hand-side of a binding should apply to. |
583 | bool resolveQualifiedId(QQmlJS::AST::UiQualifiedId **nameToResolve, Object **object, bool onAssignment = false); |
584 | |
585 | void recordError(const QQmlJS::SourceLocation &location, const QString &description); |
586 | |
587 | quint32 registerString(const QString &str) const { return jsGenerator->registerString(str); } |
588 | template <typename _Tp> _Tp *New() { return pool->New<_Tp>(); } |
589 | |
590 | QString stringAt(int index) const { return jsGenerator->stringForIndex(index); } |
591 | |
592 | static bool isStatementNodeScript(QQmlJS::AST::Statement *statement); |
593 | static bool isRedundantNullInitializerForPropertyDeclaration(Property *property, QQmlJS::AST::Statement *statement); |
594 | |
595 | QString sanityCheckFunctionNames(Object *obj, const QSet<QString> &illegalNames, QQmlJS::SourceLocation *errorLocation); |
596 | |
597 | QList<QQmlJS::DiagnosticMessage> errors; |
598 | |
599 | QSet<QString> illegalNames; |
600 | QSet<QString> inlineComponentsNames; |
601 | |
602 | QList<const QV4::CompiledData::Import *> _imports; |
603 | QList<Pragma*> _pragmas; |
604 | QVector<Object*> _objects; |
605 | |
606 | QV4::CompiledData::TypeReferenceMap _typeReferences; |
607 | |
608 | Object *_object; |
609 | Property *_propertyDeclaration; |
610 | |
611 | QQmlJS::MemoryPool *pool; |
612 | QString sourceCode; |
613 | QV4::Compiler::JSUnitGenerator *jsGenerator; |
614 | |
615 | bool insideInlineComponent = false; |
616 | }; |
617 | |
618 | struct Q_QML_COMPILER_PRIVATE_EXPORT QmlUnitGenerator |
619 | { |
620 | void generate(Document &output, const QV4::CompiledData::DependentTypesHasher &dependencyHasher = QV4::CompiledData::DependentTypesHasher()); |
621 | |
622 | private: |
623 | typedef bool (Binding::*BindingFilter)() const; |
624 | char *writeBindings(char *bindingPtr, const Object *o, BindingFilter filter) const; |
625 | }; |
626 | |
627 | struct Q_QML_COMPILER_PRIVATE_EXPORT JSCodeGen : public QV4::Compiler::Codegen |
628 | { |
629 | JSCodeGen(Document *document, const QSet<QString> &globalNames, |
630 | QV4::Compiler::CodegenWarningInterface *iface = |
631 | QV4::Compiler::defaultCodegenWarningInterface(), |
632 | bool storeSourceLocations = false); |
633 | |
634 | // Returns mapping from input functions to index in IR::Module::functions / compiledData->runtimeFunctions |
635 | QVector<int> |
636 | generateJSCodeForFunctionsAndBindings(const QList<CompiledFunctionOrExpression> &functions); |
637 | |
638 | bool generateRuntimeFunctions(QmlIR::Object *object); |
639 | |
640 | private: |
641 | Document *document; |
642 | }; |
643 | |
644 | // RegisterStringN ~= std::function<int(QStringView)> |
645 | // FinalizeTranlationData ~= std::function<void(QV4::CompiledData::Binding::ValueType, QV4::CompiledData::TranslationData)> |
646 | /* |
647 | \internal |
648 | \a base: name of the potential translation function |
649 | \a args: arguments to the function call |
650 | \a registerMainString: Takes the first argument passed to the translation function, and it's |
651 | result will be stored in a TranslationData's stringIndex for translation bindings and in numbeIndex |
652 | for string bindings. |
653 | \a registerCommentString: Takes the comment argument passed to some of the translation functions. |
654 | Result will be stored in a TranslationData's commentIndex |
655 | \a finalizeTranslationData: Takes the type of the binding and the previously set up TranslationData |
656 | */ |
657 | template< |
658 | typename RegisterMainString, |
659 | typename RegisterCommentString, |
660 | typename RegisterContextString, |
661 | typename FinalizeTranslationData> |
662 | void tryGeneratingTranslationBindingBase(QStringView base, QQmlJS::AST::ArgumentList *args, |
663 | RegisterMainString registerMainString, |
664 | RegisterCommentString , |
665 | RegisterContextString registerContextString, |
666 | FinalizeTranslationData finalizeTranslationData |
667 | ) |
668 | { |
669 | if (base == QLatin1String("qsTr" )) { |
670 | QV4::CompiledData::TranslationData translationData; |
671 | translationData.number = -1; |
672 | translationData.commentIndex = 0; // empty string |
673 | translationData.contextIndex = 0; |
674 | |
675 | if (!args || !args->expression) |
676 | return; // no arguments, stop |
677 | |
678 | QStringView translation; |
679 | if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
680 | translation = arg1->value; |
681 | } else { |
682 | return; // first argument is not a string, stop |
683 | } |
684 | |
685 | translationData.stringIndex = registerMainString(translation); |
686 | |
687 | args = args->next; |
688 | |
689 | if (args) { |
690 | QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression); |
691 | if (!arg2) |
692 | return; // second argument is not a string, stop |
693 | translationData.commentIndex = registerCommentString(arg2->value); |
694 | |
695 | args = args->next; |
696 | if (args) { |
697 | if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(ast: args->expression)) { |
698 | translationData.number = int(arg3->value); |
699 | args = args->next; |
700 | } else { |
701 | return; // third argument is not a translation number, stop |
702 | } |
703 | } |
704 | } |
705 | |
706 | if (args) |
707 | return; // too many arguments, stop |
708 | |
709 | finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData); |
710 | } else if (base == QLatin1String("qsTrId" )) { |
711 | QV4::CompiledData::TranslationData translationData; |
712 | translationData.number = -1; |
713 | translationData.commentIndex = 0; // empty string, but unused |
714 | translationData.contextIndex = 0; |
715 | |
716 | if (!args || !args->expression) |
717 | return; // no arguments, stop |
718 | |
719 | QStringView id; |
720 | if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
721 | id = arg1->value; |
722 | } else { |
723 | return; // first argument is not a string, stop |
724 | } |
725 | translationData.stringIndex = registerMainString(id); |
726 | |
727 | args = args->next; |
728 | |
729 | if (args) { |
730 | if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(ast: args->expression)) { |
731 | translationData.number = int(arg3->value); |
732 | args = args->next; |
733 | } else { |
734 | return; // third argument is not a translation number, stop |
735 | } |
736 | } |
737 | |
738 | if (args) |
739 | return; // too many arguments, stop |
740 | |
741 | finalizeTranslationData(QV4::CompiledData::Binding::Type_TranslationById, translationData); |
742 | } else if (base == QLatin1String("QT_TR_NOOP" ) || base == QLatin1String("QT_TRID_NOOP" )) { |
743 | if (!args || !args->expression) |
744 | return; // no arguments, stop |
745 | |
746 | QStringView str; |
747 | if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
748 | str = arg1->value; |
749 | } else { |
750 | return; // first argument is not a string, stop |
751 | } |
752 | |
753 | args = args->next; |
754 | if (args) |
755 | return; // too many arguments, stop |
756 | |
757 | QV4::CompiledData::TranslationData translationData; |
758 | translationData.number = registerMainString(str); |
759 | finalizeTranslationData(QV4::CompiledData::Binding::Type_String, translationData); |
760 | } else if (base == QLatin1String("QT_TRANSLATE_NOOP" )) { |
761 | if (!args || !args->expression) |
762 | return; // no arguments, stop |
763 | |
764 | args = args->next; |
765 | if (!args || !args->expression) |
766 | return; // no second arguments, stop |
767 | |
768 | QStringView str; |
769 | if (QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
770 | str = arg2->value; |
771 | } else { |
772 | return; // first argument is not a string, stop |
773 | } |
774 | |
775 | args = args->next; |
776 | if (args) |
777 | return; // too many arguments, stop |
778 | |
779 | QV4::CompiledData::TranslationData fakeTranslationData; |
780 | fakeTranslationData.number = registerMainString(str); |
781 | finalizeTranslationData(QV4::CompiledData::Binding::Type_String, fakeTranslationData); |
782 | } else if (base == QLatin1String("qsTranslate" )) { |
783 | QV4::CompiledData::TranslationData translationData; |
784 | translationData.number = -1; |
785 | translationData.commentIndex = 0; // empty string |
786 | |
787 | if (!args || !args->next) |
788 | return; // less than 2 arguments, stop |
789 | |
790 | QStringView translation; |
791 | if (QQmlJS::AST::StringLiteral *arg1 |
792 | = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
793 | translation = arg1->value; |
794 | } else { |
795 | return; // first argument is not a string, stop |
796 | } |
797 | |
798 | translationData.contextIndex = registerContextString(translation); |
799 | |
800 | args = args->next; |
801 | Q_ASSERT(args); |
802 | |
803 | QQmlJS::AST::StringLiteral *arg2 |
804 | = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression); |
805 | if (!arg2) |
806 | return; // second argument is not a string, stop |
807 | translationData.stringIndex = registerMainString(arg2->value); |
808 | |
809 | args = args->next; |
810 | if (args) { |
811 | QQmlJS::AST::StringLiteral *arg3 |
812 | = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression); |
813 | if (!arg3) |
814 | return; // third argument is not a string, stop |
815 | translationData.commentIndex = registerCommentString(arg3->value); |
816 | |
817 | args = args->next; |
818 | if (args) { |
819 | if (QQmlJS::AST::NumericLiteral *arg4 |
820 | = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(ast: args->expression)) { |
821 | translationData.number = int(arg4->value); |
822 | args = args->next; |
823 | } else { |
824 | return; // fourth argument is not a translation number, stop |
825 | } |
826 | } |
827 | } |
828 | |
829 | if (args) |
830 | return; // too many arguments, stop |
831 | |
832 | finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData); |
833 | } |
834 | } |
835 | |
836 | } // namespace QmlIR |
837 | |
838 | QT_END_NAMESPACE |
839 | |
840 | #endif // QQMLIRBUILDER_P_H |
841 | |