1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef QQMLJSCOMPILER_P_H
5#define QQMLJSCOMPILER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16
17#include <qtqmlcompilerexports.h>
18
19#include <QtCore/qstring.h>
20#include <QtCore/qlist.h>
21#include <QtCore/qloggingcategory.h>
22
23#include <private/qqmlirbuilder_p.h>
24#include <private/qqmljscompilepass_p.h>
25#include <private/qqmljscompilerstats_p.h>
26#include <private/qqmljsdiagnosticmessage_p.h>
27#include <private/qqmljsimporter_p.h>
28#include <private/qqmljslogger_p.h>
29#include <private/qqmljstyperesolver_p.h>
30#include <private/qv4compileddata_p.h>
31
32#include <functional>
33
34QT_BEGIN_NAMESPACE
35
36QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(lcAotCompiler, Q_QMLCOMPILER_EXPORT);
37
38struct Q_QMLCOMPILER_EXPORT QQmlJSCompileError
39{
40 QString message;
41 void print();
42 QQmlJSCompileError augment(const QString &contextErrorMessage) const;
43 void appendDiagnostics(const QString &inputFileName,
44 const QList<QQmlJS::DiagnosticMessage> &diagnostics);
45 void appendDiagnostic(const QString &inputFileName,
46 const QQmlJS::DiagnosticMessage &diagnostic);
47};
48
49struct Q_QMLCOMPILER_EXPORT QQmlJSAotFunction
50{
51 QStringList includes;
52 QString code;
53 QString signature;
54 std::optional<QString> skipReason;
55 int numArguments = 0;
56};
57
58class Q_QMLCOMPILER_EXPORT QQmlJSAotCompiler
59{
60public:
61 enum Flag {
62 NoFlags = 0x0,
63 ValidateBasicBlocks = 0x1,
64 };
65 Q_DECLARE_FLAGS(Flags, Flag)
66
67 QQmlJSAotCompiler(QQmlJSImporter *importer, const QString &resourcePath,
68 const QStringList &qmldirFiles, QQmlJSLogger *logger);
69
70 virtual ~QQmlJSAotCompiler() = default;
71
72 virtual void setDocument(const QmlIR::JSCodeGen *codegen, const QmlIR::Document *document);
73 virtual void setScope(const QmlIR::Object *object, const QmlIR::Object *scope);
74 virtual std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>> compileBinding(
75 const QV4::Compiler::Context *context, const QmlIR::Binding &irBinding,
76 QQmlJS::AST::Node *astNode);
77 virtual std::variant<QQmlJSAotFunction, QList<QQmlJS::DiagnosticMessage>> compileFunction(
78 const QV4::Compiler::Context *context, const QString &name, QQmlJS::AST::Node *astNode);
79
80 virtual QQmlJSAotFunction globalCode() const;
81
82 Flags m_flags;
83
84protected:
85 std::optional<QList<QQmlJS::DiagnosticMessage>> finalizeBindingOrFunction();
86
87 virtual QQmlJS::DiagnosticMessage diagnose(
88 const QString &message, QtMsgType type, const QQmlJS::SourceLocation &location) const;
89
90 QQmlJSTypeResolver m_typeResolver;
91
92 const QString m_resourcePath;
93 const QStringList m_qmldirFiles;
94
95 const QmlIR::Document *m_document = nullptr;
96 const QmlIR::Object *m_currentObject = nullptr;
97 const QmlIR::Object *m_currentScope = nullptr;
98 const QV4::Compiler::JSUnitGenerator *m_unitGenerator = nullptr;
99
100 QQmlJSImporter *m_importer = nullptr;
101 QQmlJSLogger *m_logger = nullptr;
102
103private:
104 QQmlJSAotFunction doCompile(
105 const QV4::Compiler::Context *context, QQmlJSCompilePass::Function *function);
106 QQmlJSAotFunction doCompileAndRecordAotStats(
107 const QV4::Compiler::Context *context, QQmlJSCompilePass::Function *function,
108 const QString &name, QQmlJS::SourceLocation location);
109};
110
111Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlJSAotCompiler::Flags);
112
113using QQmlJSAotFunctionMap = QMap<int, QQmlJSAotFunction>;
114using QQmlJSSaveFunction
115 = std::function<bool(const QV4::CompiledData::SaveableUnitPointer &,
116 const QQmlJSAotFunctionMap &, QString *)>;
117
118bool Q_QMLCOMPILER_EXPORT qCompileQmlFile(const QString &inputFileName,
119 QQmlJSSaveFunction saveFunction,
120 QQmlJSAotCompiler *aotCompiler, QQmlJSCompileError *error,
121 bool storeSourceLocation = false,
122 QV4::Compiler::CodegenWarningInterface *wInterface =
123 QV4::Compiler::defaultCodegenWarningInterface(),
124 const QString *fileContents = nullptr);
125bool Q_QMLCOMPILER_EXPORT qCompileQmlFile(QmlIR::Document &irDocument, const QString &inputFileName,
126 QQmlJSSaveFunction saveFunction,
127 QQmlJSAotCompiler *aotCompiler, QQmlJSCompileError *error,
128 bool storeSourceLocation = false,
129 QV4::Compiler::CodegenWarningInterface *wInterface =
130 QV4::Compiler::defaultCodegenWarningInterface(),
131 const QString *fileContents = nullptr);
132bool Q_QMLCOMPILER_EXPORT qCompileJSFile(const QString &inputFileName, const QString &inputFileUrl,
133 QQmlJSSaveFunction saveFunction,
134 QQmlJSCompileError *error);
135
136bool Q_QMLCOMPILER_EXPORT qSaveQmlJSUnitAsCpp(const QString &inputFileName,
137 const QString &outputFileName,
138 const QV4::CompiledData::SaveableUnitPointer &unit,
139 const QQmlJSAotFunctionMap &aotFunctions,
140 QString *errorString);
141
142QT_END_NAMESPACE
143
144#endif // QQMLJSCOMPILER_P_H
145

source code of qtdeclarative/src/qmlcompiler/qqmljscompiler_p.h