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 <private/qtqmlcompilerexports_p.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/qqmljsdiagnosticmessage_p.h>
26#include <private/qqmljsimporter_p.h>
27#include <private/qqmljslogger_p.h>
28#include <private/qqmljstyperesolver_p.h>
29#include <private/qv4compileddata_p.h>
30
31#include <functional>
32
33QT_BEGIN_NAMESPACE
34
35Q_QMLCOMPILER_PRIVATE_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcAotCompiler);
36
37struct Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSCompileError
38{
39 QString message;
40 void print();
41 QQmlJSCompileError augment(const QString &contextErrorMessage) const;
42 void appendDiagnostics(const QString &inputFileName,
43 const QList<QQmlJS::DiagnosticMessage> &diagnostics);
44 void appendDiagnostic(const QString &inputFileName,
45 const QQmlJS::DiagnosticMessage &diagnostic);
46};
47
48struct Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSAotFunction
49{
50 QStringList includes;
51 QStringList argumentTypes;
52 QString code;
53 QString returnType;
54};
55
56class Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSAotCompiler
57{
58public:
59 QQmlJSAotCompiler(QQmlJSImporter *importer, const QString &resourcePath,
60 const QStringList &qmldirFiles, QQmlJSLogger *logger);
61
62 virtual ~QQmlJSAotCompiler() = default;
63
64 virtual void setDocument(const QmlIR::JSCodeGen *codegen, const QmlIR::Document *document);
65 virtual void setScope(const QmlIR::Object *object, const QmlIR::Object *scope);
66 virtual std::variant<QQmlJSAotFunction, QQmlJS::DiagnosticMessage> compileBinding(
67 const QV4::Compiler::Context *context, const QmlIR::Binding &irBinding,
68 QQmlJS::AST::Node *astNode);
69 virtual std::variant<QQmlJSAotFunction, QQmlJS::DiagnosticMessage> compileFunction(
70 const QV4::Compiler::Context *context, const QString &name, QQmlJS::AST::Node *astNode);
71
72 virtual QQmlJSAotFunction globalCode() const;
73
74protected:
75 virtual QQmlJS::DiagnosticMessage diagnose(
76 const QString &message, QtMsgType type, const QQmlJS::SourceLocation &location) const;
77
78 QQmlJSTypeResolver m_typeResolver;
79
80 const QString m_resourcePath;
81 const QStringList m_qmldirFiles;
82
83 const QmlIR::Document *m_document = nullptr;
84 const QmlIR::Object *m_currentObject = nullptr;
85 const QmlIR::Object *m_currentScope = nullptr;
86 const QV4::Compiler::JSUnitGenerator *m_unitGenerator = nullptr;
87
88 QQmlJSImporter *m_importer = nullptr;
89 QQmlJSLogger *m_logger = nullptr;
90
91private:
92 QQmlJSAotFunction doCompile(
93 const QV4::Compiler::Context *context, QQmlJSCompilePass::Function *function,
94 QQmlJS::DiagnosticMessage *error);
95};
96
97
98using QQmlJSAotFunctionMap = QMap<int, QQmlJSAotFunction>;
99using QQmlJSSaveFunction
100 = std::function<bool(const QV4::CompiledData::SaveableUnitPointer &,
101 const QQmlJSAotFunctionMap &, QString *)>;
102
103bool Q_QMLCOMPILER_PRIVATE_EXPORT qCompileQmlFile(const QString &inputFileName,
104 QQmlJSSaveFunction saveFunction,
105 QQmlJSAotCompiler *aotCompiler, QQmlJSCompileError *error,
106 bool storeSourceLocation = false,
107 QV4::Compiler::CodegenWarningInterface *interface =
108 QV4::Compiler::defaultCodegenWarningInterface(),
109 const QString *fileContents = nullptr);
110bool Q_QMLCOMPILER_PRIVATE_EXPORT qCompileQmlFile(QmlIR::Document &irDocument, const QString &inputFileName,
111 QQmlJSSaveFunction saveFunction,
112 QQmlJSAotCompiler *aotCompiler, QQmlJSCompileError *error,
113 bool storeSourceLocation = false,
114 QV4::Compiler::CodegenWarningInterface *interface =
115 QV4::Compiler::defaultCodegenWarningInterface(),
116 const QString *fileContents = nullptr);
117bool Q_QMLCOMPILER_PRIVATE_EXPORT qCompileJSFile(const QString &inputFileName, const QString &inputFileUrl,
118 QQmlJSSaveFunction saveFunction,
119 QQmlJSCompileError *error);
120
121bool Q_QMLCOMPILER_PRIVATE_EXPORT qSaveQmlJSUnitAsCpp(const QString &inputFileName,
122 const QString &outputFileName,
123 const QV4::CompiledData::SaveableUnitPointer &unit,
124 const QQmlJSAotFunctionMap &aotFunctions,
125 QString *errorString);
126
127QT_END_NAMESPACE
128
129#endif // QQMLJSCOMPILER_P_H
130

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