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 | |
16 | QT_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 |
22 | struct 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 | |
36 | struct 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 |
49 | struct 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 | |
63 | struct QmltcMethodBase |
64 | { |
65 | QStringList ; // 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 |
75 | struct 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 |
85 | struct QmltcCtor : QmltcMethodBase |
86 | { |
87 | QStringList initializerList; // C++ ctor's initializer list |
88 | }; |
89 | |
90 | // Represents C++ dtor of a type |
91 | struct QmltcDtor : QmltcMethodBase |
92 | { |
93 | }; |
94 | |
95 | // Represents QML -> C++ compiled type |
96 | struct QmltcType |
97 | { |
98 | QString cppType; // C++ type of the QML type |
99 | QStringList baseClasses; // C++ type names of base classes |
100 | QStringList mocCode; // Qt MOC code |
101 | QStringList otherCode; // Random code that doesn't fit any category, e.g. friend declarations |
102 | |
103 | // member types: enumerations and child types |
104 | QList<QmltcEnum> enums; |
105 | QList<QmltcType> children; // these are pretty much always empty |
106 | |
107 | // special member functions: |
108 | QmltcCtor baselineCtor {}; // does basic contruction |
109 | QmltcCtor externalCtor {}; // calls basicCtor, calls init |
110 | QmltcMethod init {}; // starts object initialization (context setup), calls finalize |
111 | QmltcMethod beginClass {}; // calls QQmlParserStatus::classBegin() |
112 | QmltcMethod endInit {}; // ends object initialization (with "simple" bindings setup) |
113 | QmltcMethod setComplexBindings {}; // sets up "complex" (e.g. script) bindings |
114 | QmltcMethod completeComponent {}; // calls QQmlParserStatus::componentComplete() |
115 | QmltcMethod finalizeComponent {}; // calls QQmlFinalizerHook::componentFinalized() |
116 | QmltcMethod handleOnCompleted {}; // calls Component.onCompleted |
117 | |
118 | std::optional<QmltcDtor> dtor {}; |
119 | |
120 | // member functions: methods, signals and slots |
121 | QList<QmltcMethod> functions; |
122 | // member variables |
123 | QList<QmltcVariable> variables; |
124 | QList<QmltcProperty> properties; |
125 | |
126 | // QML document root specific: |
127 | std::optional<QmltcMethod> typeCount; // the number of QML types defined in a document |
128 | |
129 | // TODO: only needed for binding callables - should not be needed, generally |
130 | bool ignoreInit = false; // specifies whether init and externalCtor should be ignored |
131 | |
132 | // needed for singletons |
133 | std::optional<QmltcMethod> staticCreate{}; |
134 | }; |
135 | |
136 | // Represents whole QML program, compiled to C++ |
137 | struct QmltcProgram |
138 | { |
139 | QString url; // QML file url |
140 | QString cppPath; // C++ output .cpp path |
141 | QString hPath; // C++ output .h path |
142 | QString outNamespace; |
143 | QString exportMacro; // if not empty, the macro that should be used to export the generated |
144 | // classes |
145 | QSet<QString> includes; // non-default C++ include files |
146 | QmltcMethod urlMethod; // returns QUrl of the QML document |
147 | |
148 | QList<QmltcType> compiledTypes; // all QML types that are compiled to C++ |
149 | }; |
150 | |
151 | QT_END_NAMESPACE |
152 | |
153 | #endif // QMLTCOUTPUTIR_H |
154 | |