aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmltc/prototype/codegeneratorutil.cpp
blob: 878a1f4b071ba4c61e16e423913a1364a70ad6dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "prototype/codegeneratorutil.h"
#include "prototype/qml2cpppropertyutils.h"

#include <QtCore/qstringbuilder.h>

#include <tuple>

QT_BEGIN_NAMESPACE

// NB: this variable would behave correctly as long as QML init and QML finalize
// are non-virtual functions
const QmltcVariable CodeGeneratorUtility::childrenOffsetVariable { u"qsizetype"_qs,
                                                                   u"QML_choffset"_qs, QString() };

const QmltcVariable CodeGeneratorUtility::compilationUnitVariable {
    u"QV4::ExecutableCompilationUnit *"_qs, u"QML_cu"_qs, QString()
};

static std::tuple<QStringList, QString, QStringList>
wrapIfMismatchingType(const QQmlJSMetaProperty &p, QString value)
{
    auto isDerivedFromBuiltin = [](QQmlJSScope::ConstPtr t, const QString &builtin) {
        for (; t; t = t->baseType()) {
            if (t->internalName() == builtin)
                return true;
        }
        return false;
    };
    QStringList prologue;
    QStringList epilogue;
    auto propType = p.type();
    if (isDerivedFromBuiltin(propType, u"QVariant"_qs)) {
        const QString variantName = u"var_" + p.propertyName();
        prologue << u"{ // accepts QVariant"_qs;
        prologue << u"QVariant " + variantName + u";";
        prologue << variantName + u".setValue(" + value + u");";
        epilogue << u"}"_qs;
        value = u"std::move(" + variantName + u")";
    }
    /*else if (isDerivedFromBuiltin(propType, u"QQmlComponent"_qs)) {
        // assume value can always be converted to "QObject *"
        prologue << u"// accepts QQmlComponent"_qs;
        value = u"new QQmlComponent(" + value + u")";
    }*/
    return { prologue, value, epilogue };
}

// TODO: when assigning to a property, if property.type() != value.type(), have
// to wrap it (actually, wrap unconditionally if property.type() is QVariant or
// QQmlComponent -- other cases out of scope for now)
QStringList CodeGeneratorUtility::generate_assignToProperty(
        const QQmlJSScope::ConstPtr &type, const QString &propertyName, const QQmlJSMetaProperty &p,
        const QString &value, const QString &accessor, bool constructQVariant)
{
    Q_ASSERT(p.isValid());
    Q_ASSERT(!p.isList()); // NB: this code does not handle list properties

    QStringList code;
    code.reserve(6); // should always be enough

    if (type->hasOwnProperty(propertyName)) {
        Q_ASSERT(!p.isPrivate());
        // this object is compiled, so just assignment should work fine
        code << accessor + u"->m_" + propertyName + u" = " + value + u";";
    } else if (QString propertySetter = p.write(); !propertySetter.isEmpty()) {
        // there's a WRITE function
        auto [prologue, wrappedValue, epilogue] = wrapIfMismatchingType(p, value);
        code += prologue;
        code << CodeGeneratorUtility::generate_getPrivateClass(accessor, p) + u"->" + propertySetter
                        + u"(" + wrappedValue + u");";
        code += epilogue;
    } else {
        // this property is weird, fallback to `setProperty`
        code << u"{ // couldn't find property setter, so using QObject::setProperty()"_qs;
        QString val = value;
        if (constructQVariant) {
            const QString variantName = u"var_" + propertyName;
            code << u"QVariant " + variantName + u";";
            code << variantName + u".setValue(" + val + u");";
            val = u"std::move(" + variantName + u")";
        }
        // NB: setProperty() would handle private properties
        code << accessor + u"->setProperty(\"" + propertyName + u"\", " + val + u");";
        code << u"}"_qs;
    }

    return code;
}

QStringList CodeGeneratorUtility::generate_assignToSpecialAlias(
        const QQmlJSScope::ConstPtr &type, const QString &propertyName, const QQmlJSMetaProperty &p,
        const QString &value, const QString &accessor, bool constructQVariant)
{
    Q_UNUSED(type);
    Q_UNUSED(propertyName);
    Q_UNUSED(constructQVariant);

    Q_ASSERT(p.isValid());
    Q_ASSERT(!p.isList()); // NB: this code does not handle list properties
    Q_ASSERT(p.isAlias());
    Q_ASSERT(p.type());

    QStringList code;
    code.reserve(6); // should always be enough
    // pretend there's a WRITE function
    Qml2CppPropertyData data(p);
    auto [prologue, wrappedValue, epilogue] = wrapIfMismatchingType(p, value);
    code += prologue;
    code << CodeGeneratorUtility::generate_getPrivateClass(accessor, p) + u"->" + data.write + u"("
                    + wrappedValue + u");";
    code += epilogue;
    return code;
}

