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
36Q_QMLCOMPILER_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcAotCompiler);
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 int numArguments = 0;
55};
56
57class Q_QMLCOMPILER_EXPORT QQmlJSAotCompiler
58{
59public:
60 enum Flag {
61 NoFlags = 0x0,
62 ValidateBasicBlocks = 0x1,
63 };
64 Q_DECLARE_FLAGS(Flags, Flag)
65
66 QQmlJSAotCompiler(QQmlJSImporter *importer, const QString &resourcePath,
67 const QStringList &qmldirFiles, QQmlJSLogger *logger);
68
69 virtual ~QQmlJSAotCompiler() = default;
70
71 virtual void setDocument(const QmlIR::JSCodeGen *codegen, const QmlIR::Document *document);
72 virtual void setScope(const QmlIR::Object *object, const QmlIR::Object *scope);
73 virtual std::variant<QQmlJSAotFunction, QQmlJS::DiagnosticMessage> compileBinding(
74 const QV4::Compiler::Context *context, const QmlIR::Binding &irBinding,
75 QQmlJS::AST::Node *astNode);
76 virtual std::variant<QQmlJSAotFunction, QQmlJS::DiagnosticMessage> compileFunction(
77 const QV4::Compiler::Context *context, const QString &name, QQmlJS::AST::Node *astNode);
78
79 virtual QQmlJSAotFunction globalCode() const;
80
81 Flags m_flags;
82
83protected:
84 virtual QQmlJS::DiagnosticMessage diagnose(
85 const QString &message, QtMsgType type, const QQmlJS::SourceLocation &location) const;
86
87 QQmlJSTypeResolver m_typeResolver;
88
89 const QString m_resourcePath;
90 const QStringList m_qmldirFiles;
91
92 const QmlIR::Document *m_document = nullptr;
93 const QmlIR::Object *m_currentObject = nullptr;
94 const QmlIR::Object *m_currentScope = nullptr;
95 const QV4::Compiler::JSUnitGenerator *m_unitGenerator = nullptr;
96
97 QQmlJSImporter *m_importer = nullptr;
98 QQmlJSLogger *m_logger = nullptr;
99
100private:
101 QQmlJSAotFunction doCompile(const QV4::Compiler::Context *context,
102 QQmlJSCompilePass::Function *function,
103 QQmlJS::DiagnosticMessage *error);
104 QQmlJSAotFunction doCompileAndRecordAotStats(const QV4::Compiler::Context *context,
105 QQmlJSCompilePass::Function *function,
106 QQmlJS::DiagnosticMessage *error,
107 const QString &name,
108 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 *interface =
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 *interface =
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

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

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