| 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_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_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_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 | |
| 464 | struct Q_QML_COMPILER_EXPORT Document |
| 465 | { |
| 466 | Document(const QString &fileName, const QString &finalUrl, bool debugMode); |
| 467 | QString code; |
| 468 | QQmlJS::Engine jsParserEngine; |
| 469 | QV4::Compiler::Module jsModule; |
| 470 | QList<const QV4::CompiledData::Import *> imports; |
| 471 | QList<Pragma*> pragmas; |
| 472 | QQmlJS::AST::UiProgram *program; |
| 473 | QVector<Object*> objects; |
| 474 | QV4::Compiler::JSUnitGenerator jsGenerator; |
| 475 | |
| 476 | QQmlRefPointer<QV4::CompiledData::CompilationUnit> javaScriptCompilationUnit; |
| 477 | |
| 478 | bool isSingleton() const { |
| 479 | return std::any_of(first: pragmas.constBegin(), last: pragmas.constEnd(), pred: [](const Pragma *pragma) { |
| 480 | return pragma->type == Pragma::Singleton; |
| 481 | }); |
| 482 | } |
| 483 | |
| 484 | int registerString(const QString &str) { return jsGenerator.registerString(str); } |
| 485 | QString stringAt(int index) const { return jsGenerator.stringForIndex(index); } |
| 486 | |
| 487 | int objectCount() const {return objects.size();} |
| 488 | Object* objectAt(int i) const {return objects.at(i);} |
| 489 | }; |
| 490 | |
| 491 | class Q_QML_COMPILER_EXPORT ScriptDirectivesCollector : public QQmlJS::Directives |
| 492 | { |
| 493 | QmlIR::Document *document; |
| 494 | QQmlJS::Engine *engine; |
| 495 | QV4::Compiler::JSUnitGenerator *jsGenerator; |
| 496 | |
| 497 | public: |
| 498 | ScriptDirectivesCollector(QmlIR::Document *doc); |
| 499 | |
| 500 | void pragmaLibrary() override; |
| 501 | void importFile(const QString &jsfile, const QString &module, int lineNumber, int column) override; |
| 502 | void importModule(const QString &uri, const QString &version, const QString &module, int lineNumber, int column) override; |
| 503 | }; |
| 504 | |
| 505 | struct Q_QML_COMPILER_EXPORT IRBuilder : public QQmlJS::AST::Visitor |
| 506 | { |
| 507 | Q_DECLARE_TR_FUNCTIONS(QQmlCodeGenerator) |
| 508 | public: |
| 509 | IRBuilder(const QSet<QString> &illegalNames); |
| 510 | bool generateFromQml(const QString &code, const QString &url, Document *output); |
| 511 | |
| 512 | using QQmlJS::AST::Visitor::visit; |
| 513 | using QQmlJS::AST::Visitor::endVisit; |
| 514 | |
| 515 | bool visit(QQmlJS::AST::UiArrayMemberList *ast) override; |
| 516 | bool visit(QQmlJS::AST::UiImport *ast) override; |
| 517 | bool visit(QQmlJS::AST::UiPragma *ast) override; |
| 518 | bool (QQmlJS::AST::UiHeaderItemList *ast) override; |
| 519 | bool visit(QQmlJS::AST::UiObjectInitializer *ast) override; |
| 520 | bool visit(QQmlJS::AST::UiObjectMemberList *ast) override; |
| 521 | bool visit(QQmlJS::AST::UiParameterList *ast) override; |
| 522 | bool visit(QQmlJS::AST::UiProgram *) override; |
| 523 | bool visit(QQmlJS::AST::UiQualifiedId *ast) override; |
| 524 | bool visit(QQmlJS::AST::UiArrayBinding *ast) override; |
| 525 | bool visit(QQmlJS::AST::UiObjectBinding *ast) override; |
| 526 | bool visit(QQmlJS::AST::UiObjectDefinition *ast) override; |
| 527 | bool visit(QQmlJS::AST::UiInlineComponent *ast) override; |
| 528 | bool visit(QQmlJS::AST::UiEnumDeclaration *ast) override; |
| 529 | bool visit(QQmlJS::AST::UiPublicMember *ast) override; |
| 530 | bool visit(QQmlJS::AST::UiScriptBinding *ast) override; |
| 531 | bool visit(QQmlJS::AST::UiSourceElement *ast) override; |
| 532 | bool visit(QQmlJS::AST::UiRequired *ast) override; |
| 533 | |
| 534 | void throwRecursionDepthError() override |
| 535 | { |
| 536 | recordError(location: QQmlJS::SourceLocation(), |
| 537 | QStringLiteral("Maximum statement or expression depth exceeded" )); |
| 538 | } |
| 539 | |
| 540 | void accept(QQmlJS::AST::Node *node); |
| 541 | |
| 542 | // returns index in _objects |
| 543 | bool defineQMLObject( |
| 544 | int *objectIndex, QQmlJS::AST::UiQualifiedId *qualifiedTypeNameId, |
| 545 | const QV4::CompiledData::Location &location, |
| 546 | QQmlJS::AST::UiObjectInitializer *initializer, Object *declarationsOverride = nullptr); |
| 547 | |
| 548 | bool defineQMLObject( |
| 549 | int *objectIndex, QQmlJS::AST::UiObjectDefinition *node, |
| 550 | Object *declarationsOverride = nullptr) |
| 551 | { |
| 552 | const QQmlJS::SourceLocation location = node->qualifiedTypeNameId->firstSourceLocation(); |
| 553 | return defineQMLObject( |
| 554 | objectIndex, qualifiedTypeNameId: node->qualifiedTypeNameId, |
| 555 | location: { location.startLine, location.startColumn }, initializer: node->initializer, |
| 556 | declarationsOverride); |
| 557 | } |
| 558 | |
| 559 | static QString asString(QQmlJS::AST::UiQualifiedId *node); |
| 560 | QStringView asStringRef(QQmlJS::AST::Node *node); |
| 561 | static QTypeRevision (QStringView string); |
| 562 | QStringView textRefAt(const QQmlJS::SourceLocation &loc) const |
| 563 | { return QStringView(sourceCode).mid(pos: loc.offset, n: loc.length); } |
| 564 | QStringView textRefAt(const QQmlJS::SourceLocation &first, |
| 565 | const QQmlJS::SourceLocation &last) const; |
| 566 | |
| 567 | void setBindingValue(QV4::CompiledData::Binding *binding, QQmlJS::AST::Statement *statement, |
| 568 | QQmlJS::AST::Node *parentNode); |
| 569 | void tryGeneratingTranslationBinding(QStringView base, QQmlJS::AST::ArgumentList *args, QV4::CompiledData::Binding *binding); |
| 570 | |
| 571 | void appendBinding(QQmlJS::AST::UiQualifiedId *name, QQmlJS::AST::Statement *value, |
| 572 | QQmlJS::AST::Node *parentNode); |
| 573 | void appendBinding(QQmlJS::AST::UiQualifiedId *name, int objectIndex, bool isOnAssignment = false); |
| 574 | void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation, |
| 575 | const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex, |
| 576 | QQmlJS::AST::Statement *value, QQmlJS::AST::Node *parentNode); |
| 577 | void appendBinding(const QQmlJS::SourceLocation &qualifiedNameLocation, |
| 578 | const QQmlJS::SourceLocation &nameLocation, quint32 propertyNameIndex, |
| 579 | int objectIndex, bool isListItem = false, bool isOnAssignment = false); |
| 580 | |
| 581 | bool appendAlias(QQmlJS::AST::UiPublicMember *node); |
| 582 | |
| 583 | Object *bindingsTarget() const; |
| 584 | |
| 585 | bool setId(const QQmlJS::SourceLocation &idLocation, QQmlJS::AST::Statement *value); |
| 586 | |
| 587 | // resolves qualified name (font.pixelSize for example) and returns the last name along |
| 588 | // with the object any right-hand-side of a binding should apply to. |
| 589 | bool resolveQualifiedId(QQmlJS::AST::UiQualifiedId **nameToResolve, Object **object, bool onAssignment = false); |
| 590 | |
| 591 | void recordError(const QQmlJS::SourceLocation &location, const QString &description); |
| 592 | |
| 593 | quint32 registerString(const QString &str) const { return jsGenerator->registerString(str); } |
| 594 | template <typename _Tp> _Tp *New() { return pool->New<_Tp>(); } |
| 595 | |
| 596 | QString stringAt(int index) const { return jsGenerator->stringForIndex(index); } |
| 597 | |
| 598 | static bool isStatementNodeScript(QQmlJS::AST::Statement *statement); |
| 599 | static bool isRedundantNullInitializerForPropertyDeclaration(Property *property, QQmlJS::AST::Statement *statement); |
| 600 | |
| 601 | QString sanityCheckFunctionNames(Object *obj, const QSet<QString> &illegalNames, QQmlJS::SourceLocation *errorLocation); |
| 602 | |
| 603 | QList<QQmlJS::DiagnosticMessage> errors; |
| 604 | |
| 605 | QSet<QString> illegalNames; |
| 606 | QSet<QString> inlineComponentsNames; |
| 607 | |
| 608 | QList<const QV4::CompiledData::Import *> _imports; |
| 609 | QList<Pragma*> _pragmas; |
| 610 | QVector<Object*> _objects; |
| 611 | |
| 612 | QV4::CompiledData::TypeReferenceMap _typeReferences; |
| 613 | |
| 614 | Object *_object; |
| 615 | Property *_propertyDeclaration; |
| 616 | |
| 617 | QQmlJS::MemoryPool *pool; |
| 618 | QString sourceCode; |
| 619 | QV4::Compiler::JSUnitGenerator *jsGenerator; |
| 620 | |
| 621 | bool insideInlineComponent = false; |
| 622 | }; |
| 623 | |
| 624 | struct Q_QML_COMPILER_EXPORT QmlUnitGenerator |
| 625 | { |
| 626 | void generate(Document &output, const QV4::CompiledData::DependentTypesHasher &dependencyHasher = QV4::CompiledData::DependentTypesHasher()); |
| 627 | |
| 628 | private: |
| 629 | typedef bool (Binding::*BindingFilter)() const; |
| 630 | char *writeBindings(char *bindingPtr, const Object *o, BindingFilter filter) const; |
| 631 | }; |
| 632 | |
| 633 | struct Q_QML_COMPILER_EXPORT JSCodeGen : public QV4::Compiler::Codegen |
| 634 | { |
| 635 | JSCodeGen(Document *document, const QSet<QString> &globalNames, |
| 636 | QV4::Compiler::CodegenWarningInterface *iface = |
| 637 | QV4::Compiler::defaultCodegenWarningInterface(), |
| 638 | bool storeSourceLocations = false); |
| 639 | |
| 640 | // Returns mapping from input functions to index in IR::Module::functions / compiledData->runtimeFunctions |
| 641 | QVector<int> |
| 642 | generateJSCodeForFunctionsAndBindings(const QList<CompiledFunctionOrExpression> &functions); |
| 643 | |
| 644 | bool generateRuntimeFunctions(QmlIR::Object *object); |
| 645 | |
| 646 | private: |
| 647 | Document *document; |
| 648 | }; |
| 649 | |
| 650 | // RegisterStringN ~= std::function<int(QStringView)> |
| 651 | // FinalizeTranlationData ~= std::function<void(QV4::CompiledData::Binding::ValueType, QV4::CompiledData::TranslationData)> |
| 652 | /* |
| 653 | \internal |
| 654 | \a base: name of the potential translation function |
| 655 | \a args: arguments to the function call |
| 656 | \a registerMainString: Takes the first argument passed to the translation function, and it's |
| 657 | result will be stored in a TranslationData's stringIndex for translation bindings and in numbeIndex |
| 658 | for string bindings. |
| 659 | \a registerCommentString: Takes the comment argument passed to some of the translation functions. |
| 660 | Result will be stored in a TranslationData's commentIndex |
| 661 | \a finalizeTranslationData: Takes the type of the binding and the previously set up TranslationData |
| 662 | */ |
| 663 | template< |
| 664 | typename RegisterMainString, |
| 665 | typename RegisterCommentString, |
| 666 | typename RegisterContextString, |
| 667 | typename FinalizeTranslationData> |
| 668 | void tryGeneratingTranslationBindingBase(QStringView base, QQmlJS::AST::ArgumentList *args, |
| 669 | RegisterMainString registerMainString, |
| 670 | RegisterCommentString , |
| 671 | RegisterContextString registerContextString, |
| 672 | FinalizeTranslationData finalizeTranslationData |
| 673 | ) |
| 674 | { |
| 675 | if (base == QLatin1String("qsTr" )) { |
| 676 | QV4::CompiledData::TranslationData translationData; |
| 677 | translationData.number = -1; |
| 678 | |
| 679 | // empty string |
| 680 | translationData.commentIndex = 0; |
| 681 | |
| 682 | // No context (not empty string) |
| 683 | translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex; |
| 684 | |
| 685 | if (!args || !args->expression) |
| 686 | return; // no arguments, stop |
| 687 | |
| 688 | QStringView translation; |
| 689 | if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
| 690 | translation = arg1->value; |
| 691 | } else { |
| 692 | return; // first argument is not a string, stop |
| 693 | } |
| 694 | |
| 695 | translationData.stringIndex = registerMainString(translation); |
| 696 | |
| 697 | args = args->next; |
| 698 | |
| 699 | if (args) { |
| 700 | QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression); |
| 701 | if (!arg2) |
| 702 | return; // second argument is not a string, stop |
| 703 | translationData.commentIndex = registerCommentString(arg2->value); |
| 704 | |
| 705 | args = args->next; |
| 706 | if (args) { |
| 707 | if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(ast: args->expression)) { |
| 708 | translationData.number = int(arg3->value); |
| 709 | args = args->next; |
| 710 | } else { |
| 711 | return; // third argument is not a translation number, stop |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if (args) |
| 717 | return; // too many arguments, stop |
| 718 | |
| 719 | finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData); |
| 720 | } else if (base == QLatin1String("qsTrId" )) { |
| 721 | QV4::CompiledData::TranslationData translationData; |
| 722 | translationData.number = -1; |
| 723 | |
| 724 | // empty string, but unused |
| 725 | translationData.commentIndex = 0; |
| 726 | |
| 727 | // No context (not empty string) |
| 728 | translationData.contextIndex = QV4::CompiledData::TranslationData::NoContextIndex; |
| 729 | |
| 730 | if (!args || !args->expression) |
| 731 | return; // no arguments, stop |
| 732 | |
| 733 | QStringView id; |
| 734 | if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
| 735 | id = arg1->value; |
| 736 | } else { |
| 737 | return; // first argument is not a string, stop |
| 738 | } |
| 739 | translationData.stringIndex = registerMainString(id); |
| 740 | |
| 741 | args = args->next; |
| 742 | |
| 743 | if (args) { |
| 744 | if (QQmlJS::AST::NumericLiteral *arg3 = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(ast: args->expression)) { |
| 745 | translationData.number = int(arg3->value); |
| 746 | args = args->next; |
| 747 | } else { |
| 748 | return; // third argument is not a translation number, stop |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | if (args) |
| 753 | return; // too many arguments, stop |
| 754 | |
| 755 | finalizeTranslationData(QV4::CompiledData::Binding::Type_TranslationById, translationData); |
| 756 | } else if (base == QLatin1String("QT_TR_NOOP" ) || base == QLatin1String("QT_TRID_NOOP" )) { |
| 757 | if (!args || !args->expression) |
| 758 | return; // no arguments, stop |
| 759 | |
| 760 | QStringView str; |
| 761 | if (QQmlJS::AST::StringLiteral *arg1 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
| 762 | str = arg1->value; |
| 763 | } else { |
| 764 | return; // first argument is not a string, stop |
| 765 | } |
| 766 | |
| 767 | args = args->next; |
| 768 | // QT_TR_NOOP can have a disambiguation string, QT_TRID_NOOP can't |
| 769 | if (args && base == QLatin1String("QT_TR_NOOP" )) { |
| 770 | // we have a disambiguation string; we don't need to do anything with it |
| 771 | if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) |
| 772 | args = args->next; |
| 773 | else // second argument is not a string, stop |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | if (args) |
| 778 | return; // too many arguments, stop |
| 779 | |
| 780 | QV4::CompiledData::TranslationData translationData; |
| 781 | translationData.number = registerMainString(str); |
| 782 | finalizeTranslationData(QV4::CompiledData::Binding::Type_String, translationData); |
| 783 | } else if (base == QLatin1String("QT_TRANSLATE_NOOP" )) { |
| 784 | if (!args || !args->expression) |
| 785 | return; // no arguments, stop |
| 786 | |
| 787 | args = args->next; |
| 788 | if (!args || !args->expression) |
| 789 | return; // no second arguments, stop |
| 790 | |
| 791 | QStringView str; |
| 792 | if (QQmlJS::AST::StringLiteral *arg2 = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
| 793 | str = arg2->value; |
| 794 | } else { |
| 795 | return; // first argument is not a string, stop |
| 796 | } |
| 797 | |
| 798 | args = args->next; |
| 799 | if (args) { |
| 800 | // we have a disambiguation string; we don't need to do anything with it |
| 801 | if (QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) |
| 802 | args = args->next; |
| 803 | else // third argument is not a string, stop |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | if (args) |
| 808 | return; // too many arguments, stop |
| 809 | |
| 810 | QV4::CompiledData::TranslationData fakeTranslationData; |
| 811 | fakeTranslationData.number = registerMainString(str); |
| 812 | finalizeTranslationData(QV4::CompiledData::Binding::Type_String, fakeTranslationData); |
| 813 | } else if (base == QLatin1String("qsTranslate" )) { |
| 814 | QV4::CompiledData::TranslationData translationData; |
| 815 | translationData.number = -1; |
| 816 | translationData.commentIndex = 0; // empty string |
| 817 | |
| 818 | if (!args || !args->next) |
| 819 | return; // less than 2 arguments, stop |
| 820 | |
| 821 | QStringView translation; |
| 822 | if (QQmlJS::AST::StringLiteral *arg1 |
| 823 | = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression)) { |
| 824 | translation = arg1->value; |
| 825 | } else { |
| 826 | return; // first argument is not a string, stop |
| 827 | } |
| 828 | |
| 829 | translationData.contextIndex = registerContextString(translation); |
| 830 | |
| 831 | args = args->next; |
| 832 | Q_ASSERT(args); |
| 833 | |
| 834 | QQmlJS::AST::StringLiteral *arg2 |
| 835 | = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression); |
| 836 | if (!arg2) |
| 837 | return; // second argument is not a string, stop |
| 838 | translationData.stringIndex = registerMainString(arg2->value); |
| 839 | |
| 840 | args = args->next; |
| 841 | if (args) { |
| 842 | QQmlJS::AST::StringLiteral *arg3 |
| 843 | = QQmlJS::AST::cast<QQmlJS::AST::StringLiteral *>(ast: args->expression); |
| 844 | if (!arg3) |
| 845 | return; // third argument is not a string, stop |
| 846 | translationData.commentIndex = registerCommentString(arg3->value); |
| 847 | |
| 848 | args = args->next; |
| 849 | if (args) { |
| 850 | if (QQmlJS::AST::NumericLiteral *arg4 |
| 851 | = QQmlJS::AST::cast<QQmlJS::AST::NumericLiteral *>(ast: args->expression)) { |
| 852 | translationData.number = int(arg4->value); |
| 853 | args = args->next; |
| 854 | } else { |
| 855 | return; // fourth argument is not a translation number, stop |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | if (args) |
| 861 | return; // too many arguments, stop |
| 862 | |
| 863 | finalizeTranslationData(QV4::CompiledData::Binding::Type_Translation, translationData); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | } // namespace QmlIR |
| 868 | |
| 869 | QT_END_NAMESPACE |
| 870 | |
| 871 | #endif // QQMLIRBUILDER_P_H |
| 872 | |