aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmltyperegistrar/qmetatypesjsonprocessor_p.h
blob: 9d86665c52d7525ee2a2b2fea4ffa259c3b8b17a (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#ifndef METATYPESJSONPROCESSOR_P_H
#define METATYPESJSONPROCESSOR_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QtCore/qcbormap.h>
#include <QtCore/qstring.h>
#include <QtCore/qtyperevision.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qvector.h>

QT_BEGIN_NAMESPACE

// With all the QAnyStringViews in this file we rely on the Cbor data to stay
// in place if you don't change the Cbor contents. We assume that Cbor data
// is implicitly shared so that merely copying const Cbor objects does not copy
// the contents.

enum class Access { Public, Protected, Private };

struct BaseType
{
    using Container = QVarLengthArray<BaseType, 1>;

    BaseType() = default;
    BaseType(const QCborMap &cbor);

    QAnyStringView name;
    Access access;
};

struct ClassInfo
{
    using Container = std::vector<ClassInfo>;

    ClassInfo() = default;
    ClassInfo(const QCborMap &cbor);

    QAnyStringView name;
    QAnyStringView value;
};

struct Interface
{
    using Container = QVarLengthArray<Interface, 1>;

    Interface() = default;
    Interface(const QCborValue &cbor);

    QAnyStringView className;
};

struct Property
{
    using Container = std::vector<Property>;

    Property() = default;
    Property(const QCborMap &cbor);

    QAnyStringView name;
    QAnyStringView type;

    QAnyStringView member;
    QAnyStringView read;
    QAnyStringView write;
    QAnyStringView reset;
    QAnyStringView notify;
    QAnyStringView bindable;

    QAnyStringView privateClass;

    int index = -1;

    QTypeRevision revision;

    bool isFinal = false;
    bool isConstant = false;
    bool isRequired = false;
};

struct Argument
{
    using Container = std::vector<Argument>;

    Argument() = default;
    Argument(const QCborMap &cbor);

    QAnyStringView name;
    QAnyStringView type;
};

struct Method
{
    using Container = std::vector<Method>;

    Method() = default;
    Method(const QCborMap &cbor, bool isConstructor);

    QAnyStringView name;

    Argument::Container arguments;
    QAnyStringView returnType;

    QTypeRevision revision;

    Access access = Access::Public;

    bool isCloned = false;
    bool isJavaScriptFunction = false;
    bool isConstructor = false;
};

struct Enum
{
    using Container = std::vector<Enum>;

    Enum() = default;
    Enum(const QCborMap &cbor);

    QAnyStringView name;
    QAnyStringView alias;
    QAnyStringView type;

    QList<QAnyStringView> values;

    bool isFlag = false;
    bool isClass = false;
};

struct MetaTypePrivate
{
    Q_DISABLE_COPY_MOVE(MetaTypePrivate)

    enum Kind : quint8 { Object, Gadget, Namespace, Unknown };

    MetaTypePrivate() = default;
    MetaTypePrivate(const QCborMap &cbor, const QString &inputFile);

    const QCborMap cbor; // need to keep this to hold on to the strings
    const QString inputFile;

    QAnyStringView className;
    QAnyStringView qualifiedClassName;
    BaseType::Container superClasses;
    ClassInfo::Container classInfos;
    Interface::Container ifaces;

    Property::Container properties;

    Method::Container methods;
    Method::Container sigs;
    Method::Container constructors;

    Enum::Container enums;

    Kind kind = Unknown;
};

class MetaType
{
public:
    using Kind = MetaTypePrivate::Kind;

    MetaType() = default;
    MetaType(const QCborMap &cbor, const QString &inputFile);

    bool isEmpty() const { return d == &s_empty; }

    QString inputFile() const { return d->inputFile; }
    QAnyStringView className() const { return d->className; }
    QAnyStringView qualifiedClassName() const { return d->qualifiedClassName; }
    const BaseType::Container &superClasses() const { return d->superClasses; }
    const ClassInfo::Container &classInfos() const { return d->classInfos; }
    const Interface::Container &ifaces() const { return d->ifaces; }

    const Property::Container &properties() const { return d->properties; }
    const Method::Container &methods() const { return d->methods; }
    const Method::Container &sigs() const { return d->sigs; }
    const Method::Container &constructors() const { return d->constructors; }

    const Enum::Container &enums() const { return d->enums; }

    Kind kind() const { return d->kind; }

private:
    friend bool operator==(const MetaType &a, const MetaType &b) noexcept
    {
        return a.d == b.d;
    }

    friend bool operator!=(const MetaType &a, const MetaType &b) noexcept
    {
        return !(a == b);
    }

    static const MetaTypePrivate s_empty;
    const MetaTypePrivate *d = &s_empty;
};

class MetaTypesJsonProcessor
{
public:
    static QList<QAnyStringView> namespaces(const MetaType &classDef);

    MetaTypesJsonProcessor(bool privateIncludes) : m_privateIncludes(privateIncludes) {}

    bool processTypes(const QStringList &files);

    bool processForeignTypes(const QString &foreignTypesFile);
    bool processForeignTypes(const QStringList &foreignTypesFiles);

    void postProcessTypes();
    void postProcessForeignTypes();

    QVector<MetaType> types() const { return m_types; }
    QVector<MetaType> foreignTypes() const { return m_foreignTypes; }
    QList<QAnyStringView> referencedTypes() const { return m_referencedTypes; }
    QList<QString> includes() const { return m_includes; }

    QString extractRegisteredTypes() const;

private:
    enum RegistrationMode {
        NoRegistration,
        ObjectRegistration,
        GadgetRegistration,
        NamespaceRegistration
    };

    struct PreProcessResult {
        QList<QAnyStringView> primitiveAliases;
        QAnyStringView foreignPrimitive;
        RegistrationMode mode;
    };

    struct PotentialPrimitiveType {
        QAnyStringView name;
        QString file;
    };

    enum class PopulateMode { No, Yes };
    static PreProcessResult preProcess(const MetaType &classDef, PopulateMode populateMode);
    void addRelatedTypes();

    void sortTypes(QVector<MetaType> &types);
    QString resolvedInclude(QAnyStringView include);
    void processTypes(const QCborMap &types);
    void processForeignTypes(const QCborMap &types);

    bool isPrimitive(QAnyStringView type) const
    {
        return std::binary_search(m_primitiveTypes.begin(), m_primitiveTypes.end(), type);
    }

    QList<QString> m_includes;
    QList<QAnyStringView> m_referencedTypes;
    QList<QAnyStringView> m_primitiveTypes;
    QVector<MetaType> m_types;
    QVector<MetaType> m_foreignTypes;
    bool m_privateIncludes = false;
};

QT_END_NAMESPACE

#endif // METATYPESJSONPROCESSOR_P_H