summaryrefslogtreecommitdiffstats
path: root/tools/repc/utils.cpp
blob: bd541c060b6ab57cfda63b9a3e5c7f6bd03f414d (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
/****************************************************************************
**
** Copyright (C) 2017 Ford Motor Company
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtRemoteObjects module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
****************************************************************************/

#include "utils.h"

#include "repparser.h"

#include <moc.h>

#define _(X) QString::fromLatin1(X)

QT_BEGIN_NAMESPACE

static QByteArray join(const QVector<QByteArray> &array, const QByteArray &separator)
{
    QByteArray res;
    const int sz = array.size();
    if (!sz)
        return res;
    for (int i = 0; i < sz - 1; i++)
        res += array.at(i) + separator;
    res += array.at(sz - 1);
    return res;
}

static QVector<QByteArray> generateProperties(const QVector<PropertyDef> &properties, bool isPod=false)
{
    QVector<QByteArray> ret;
    for (const PropertyDef& property : properties) {
        if (!isPod && property.notifyId == -1 && !property.constant) {
            qWarning() << "Skipping property" << property.name << "because is non-notifiable & non-constant";
            continue; // skip non-notifiable properties
        }
        QByteArray prop = property.type + " " + property.name;
        if (property.constant)
            prop += " CONSTANT";
        if (property.write.isEmpty() && !property.read.isEmpty())
            prop += " READONLY";
        ret << prop;
    }
    return ret;
}

static QByteArray generateFunctions(const QByteArray &type, const QVector<FunctionDef> &functionList)
{
    QByteArray ret;
    for (const FunctionDef &func : functionList) {
        if (func.access != FunctionDef::Public)
            continue;

        ret += type + "(" + func.normalizedType + " " + func.name + "(";
        const int sz = func.arguments.size();
        if (sz) {
            for (int i = 0; i < sz - 1 ; i++) {
                const ArgumentDef &arg = func.arguments.at(i);
                ret += arg.normalizedType + " " + arg.name + ", ";
            }

            const ArgumentDef &arg = func.arguments.at(sz -1);
            ret += arg.normalizedType + " " + arg.name;
        }
        ret += "));\n";
    }
    return ret;
}

static bool highToLowSort(int a, int b)
{
    return a > b;
}

static QVector<FunctionDef> cleanedSignalList(const ClassDef &cdef)
{
    auto ret = cdef.signalList;
    QVector<int> positions;
    for (const PropertyDef &prop :  qAsConst(cdef.propertyList)) {
        if (prop.notifyId != -1) {
            Q_ASSERT(prop.notify == ret.at(prop.notifyId).name);
            positions.push_back(prop.notifyId);
        }
    }
    std::sort(positions.begin(), positions.end(), highToLowSort);
    for (int pos : qAsConst(positions))
        ret.removeAt(pos);
    return ret;
}

static QVector<FunctionDef> cleanedSlotList(const ClassDef &cdef)
{
    auto ret = cdef.slotList;
    for (const PropertyDef &prop : qAsConst(cdef.propertyList)) {
        if (!prop.write.isEmpty()) {
            auto it = ret.begin();
            while (it != ret.end()) {
                const FunctionDef& fdef = *it;
                if (fdef.name == prop.write &&
                    fdef.arguments.size() == 1 &&
                    fdef.arguments[0].type.name == prop.type) {
                    ret.erase(it);
                    break;
                }
                ++it;
            }
        }
    }
    return ret;
}

QByteArray generateClass(const ClassDef &cdef, bool alwaysGenerateClass /* = false */)
{
    const auto signalList = cleanedSignalList(cdef);
    if (signalList.isEmpty() && cdef.slotList.isEmpty() && !alwaysGenerateClass)
        return "POD " + cdef.classname + "(" + join(generateProperties(cdef.propertyList, true), ", ") + ")\n";

    QByteArray ret("class " + cdef.classname + "\n{\n");
    if (!cdef.propertyList.isEmpty())
        ret += "    PROP(" + join(generateProperties(cdef.propertyList), ");\n    PROP(") + ");\n";
    ret += generateFunctions("    SLOT", cleanedSlotList(cdef));
    ret += generateFunctions("    SIGNAL", signalList);
    ret += "}\n";
    return ret;
}

static QVector<PODAttribute> propertyList2PODAttributes(const QVector<PropertyDef> &list)
{
    QVector<PODAttribute> ret;
    for (const PropertyDef &prop : list)
        ret.push_back(PODAttribute(_(prop.type), _(prop.name)));
    return ret;
}

QVector<ASTProperty> propertyList2AstProperties(const QVector<PropertyDef> &list)
{
    QVector<ASTProperty> ret;
    for (const PropertyDef &property : list) {
        if (property.notifyId == -1 && !property.constant) {
            qWarning() << "Skipping property" << property.name << "because is non-notifiable & non-constant";
            continue; // skip non-notifiable properties
        }
        ASTProperty prop;
        prop.name = _(property.name);
        prop.type = _(property.type);
        prop.modifier = property.constant
                        ? ASTProperty::Constant
                        : property.write.isEmpty() && !property.read.isEmpty()
                          ? ASTProperty::ReadOnly
                          : ASTProperty::ReadWrite;
        ret.push_back(prop);
    }
    return ret;
}

QVector<ASTFunction> functionList2AstFunctionList(const QVector<FunctionDef> &list)
{
    QVector<ASTFunction> ret;
    for (const FunctionDef &fdef : list) {
        if (fdef.access != FunctionDef::Public)
            continue;

        ASTFunction func;
        func.name = _(fdef.name);
        func.returnType = _(fdef.type.name);
        for (const ArgumentDef &arg : fdef.arguments)
            func.params.push_back(ASTDeclaration(_(arg.type.name), _(arg.name)));
        ret.push_back(func);
    }
    return ret;
}

AST classList2AST(const QVector<ClassDef> &classList)
{
    AST ret;
    for (const ClassDef &cdef : classList) {
        const auto signalList = cleanedSignalList(cdef);
        if (signalList.isEmpty() && cdef.slotList.isEmpty()) {
            POD pod;
            pod.name = _(cdef.classname);
            pod.attributes = propertyList2PODAttributes(cdef.propertyList);
            ret.pods.push_back(pod);
        } else {
            ASTClass cl(_(cdef.classname));
            cl.properties = propertyList2AstProperties(cdef.propertyList);
            cl.signalsList = functionList2AstFunctionList(signalList);
            cl.slotsList = functionList2AstFunctionList(cleanedSlotList(cdef));
            ret.classes.push_back(cl);
        }
    }
    return ret;
}

QT_END_NAMESPACE