File: xmlspecparser.cpp

package info (click to toggle)
qtbase-opensource-src 5.11.3+dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 312,420 kB
  • sloc: cpp: 1,831,913; ansic: 318,856; xml: 113,585; python: 9,691; java: 7,209; asm: 4,023; perl: 2,151; sh: 1,790; yacc: 1,733; lex: 1,197; makefile: 301; objc: 70
file content (427 lines) | stat: -rw-r--r-- 13,280 bytes parent folder | download | duplicates (5)
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/***************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB)
** Contact: https://www.qt.io/licensing/
**
** This file is part of the utilities 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 "xmlspecparser.h"

#include <QDebug>
#include <QFile>
#include <QRegularExpression>
#include <QStringList>
#include <QTextStream>
#include <QXmlStreamReader>

#ifdef SPECPARSER_DEBUG
#define qXmlSpecParserDebug qDebug
#else
#define qXmlSpecParserDebug QT_NO_QDEBUG_MACRO
#endif

bool XmlSpecParser::parse()
{
    // Open up a stream on the actual OpenGL function spec file
    QFile file(specFileName());
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        qWarning() << "Failed to open spec file:" << specFileName() << "Aborting";
        return false;
    }

    QXmlStreamReader stream(&file);

    // Extract the info that we need
    parseFunctions(stream);
    return true;
}

void XmlSpecParser::parseParam(QXmlStreamReader &stream, Function &func)
{
    Argument arg;
    arg.type = QString();

    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "ptype") {
                if (stream.readNext() == QXmlStreamReader::Characters)
                    arg.type.append(stream.text().toString());
            }

            else if (tag == "name") {
                if (stream.readNext() == QXmlStreamReader::Characters)
                    arg.name = stream.text().toString().trimmed();
            }
        } else if (stream.isCharacters()) {
            arg.type.append(stream.text().toString());
        } else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "param") {
                // compatibility with old spec
                QRegularExpression typeRegExp("(const )?(.+)(?<!\\*)((?:(?!\\*$)\\*)*)(\\*)?");

                // remove extra whitespace
                arg.type = arg.type.trimmed();

                // set default
                arg.direction = Argument::In;
                arg.mode = Argument::Value;

                QRegularExpressionMatch exp = typeRegExp.match(arg.type);
                if (exp.hasMatch()) {
                    if (!exp.captured(4).isEmpty()) {
                        arg.mode = Argument::Reference;

                        if (exp.captured(1).isEmpty())
                            arg.direction = Argument::Out;
                    }

                    arg.type = exp.captured(2) + exp.captured(3);
                }

                break;
            }
        }
    }

    // remove any excess whitespace
    arg.type = arg.type.trimmed();
    arg.name = arg.name.trimmed();

    // maybe some checks?
    func.arguments.append(arg);
}

void XmlSpecParser::parseCommand(QXmlStreamReader &stream)
{
    Function func;

    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "proto") {
                while (!stream.isEndDocument()) {
                    stream.readNext();
                    if (stream.isStartElement() && (stream.name().toString() == "name")) {
                        if (stream.readNext() == QXmlStreamReader::Characters)
                            func.name = stream.text().toString();
                    } else if (stream.isCharacters()) {
                        func.returnType.append(stream.text().toString());
                    } else if (stream.isEndElement() && (stream.name().toString() == "proto")) {
                        break;
                    }
                }
            }

            if (tag == "param")
                parseParam(stream, func);
        }

        else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "command")
                break;
        }
    }

    // maybe checks?
    func.returnType = func.returnType.trimmed();

    // for compatibility with old spec
    if (func.name.startsWith("gl"))
        func.name.remove(0, 2);

    m_functionList.insert(func.name, func);
}

void XmlSpecParser::parseCommands(QXmlStreamReader &stream)
{
    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "command")
                parseCommand(stream);
        }

        else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "commands")
                break;
        }
    }
}

void XmlSpecParser::parseRequire(QXmlStreamReader &stream, FunctionList &funcs)
{
    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "command") {
                QString func = stream.attributes().value("name").toString();

                // for compatibility with old spec
                if (func.startsWith("gl"))
                    func.remove(0, 2);

                funcs.append(m_functionList[func]);
            }
        } else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "require")
                break;
        }
    }
}

void XmlSpecParser::parseRemoveCore(QXmlStreamReader &stream)
{
    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "command") {
                QString func = stream.attributes().value("name").toString();

                // for compatibility with old spec
                if (func.startsWith("gl"))
                    func.remove(0, 2);

                // go through list of version and mark as incompatible
                Q_FOREACH (const Version& v, m_versions) {
                    // Combine version and profile for this subset of functions
                    VersionProfile version;
                    version.version = v;
                    version.profile = VersionProfile::CoreProfile;

                    // Fetch the functions and add to collection for this class
                    Q_FOREACH (const Function& f, m_functions.values(version)) {
                        if (f.name == func) {
                            VersionProfile newVersion;
                            newVersion.version = v;
                            newVersion.profile = VersionProfile::CompatibilityProfile;

                            m_functions.insert(newVersion, f);
                            m_functions.remove(version, f);
                        }
                    }
                }
            }
        } else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "remove")
                break;
        }
    }
}

void XmlSpecParser::parseFeature(QXmlStreamReader &stream)
{
    QRegularExpression versionRegExp("(\\d).(\\d)");
    QXmlStreamAttributes attributes = stream.attributes();

    QRegularExpressionMatch match = versionRegExp.match(attributes.value("number").toString());

    if (!match.hasMatch()) {
        qWarning() << "Malformed version indicator";
        return;
    }

    if (attributes.value("api").toString() != "gl")
        return;

    int majorVersion = match.captured(1).toInt();
    int minorVersion = match.captured(2).toInt();

    Version v;
    VersionProfile core, compat;

    v.major = majorVersion;
    v.minor = minorVersion;
    core.version = compat.version = v;
    core.profile = VersionProfile::CoreProfile;
    compat.profile = VersionProfile::CompatibilityProfile;

    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "require") {
                FunctionList funcs;

                if (stream.attributes().value("profile").toString() == "compatibility") {
                    parseRequire(stream, funcs);
                    Q_FOREACH (const Function& f, funcs) {
                        m_functions.insert(compat, f);
                    }
                } else {
                    parseRequire(stream, funcs);
                    Q_FOREACH (const Function& f, funcs) {
                        m_functions.insert(core, f);
                    }
                }
            } else if (tag == "remove") {
                if (stream.attributes().value("profile").toString() == "core")
                    parseRemoveCore(stream);
            }
        } else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "feature")
                break;
        }
    }

    m_versions.append(v);
}

void XmlSpecParser::parseExtension(QXmlStreamReader &stream)
{
    QXmlStreamAttributes attributes = stream.attributes();
    QString name = attributes.value("name").toString();

    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "require") {
                if (stream.attributes().value("profile").toString() == "compatibility") {
                    FunctionList funcs;
                    parseRequire(stream, funcs);

                    Q_FOREACH (const Function& f, funcs) {
                        FunctionProfile fp;
                        fp.function = f;
                        fp.profile = VersionProfile::CompatibilityProfile;
                        m_extensionFunctions.insert(name, fp);
                    }
                } else {
                    FunctionList funcs;
                    parseRequire(stream, funcs);
                    Q_FOREACH (const Function& f, funcs) {
                        FunctionProfile fp;
                        fp.function = f;
                        fp.profile = VersionProfile::CoreProfile;
                        m_extensionFunctions.insert(name, fp);
                    }
                }


            }
        } else if (stream.isEndElement()) {
            QString tag = stream.name().toString();

            if (tag == "extension")
                break;
        }
    }
}

void XmlSpecParser::parseFunctions(QXmlStreamReader &stream)
{
    while (!stream.isEndDocument()) {
        stream.readNext();

        if (stream.isStartElement()) {
            QString tag = stream.name().toString();

            if (tag == "feature") {
                parseFeature(stream);
            } else if (tag == "commands") {
                parseCommands(stream);
            } else if (tag == "extension") {
                parseExtension(stream);
            }
        } else if (stream.isEndElement()) {
            stream.readNext();
        }
    }

    // hack - add GL_ARB_imaging to every version after 1.2 inclusive
    Version versionThreshold;
    versionThreshold.major = 1;
    versionThreshold.minor = 2;
    QList<FunctionProfile> funcs = m_extensionFunctions.values("GL_ARB_imaging");

    VersionProfile vp;
    vp.version = versionThreshold;

    Q_FOREACH (const FunctionProfile& fp, funcs) {
        vp.profile = fp.profile;
        m_functions.insert(vp, fp.function);
    }

    // now we will prune any duplicates
    QSet<QString> funcset;

    Q_FOREACH (const Version& v, m_versions) {
        // check compatibility first
        VersionProfile vp;
        vp.version = v;

        vp.profile = VersionProfile::CompatibilityProfile;

        Q_FOREACH (const Function& f, m_functions.values(vp)) {
            // remove duplicate
            if (funcset.contains(f.name))
                m_functions.remove(vp, f);

            funcset.insert(f.name);
        }

        vp.profile = VersionProfile::CoreProfile;

        Q_FOREACH (const Function& f, m_functions.values(vp)) {

            // remove duplicate
            if (funcset.contains(f.name))
                m_functions.remove(vp, f);

            funcset.insert(f.name);
        }
    }
}