QStringList CodeGeneratorUtility::generate_callExecuteRuntimeFunction(
        const QString &url, qsizetype index, const QString &accessor, const QString &returnType,
        const QList<QmltcVariable> &parameters)
{
    QStringList code;
    code.reserve(12); // should always be enough

    code << u"QQmlEnginePrivate *e = QQmlEnginePrivate::get(qmlEngine(" + accessor + u"));";

    const QString returnValueName = u"_ret"_qs;
    QStringList args;
    args.reserve(parameters.size() + 1);
    QStringList types;
    types.reserve(parameters.size() + 1);
    if (returnType == u"void"_qs) {
        args << u"nullptr"_qs;
        types << u"QMetaType::fromType<void>()"_qs;
    } else {
        code << returnType + u" " + returnValueName + u"{};"; // TYPE _ret{};
        args << u"const_cast<void *>(reinterpret_cast<const void *>(std::addressof("
                        + returnValueName + u")))";
        types << u"QMetaType::fromType<std::decay_t<" + returnType + u">>()";
    }

    for (const QmltcVariable &p : parameters) {
        args << u"const_cast<void *>(reinterpret_cast<const void *>(std::addressof(" + p.name
                        + u")))";
        types << u"QMetaType::fromType<std::decay_t<" + p.cppType + u">>()";
    }

    code << u"void *_a[] = { " + args.join(u", "_qs) + u" };";
    code << u"QMetaType _t[] = { " + types.join(u", "_qs) + u" };";
    code << u"e->executeRuntimeFunction(" + url + u", " + QString::number(index) + u", " + accessor
                    + u", " + QString::number(parameters.size()) + u", _a, _t);";
    if (returnType != u"void"_qs)
        code << u"return " + returnValueName + u";";

    return code;
}

QStringList CodeGeneratorUtility::generate_createBindingOnProperty(
        const QString &unitVarName, const QString &scope, qsizetype functionIndex,
        const QString &target, int propertyIndex, const QQmlJSMetaProperty &p, int valueTypeIndex,
        const QString &subTarget)
{
    QStringList code;
    code.reserve(1);

    const QString propName = u"QStringLiteral(\"" + p.propertyName() + u"\")";
    if (QString bindable = p.bindable(); !bindable.isEmpty()) {
        // TODO: test that private properties are bindable
        QString createBindingForBindable = u"QT_PREPEND_NAMESPACE(QQmlCppBinding)::"
                                           u"createBindingForBindable("
                + unitVarName + u", " + scope + u", " + QString::number(functionIndex) + u", "
                + target + u", " + QString::number(propertyIndex) + u", "
                + QString::number(valueTypeIndex) + u", " + propName + u")";
        const QString accessor = (valueTypeIndex == -1) ? target : subTarget;
        code << CodeGeneratorUtility::generate_getPrivateClass(accessor, p) + u"->" + bindable
                        + u"().setBinding(" + createBindingForBindable + u");";
    } else {
        // TODO: test that bindings on private properties also work
        QString createBindingForNonBindable =
                u"QT_PREPEND_NAMESPACE(QQmlCppBinding)::createBindingForNonBindable(" + unitVarName
                + u", " + scope + u", " + QString::number(functionIndex) + u", " + target + u", "
                + QString::number(propertyIndex) + u", " + QString::number(valueTypeIndex) + u", "
                + propName + u")";
        // Note: in this version, the binding is set implicitly
        code << createBindingForNonBindable + u";";
    }

    return code;
}

QString CodeGeneratorUtility::generate_qOverload(const QList<QmltcVariable> &params,
                                                 const QString &overloaded)
{
    QStringList types;
    types.reserve(params.size());
    for (const QmltcVariable &p : params)
        types.emplaceBack(p.cppType);
    return u"qOverload<" + types.join(u", "_qs) + u">(" + overloaded + u")";
}

QString CodeGeneratorUtility::generate_addressof(const QString &addressed)
{
    return u"(&" + addressed + u")";
}

QString CodeGeneratorUtility::generate_getPrivateClass(const QString &accessor,
                                                       const QQmlJSMetaProperty &p)
{
    if (!p.isPrivate())
        return accessor;

    const QString privateType = p.privateClass();
    return u"static_cast<" + privateType + u" *>(QObjectPrivate::get(" + accessor + u"))";
}

QString CodeGeneratorUtility::generate_setIdValue(const QString &context, qsizetype index,
                                                  const QString &accessor, const QString &idString)
{
    Q_ASSERT(index >= 0);
    const QString idComment = u"/* id: " + idString + u" */";
    return context + u"->setIdValue(" + QString::number(index) + idComment + u", " + accessor
            + u")";
}

QT_END_NAMESPACE