| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "qqmljslintercodegen_p.h" |
| 5 | |
| 6 | #include <QtQmlCompiler/private/qqmljsimportvisitor_p.h> |
| 7 | #include <QtQmlCompiler/private/qqmljsshadowcheck_p.h> |
| 8 | #include <QtQmlCompiler/private/qqmljsstoragegeneralizer_p.h> |
| 9 | #include <QtQmlCompiler/private/qqmljsstorageinitializer_p.h> |
| 10 | #include <QtQmlCompiler/private/qqmljstypepropagator_p.h> |
| 11 | #include <QtQmlCompiler/private/qqmljsfunctioninitializer_p.h> |
| 12 | #include <QtQmlCompiler/private/qqmljsbasicblocks_p.h> |
| 13 | |
| 14 | #include <QFileInfo> |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | using namespace Qt::StringLiterals; |
| 19 | |
| 20 | QQmlJSLinterCodegen::QQmlJSLinterCodegen(QQmlJSImporter *importer, const QString &fileName, |
| 21 | const QStringList &qmldirFiles, QQmlJSLogger *logger, |
| 22 | const QQmlJS::ContextProperties &knownContextProperties) |
| 23 | : QQmlJSAotCompiler(importer, fileName, qmldirFiles, logger), |
| 24 | m_knownContextProperties(knownContextProperties) |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | void QQmlJSLinterCodegen::setDocument(const QmlIR::JSCodeGen *codegen, |
| 29 | const QmlIR::Document *document) |
| 30 | { |
| 31 | Q_UNUSED(codegen); |
| 32 | m_document = document; |
| 33 | m_unitGenerator = &document->jsGenerator; |
| 34 | } |
| 35 | |
| 36 | std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>> |
| 37 | QQmlJSLinterCodegen::compileBinding(const QV4::Compiler::Context *context, |
| 38 | const QmlIR::Binding &irBinding, QQmlJS::AST::Node *astNode) |
| 39 | { |
| 40 | const QString name = m_document->stringAt(index: irBinding.propertyNameIndex); |
| 41 | m_logger->setCompileErrorPrefix( |
| 42 | u"Could not determine signature of binding for %1: "_s .arg(a: name)); |
| 43 | |
| 44 | QQmlJSFunctionInitializer initializer( |
| 45 | &m_typeResolver, m_currentObject->location, m_currentScope->location, m_logger); |
| 46 | QQmlJSCompilePass::Function function = initializer.run(context, propertyName: name, astNode, irBinding); |
| 47 | |
| 48 | m_logger->iterateCurrentFunctionMessages(f: [this](const Message &error) { |
| 49 | diagnose(message: error.message, type: error.type, location: error.loc); |
| 50 | }); |
| 51 | |
| 52 | m_logger->setCompileErrorPrefix(u"Could not compile binding for %1: "_s .arg(a: name)); |
| 53 | m_logger->setCompileSkipPrefix(u"Compilation of binding for %1 was skipped: "_s .arg(a: name)); |
| 54 | |
| 55 | analyzeFunction(context, function: &function); |
| 56 | if (const auto errors = finalizeBindingOrFunction()) |
| 57 | return *errors; |
| 58 | |
| 59 | return QQmlJSAotFunction {}; |
| 60 | } |
| 61 | |
| 62 | std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>> |
| 63 | QQmlJSLinterCodegen::compileFunction(const QV4::Compiler::Context *context, |
| 64 | const QString &name, QQmlJS::AST::Node *astNode) |
| 65 | { |
| 66 | m_logger->setCompileErrorPrefix(u"Could not determine signature of function %1: "_s .arg(a: name)); |
| 67 | |
| 68 | QQmlJSFunctionInitializer initializer( |
| 69 | &m_typeResolver, m_currentObject->location, m_currentScope->location, m_logger); |
| 70 | QQmlJSCompilePass::Function function = initializer.run(context, functionName: name, astNode); |
| 71 | |
| 72 | m_logger->iterateCurrentFunctionMessages(f: [this](const Message &error) { |
| 73 | diagnose(message: error.message, type: error.type, location: error.loc); |
| 74 | }); |
| 75 | |
| 76 | m_logger->setCompileErrorPrefix(u"Could not compile function %1: "_s .arg(a: name)); |
| 77 | m_logger->setCompileSkipPrefix(u"Compilation of function %1 was skipped: "_s .arg(a: name)); |
| 78 | analyzeFunction(context, function: &function); |
| 79 | |
| 80 | if (const auto errors = finalizeBindingOrFunction()) |
| 81 | return *errors; |
| 82 | |
| 83 | return QQmlJSAotFunction {}; |
| 84 | } |
| 85 | |
| 86 | void QQmlJSLinterCodegen::setPassManager(QQmlSA::PassManager *passManager) |
| 87 | { |
| 88 | m_passManager = passManager; |
| 89 | auto managerPriv = QQmlSA::PassManagerPrivate::get(manager: passManager); |
| 90 | managerPriv->m_typeResolver = typeResolver(); |
| 91 | } |
| 92 | |
| 93 | void QQmlJSLinterCodegen::analyzeFunction(const QV4::Compiler::Context *context, |
| 94 | QQmlJSCompilePass::Function *function) |
| 95 | { |
| 96 | bool dummy = false; |
| 97 | QQmlJSCompilePass::BlocksAndAnnotations blocksAndAnnotations = |
| 98 | QQmlJSBasicBlocks(context, m_unitGenerator, &m_typeResolver, m_logger) |
| 99 | .run(function, compileFlags: ValidateBasicBlocks, basicBlocksValidationFailed&: dummy); |
| 100 | |
| 101 | blocksAndAnnotations = |
| 102 | QQmlJSTypePropagator(m_unitGenerator, &m_typeResolver, m_logger, |
| 103 | blocksAndAnnotations.basicBlocks, blocksAndAnnotations.annotations, |
| 104 | m_passManager, m_knownContextProperties) |
| 105 | .run(m_function: function); |
| 106 | |
| 107 | if (m_logger->isCategoryIgnored(id: qmlCompiler)) |
| 108 | return; |
| 109 | |
| 110 | if (!m_logger->currentFunctionHasCompileError()) { |
| 111 | blocksAndAnnotations = QQmlJSShadowCheck(m_unitGenerator, &m_typeResolver, m_logger, |
| 112 | blocksAndAnnotations.basicBlocks, |
| 113 | blocksAndAnnotations.annotations) |
| 114 | .run(function); |
| 115 | } |
| 116 | |
| 117 | if (!m_logger->currentFunctionHasCompileError()) { |
| 118 | blocksAndAnnotations = QQmlJSStorageInitializer(m_unitGenerator, &m_typeResolver, m_logger, |
| 119 | blocksAndAnnotations.basicBlocks, |
| 120 | blocksAndAnnotations.annotations) |
| 121 | .run(function); |
| 122 | } |
| 123 | |
| 124 | if (!m_logger->currentFunctionHasCompileError()) { |
| 125 | blocksAndAnnotations = QQmlJSStorageGeneralizer(m_unitGenerator, &m_typeResolver, m_logger, |
| 126 | blocksAndAnnotations.basicBlocks, |
| 127 | blocksAndAnnotations.annotations) |
| 128 | .run(function); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | QT_END_NAMESPACE |
| 133 | |