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#ifndef QMLTCOUTPUTIR_H
5#define QMLTCOUTPUTIR_H
6
7#include <QtCore/qstring.h>
8#include <QtCore/qlist.h>
9#include <QtCore/qstringlist.h>
10#include <QtCore/qset.h>
11
12#include <private/qqmljsmetatypes_p.h>
13
14#include <optional>
15
16QT_BEGIN_NAMESPACE
17
18// Below are the classes that represent compiled QML types in a string data
19// form. These classes are used to generate C++ code.
20
21// Represents C++ variable
22struct QmltcVariable
23{
24 QString cppType; // C++ type of a variable
25 QString name; // variable name
26 QString defaultValue; // optional initialization value
27
28 QmltcVariable() = default;
29 // special ctor for QList's emplace back
30 QmltcVariable(const QString &t, const QString &n, const QString &v = QString())
31 : cppType(t), name(n), defaultValue(v)
32 {
33 }
34};
35
36struct QmltcProperty : QmltcVariable
37{
38 QString containingClass;
39 QString signalName;
40
41 QmltcProperty() = default;
42 QmltcProperty(const QString t, const QString &n, const QString &c, const QString &s)
43 : QmltcVariable(t, n), containingClass(c), signalName(s)
44 {
45 }
46};
47
48// Represents QML -> C++ compiled enumeration type
49struct QmltcEnum
50{
51 QString cppType; // C++ type of an enum
52 QStringList keys; // enumerator keys
53 QStringList values; // enumerator values
54 QString ownMocLine; // special MOC line that follows enum declaration
55
56 QmltcEnum() = default;
57 QmltcEnum(const QString &t, const QStringList &ks, const QStringList &vs, const QString &l)
58 : cppType(t), keys(ks), values(vs), ownMocLine(l)
59 {
60 }
61};
62
63struct QmltcMethodBase
64{
65 QStringList comments; // C++ comments
66 QString name; // C++ function name
67 QList<QmltcVariable> parameterList; // C++ function parameter list
68 QStringList body; // C++ function code
69 QQmlJSMetaMethod::Access access = QQmlJSMetaMethod::Public; // access specifier
70 QStringList declarationPrefixes;
71 QStringList modifiers; // cv-qualifiers, ref-qualifier, noexcept, attributes
72};
73
74// Represents QML -> C++ compiled function
75struct QmltcMethod : QmltcMethodBase
76{
77 QString returnType; // C++ return type
78 QQmlJSMetaMethodType type = QQmlJSMetaMethodType::Method; // Qt function type
79
80 // TODO: should be a better way to handle this
81 bool userVisible = false; // tells if a function is prioritized during the output generation
82};
83
84// Represents C++ ctor of a type
85struct QmltcCtor : QmltcMethodBase
86{
87 QStringList initializerList; // C++ ctor's initializer list
88};
89
90// Represents C++ dtor of a type
91struct QmltcDtor : QmltcMethodBase
92{
93};
94
95// Represents a generated class that knows how to set the public,
96// writable properties of a compiled QML -> C++ type.
97// This is generally intended to be available for the root of the
98// document to allow the user to set the initial values for
99// properties, when creating a component, with support for strong
100// typing.
101struct QmltcPropertyInitializer {
102 QString name;
103
104 QmltcCtor constructor;
105
106 // A member containing a reference to the object for which the
107 // properties should be set.
108 QmltcVariable component;
109
110 // A member containing a cache of properties that were actually
111 // set that can be referenced later..
112 QmltcVariable initializedCache;
113
114 // Setter methods for each property.
115 QList<QmltcMethod> propertySetters;
116};
117
118// Represents a generated class that contains a bundle of values to
119// initialize the required properties of a type.
120//
121// This is generally intended to be available for the root component
122// of the document, where it will be used as a constructor argument to
123// force the user to provide initial values for the required
124// properties of the constructed type.
125struct QmltcRequiredPropertiesBundle {
126 QString name;
127
128 QList<QmltcVariable> members;
129};
130
131// Represents QML -> C++ compiled type
132struct QmltcType
133{
134 QString cppType; // C++ type of the QML type
135 QStringList baseClasses; // C++ type names of base classes
136 QStringList mocCode; // Qt MOC code
137 QStringList otherCode; // Random code that doesn't fit any category, e.g. friend declarations
138
139 // member types: enumerations and child types
140 QList<QmltcEnum> enums;
141 QList<QmltcType> children; // these are pretty much always empty
142
143 // special member functions:
144 QmltcCtor baselineCtor {}; // does basic contruction
145 QmltcCtor externalCtor {}; // calls basicCtor, calls init
146 QmltcMethod init {}; // starts object initialization (context setup), calls finalize
147 QmltcMethod beginClass {}; // calls QQmlParserStatus::classBegin()
148 QmltcMethod endInit {}; // ends object initialization (with "simple" bindings setup)
149 QmltcMethod setComplexBindings {}; // sets up "complex" (e.g. script) bindings
150 QmltcMethod completeComponent {}; // calls QQmlParserStatus::componentComplete()
151 QmltcMethod finalizeComponent {}; // calls QQmlFinalizerHook::componentFinalized()
152 QmltcMethod handleOnCompleted {}; // calls Component.onCompleted
153
154 std::optional<QmltcDtor> dtor {};
155
156 // member functions: methods, signals and slots
157 QList<QmltcMethod> functions;
158 // member variables
159 QList<QmltcVariable> variables;
160 QList<QmltcProperty> properties;
161
162 // QML document root specific:
163 std::optional<QmltcMethod> typeCount; // the number of QML types defined in a document
164
165 // TODO: only needed for binding callables - should not be needed, generally
166 bool ignoreInit = false; // specifies whether init and externalCtor should be ignored
167
168 // needed for singletons
169 std::optional<QmltcMethod> staticCreate{};
170
171 // A proxy class that provides a restricted interface that only
172 // allows setting the properties of the type.
173 QmltcPropertyInitializer propertyInitializer{};
174
175 std::optional<QmltcRequiredPropertiesBundle> requiredPropertiesBundle{};
176};
177
178// Represents whole QML program, compiled to C++
179struct QmltcProgram
180{
181 QString url; // QML file url
182 QString cppPath; // C++ output .cpp path
183 QString hPath; // C++ output .h path
184 QString outNamespace;
185 QString exportMacro; // if not empty, the macro that should be used to export the generated
186 // classes
187 QSet<QString> includes; // non-default C++ include files
188 QmltcMethod urlMethod; // returns QUrl of the QML document
189
190 QList<QmltcType> compiledTypes; // all QML types that are compiled to C++
191};
192
193QT_END_NAMESPACE
194
195#endif // QMLTCOUTPUTIR_H
196

source code of qtdeclarative/tools/qmltc/qmltcoutputir.h