67ad051 by Lars Knoll at 2009-03-23 1
/****************************************************************************
2
**
ac5c099 by Jason McDonald at 2011-01-10 3
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
04e3b30 by Jason McDonald at 2009-09-09 4
** All rights reserved.
858c70f by Jason McDonald at 2009-06-16 5
** Contact: Nokia Corporation (qt-info@nokia.com)
67ad051 by Lars Knoll at 2009-03-23 6
**
7
** This file is part of the tools applications of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
8e6e571 by Jyri Tahtela at 2011-05-13 11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
67ad051 by Lars Knoll at 2009-03-23 17
**
04e3b30 by Jason McDonald at 2009-09-09 18
** In addition, as a special exception, Nokia gives you certain additional
8e6e571 by Jyri Tahtela at 2011-05-13 19
** rights. These rights are described in the Nokia Qt LGPL Exception
04e3b30 by Jason McDonald at 2009-09-09 20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
67ad051 by Lars Knoll at 2009-03-23 21
**
8e6e571 by Jyri Tahtela at 2011-05-13 22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
309db73 by Jason McDonald at 2009-08-31 33
**
34
**
35
**
36
**
67ad051 by Lars Knoll at 2009-03-23 37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "generator.h"
43
#include "outputrevision.h"
44
#include "utils.h"
45
#include <QtCore/qmetatype.h>
46
#include <stdio.h>
47
919b723 by Olivier Goffart at 2009-08-19 48
#include <private/qmetaobject_p.h> //for the flags.
67ad051 by Lars Knoll at 2009-03-23 49
919b723 by Olivier Goffart at 2009-08-19 50
QT_BEGIN_NAMESPACE
67ad051 by Lars Knoll at 2009-03-23 51
52
uint qvariant_nameToType(const char* name)
53
{
54
    if (!name)
55
        return 0;
56
57
    if (strcmp(name, "QVariant") == 0)
58
        return 0xffffffff;
59
    if (strcmp(name, "QCString") == 0)
60
        return QMetaType::QByteArray;
61
    if (strcmp(name, "Q_LLONG") == 0)
62
        return QMetaType::LongLong;
63
    if (strcmp(name, "Q_ULLONG") == 0)
64
        return QMetaType::ULongLong;
65
    if (strcmp(name, "QIconSet") == 0)
66
        return QMetaType::QIcon;
67
68
    uint tp = QMetaType::type(name);
69
    return tp < QMetaType::User ? tp : 0;
70
}
71
72
/*
73
  Returns true if the type is a QVariant types.
74
*/
75
bool isVariantType(const char* type)
76
{
77
    return qvariant_nameToType(type) != 0;
78
}
79
4acabb3 by Kent Hansen at 2009-06-05 80
/*!
81
  Returns true if the type is qreal.
82
*/
83
static bool isQRealType(const char *type)
84
{
85
    return strcmp(type, "qreal") == 0;
86
}
87
67ad051 by Lars Knoll at 2009-03-23 88
Generator::Generator(ClassDef *classDef, const QList<QByteArray> &metaTypes, FILE *outfile)
89
    : out(outfile), cdef(classDef), metaTypes(metaTypes)
90
{
91
    if (cdef->superclassList.size())
92
        purestSuperClass = cdef->superclassList.first().first;
93
}
94
95
static inline int lengthOfEscapeSequence(const QByteArray &s, int i)
96
{
97
    if (s.at(i) != '\\' || i >= s.length() - 1)
98
        return 1;
99
    const int startPos = i;
100
    ++i;
101
    char ch = s.at(i);
102
    if (ch == 'x') {
103
        ++i;
104
        while (i < s.length() && is_hex_char(s.at(i)))
105
            ++i;
106
    } else if (is_octal_char(ch)) {
107
        while (i < startPos + 4
108
               && i < s.length()
109
               && is_octal_char(s.at(i))) {
110
            ++i;
111
        }
112
    } else { // single character escape sequence
113
        i = qMin(i + 1, s.length());
114
    }
115
    return i - startPos;
116
}
117
118
int Generator::strreg(const char *s)
119
{
120
    int idx = 0;
121
    if (!s)
122
        s = "";
123
    for (int i = 0; i < strings.size(); ++i) {
124
        const QByteArray &str = strings.at(i);
125
        if (str == s)
126
            return idx;
127
        idx += str.length() + 1;
128
        for (int i = 0; i < str.length(); ++i) {
129
            if (str.at(i) == '\\') {
130
                int cnt = lengthOfEscapeSequence(str, i) - 1;
131
                idx -= cnt;
132
                i += cnt;
133
            }
134
        }
135
    }
136
    strings.append(s);
137
    return idx;
138
}
139
140
void Generator::generateCode()
141
{
142
    bool isQt = (cdef->classname == "Qt");
143
    bool isQObject = (cdef->classname == "QObject");
144
    bool isConstructible = !cdef->constructorList.isEmpty();
145
146
//
147
// build the data array
148
//
149
    int i = 0;
150
151
152
    // filter out undeclared enumerators and sets
153
    {
154
        QList<EnumDef> enumList;
155
        for (i = 0; i < cdef->enumList.count(); ++i) {
156
            EnumDef def = cdef->enumList.at(i);
157
            if (cdef->enumDeclarations.contains(def.name)) {
158
                enumList += def;
159
            }
160
            QByteArray alias = cdef->flagAliases.value(def.name);
161
            if (cdef->enumDeclarations.contains(alias)) {
162
                def.name = alias;
163
                enumList += def;
164
            }
165
        }
166
        cdef->enumList = enumList;
167
    }
168
169
170
    QByteArray qualifiedClassNameIdentifier = cdef->qualified;
171
    qualifiedClassNameIdentifier.replace(':', '_');
172
919b723 by Olivier Goffart at 2009-08-19 173
    int index = 14;
67ad051 by Lars Knoll at 2009-03-23 174
    fprintf(out, "static const uint qt_meta_data_%s[] = {\n", qualifiedClassNameIdentifier.constData());
175
    fprintf(out, "\n // content:\n");
b881d8f by Bradley T. Hughes at 2009-11-30 176
    fprintf(out, "    %4d,       // revision\n", 5);
67ad051 by Lars Knoll at 2009-03-23 177
    fprintf(out, "    %4d,       // classname\n", strreg(cdef->qualified));
178
    fprintf(out, "    %4d, %4d, // classinfo\n", cdef->classInfoList.count(), cdef->classInfoList.count() ? index : 0);
179
    index += cdef->classInfoList.count() * 2;
180
181
    int methodCount = cdef->signalList.count() + cdef->slotList.count() + cdef->methodList.count();
182
    fprintf(out, "    %4d, %4d, // methods\n", methodCount, methodCount ? index : 0);
183
    index += methodCount * 5;
3f80a24 by Martin Jones at 2011-01-05 184
    if (cdef->revisionedMethods)
185
        index += methodCount;
67ad051 by Lars Knoll at 2009-03-23 186
    fprintf(out, "    %4d, %4d, // properties\n", cdef->propertyList.count(), cdef->propertyList.count() ? index : 0);
187
    index += cdef->propertyList.count() * 3;
188
    if(cdef->notifyableProperties)
189
        index += cdef->propertyList.count();
3f80a24 by Martin Jones at 2011-01-05 190
    if (cdef->revisionedProperties)
191
        index += cdef->propertyList.count();
67ad051 by Lars Knoll at 2009-03-23 192
    fprintf(out, "    %4d, %4d, // enums/sets\n", cdef->enumList.count(), cdef->enumList.count() ? index : 0);
193
194
    int enumsIndex = index;
195
    for (i = 0; i < cdef->enumList.count(); ++i)
196
        index += 4 + (cdef->enumList.at(i).values.count() * 2);
197
    fprintf(out, "    %4d, %4d, // constructors\n", isConstructible ? cdef->constructorList.count() : 0,
198
            isConstructible ? index : 0);
199
f858dd8 by Aaron Kennedy at 2009-07-31 200
    fprintf(out, "    %4d,       // flags\n", 0);
919b723 by Olivier Goffart at 2009-08-19 201
    fprintf(out, "    %4d,       // signalCount\n", cdef->signalList.count());
f858dd8 by Aaron Kennedy at 2009-07-31 202
203
67ad051 by Lars Knoll at 2009-03-23 204
//
205
// Build classinfo array
206
//
207
    generateClassInfos();
208
209
//
210
// Build signals array first, otherwise the signal indices would be wrong
211
//
212
    generateFunctions(cdef->signalList, "signal", MethodSignal);
213
214
//
215
// Build slots array
216
//
217
    generateFunctions(cdef->slotList, "slot", MethodSlot);
218
219
//
220
// Build method array
221
//
222
    generateFunctions(cdef->methodList, "method", MethodMethod);
223
3f80a24 by Martin Jones at 2011-01-05 224
//
225
// Build method version arrays
226
//
227
    if (cdef->revisionedMethods) {
228
        generateFunctionRevisions(cdef->signalList, "signal");
229
        generateFunctionRevisions(cdef->slotList, "slot");
230
        generateFunctionRevisions(cdef->methodList, "method");
231
    }
67ad051 by Lars Knoll at 2009-03-23 232
233
//
234
// Build property array
235
//
236
    generateProperties();
237
238
//
239
// Build enums array
240
//
241
    generateEnums(enumsIndex);
242
243
//
244
// Build constructors array
245
//
246
    if (isConstructible)
247
        generateFunctions(cdef->constructorList, "constructor", MethodConstructor);
248
249
//
250
// Terminate data array
251
//
252
    fprintf(out, "\n       0        // eod\n};\n\n");
253
254
//
255
// Build stringdata array
256
//
257
    fprintf(out, "static const char qt_meta_stringdata_%s[] = {\n", qualifiedClassNameIdentifier.constData());
258
    fprintf(out, "    \"");
259
    int col = 0;
260
    int len = 0;
261
    for (i = 0; i < strings.size(); ++i) {
262
        QByteArray s = strings.at(i);
263
        len = s.length();
264
        if (col && col + len >= 72) {
265
            fprintf(out, "\"\n    \"");
266
            col = 0;
267
        } else if (len && s.at(0) >= '0' && s.at(0) <= '9') {
268
            fprintf(out, "\"\"");
269
            len += 2;
270
        }
271
        int idx = 0;
272
        while (idx < s.length()) {
273
            if (idx > 0) {
274
                col = 0;
275
                fprintf(out, "\"\n    \"");
276
            }
277
            int spanLen = qMin(70, s.length() - idx);
278
            // don't cut escape sequences at the end of a line
279
            int backSlashPos = s.lastIndexOf('\\', idx + spanLen - 1);
280
            if (backSlashPos >= idx) {
281
                int escapeLen = lengthOfEscapeSequence(s, backSlashPos);
282
                spanLen = qBound(spanLen, backSlashPos + escapeLen - idx, s.length() - idx);
283
            }
284
            fwrite(s.constData() + idx, 1, spanLen, out);
285
            idx += spanLen;
286
            col += spanLen;
287
        }
288
289
        fputs("\\0", out);
290
        col += len + 2;
291
    }
292
    fprintf(out, "\"\n};\n\n");
293
294
295
//
296
// Generate internal qt_static_metacall() function
297
//
298
    if (isConstructible)
299
        generateStaticMetacall(qualifiedClassNameIdentifier);
300
301
//
302
// Build extra array
303
//
304
    QList<QByteArray> extraList;
305
    for (int i = 0; i < cdef->propertyList.count(); ++i) {
306
        const PropertyDef &p = cdef->propertyList.at(i);
0d09c6b by Olivier Goffart at 2009-11-30 307
        if (!isVariantType(p.type) && !metaTypes.contains(p.type) && !p.type.contains('*') &&
308
                !p.type.contains('<') && !p.type.contains('>')) {
67ad051 by Lars Knoll at 2009-03-23 309
            int s = p.type.lastIndexOf("::");
310
            if (s > 0) {
311
                QByteArray scope = p.type.left(s);
312
                if (scope != "Qt" && scope != cdef->classname && !extraList.contains(scope))
313
                    extraList += scope;
314
            }
315
        }
316
    }
317
    if (!extraList.isEmpty()) {
8d0b487 by Shane Kearns at 2009-08-28 318
        fprintf(out, "#ifdef Q_NO_DATA_RELOCATION\n");
319
        fprintf(out, "static const QMetaObjectAccessor qt_meta_extradata_%s[] = {\n    ", qualifiedClassNameIdentifier.constData());
320
        for (int i = 0; i < extraList.count(); ++i) {
321
            fprintf(out, "    %s::getStaticMetaObject,\n", extraList.at(i).constData());
322
        }
323
        fprintf(out, "#else\n");
67ad051 by Lars Knoll at 2009-03-23 324
        fprintf(out, "static const QMetaObject *qt_meta_extradata_%s[] = {\n    ", qualifiedClassNameIdentifier.constData());
325
        for (int i = 0; i < extraList.count(); ++i) {
8d0b487 by Shane Kearns at 2009-08-28 326
            fprintf(out, "    &%s::staticMetaObject,\n", extraList.at(i).constData());
67ad051 by Lars Knoll at 2009-03-23 327
        }
8d0b487 by Shane Kearns at 2009-08-28 328
        fprintf(out, "#endif //Q_NO_DATA_RELOCATION\n");
329
        fprintf(out, "    0\n};\n\n");
67ad051 by Lars Knoll at 2009-03-23 330
    }
331
332
    if (isConstructible || !extraList.isEmpty()) {
333
        fprintf(out, "static const QMetaObjectExtraData qt_meta_extradata2_%s = {\n    ",
334
                qualifiedClassNameIdentifier.constData());
335
        if (extraList.isEmpty())
336
            fprintf(out, "0, ");
337
        else
338
            fprintf(out, "qt_meta_extradata_%s, ", qualifiedClassNameIdentifier.constData());
339
        if (!isConstructible)
340
            fprintf(out, "0");
341
        else
342
            fprintf(out, "%s_qt_static_metacall", qualifiedClassNameIdentifier.constData());
343
        fprintf(out, " \n};\n\n");
344
    }
345
346
//
347
// Finally create and initialize the static meta object
348
//
349
    if (isQt)
350
        fprintf(out, "const QMetaObject QObject::staticQtMetaObject = {\n");
351
    else
352
        fprintf(out, "const QMetaObject %s::staticMetaObject = {\n", cdef->qualified.constData());
353
354
    if (isQObject)
355
        fprintf(out, "    { 0, ");
356
    else if (cdef->superclassList.size())
357
        fprintf(out, "    { &%s::staticMetaObject, ", purestSuperClass.constData());
358
    else
359
        fprintf(out, "    { 0, ");
360
    fprintf(out, "qt_meta_stringdata_%s,\n      qt_meta_data_%s, ",
361
             qualifiedClassNameIdentifier.constData(), qualifiedClassNameIdentifier.constData());
362
    if (!isConstructible && extraList.isEmpty())
363
        fprintf(out, "0 }\n");
364
    else
365
        fprintf(out, "&qt_meta_extradata2_%s }\n", qualifiedClassNameIdentifier.constData());
366
    fprintf(out, "};\n");
367
8d0b487 by Shane Kearns at 2009-08-28 368
    if(isQt)
369
        return;
370
371
//
372
// Generate static meta object accessor (needed for symbian, because DLLs do not support data imports.
373
//
374
    fprintf(out, "\n#ifdef Q_NO_DATA_RELOCATION\n");
375
    fprintf(out, "const QMetaObject &%s::getStaticMetaObject() { return staticMetaObject; }\n", cdef->qualified.constData());
376
    fprintf(out, "#endif //Q_NO_DATA_RELOCATION\n");
377
378
    if (!cdef->hasQObject)
67ad051 by Lars Knoll at 2009-03-23 379
        return;
380
f858dd8 by Aaron Kennedy at 2009-07-31 381
    fprintf(out, "\nconst QMetaObject *%s::metaObject() const\n{\n    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;\n}\n",
67ad051 by Lars Knoll at 2009-03-23 382
            cdef->qualified.constData());
8d0b487 by Shane Kearns at 2009-08-28 383
67ad051 by Lars Knoll at 2009-03-23 384
//
385
// Generate smart cast function
386
//
387
    fprintf(out, "\nvoid *%s::qt_metacast(const char *_clname)\n{\n", cdef->qualified.constData());
388
    fprintf(out, "    if (!_clname) return 0;\n");
389
    fprintf(out, "    if (!strcmp(_clname, qt_meta_stringdata_%s))\n"
390
                  "        return static_cast<void*>(const_cast< %s*>(this));\n",
391
            qualifiedClassNameIdentifier.constData(), cdef->classname.constData());
392
    for (int i = 1; i < cdef->superclassList.size(); ++i) { // for all superclasses but the first one
393
        if (cdef->superclassList.at(i).second == FunctionDef::Private)
394
            continue;
395
        const char *cname = cdef->superclassList.at(i).first;
396
        fprintf(out, "    if (!strcmp(_clname, \"%s\"))\n        return static_cast< %s*>(const_cast< %s*>(this));\n",
397
                cname, cname, cdef->classname.constData());
398
    }
399
    for (int i = 0; i < cdef->interfaceList.size(); ++i) {
400
        const QList<ClassDef::Interface> &iface = cdef->interfaceList.at(i);
401
        for (int j = 0; j < iface.size(); ++j) {
402
            fprintf(out, "    if (!strcmp(_clname, %s))\n        return ", iface.at(j).interfaceId.constData());
403
            for (int k = j; k >= 0; --k)
404
                fprintf(out, "static_cast< %s*>(", iface.at(k).className.constData());
405
            fprintf(out, "const_cast< %s*>(this)%s;\n",
406
                    cdef->classname.constData(), QByteArray(j+1, ')').constData());
407
        }
408
    }
409
    if (!purestSuperClass.isEmpty() && !isQObject) {
410
        QByteArray superClass = purestSuperClass;
411
        // workaround for VC6
412
        if (superClass.contains("::")) {
413
            fprintf(out, "    typedef %s QMocSuperClass;\n", superClass.constData());
414
            superClass = "QMocSuperClass";
415
        }
416
        fprintf(out, "    return %s::qt_metacast(_clname);\n", superClass.constData());
417
    } else {
418
        fprintf(out, "    return 0;\n");
419
    }
420
    fprintf(out, "}\n");
421
422
//
423
// Generate internal qt_metacall()  function
424
//
425
    generateMetacall();
426
427
//
428
// Generate internal signal functions
429
//
430
    for (int signalindex = 0; signalindex < cdef->signalList.size(); ++signalindex)
431
        generateSignal(&cdef->signalList[signalindex], signalindex);
432
}
433
434
435
void Generator::generateClassInfos()
436
{
437
    if (cdef->classInfoList.isEmpty())
438
        return;
439
440
    fprintf(out, "\n // classinfo: key, value\n");
441
442
    for (int i = 0; i < cdef->classInfoList.size(); ++i) {
443
        const ClassInfoDef &c = cdef->classInfoList.at(i);
444
        fprintf(out, "    %4d, %4d,\n", strreg(c.name), strreg(c.value));
445
    }
446
}
447
448
void Generator::generateFunctions(QList<FunctionDef>& list, const char *functype, int type)
449
{
450
    if (list.isEmpty())
451
        return;
452
    fprintf(out, "\n // %ss: signature, parameters, type, tag, flags\n", functype);
453
454
    for (int i = 0; i < list.count(); ++i) {
455
        const FunctionDef &f = list.at(i);
456
457
        QByteArray sig = f.name + '(';
458
        QByteArray arguments;
459
460
        for (int j = 0; j < f.arguments.count(); ++j) {
461
            const ArgumentDef &a = f.arguments.at(j);
462
            if (j) {
463
                sig += ",";
464
                arguments += ",";
465
            }
466
            sig += a.normalizedType;
467
            arguments += a.name;
468
        }
469
        sig += ')';
470
3f80a24 by Martin Jones at 2011-01-05 471
        unsigned char flags = type;
67ad051 by Lars Knoll at 2009-03-23 472
        if (f.access == FunctionDef::Private)
473
            flags |= AccessPrivate;
474
        else if (f.access == FunctionDef::Public)
475
            flags |= AccessPublic;
476
        else if (f.access == FunctionDef::Protected)
477
            flags |= AccessProtected;
478
        if (f.access == FunctionDef::Private)
479
            flags |= AccessPrivate;
480
        else if (f.access == FunctionDef::Public)
481
            flags |= AccessPublic;
482
        else if (f.access == FunctionDef::Protected)
483
            flags |= AccessProtected;
484
        if (f.isCompat)
485
            flags |= MethodCompatibility;
486
        if (f.wasCloned)
487
            flags |= MethodCloned;
488
        if (f.isScriptable)
489
            flags |= MethodScriptable;
3f80a24 by Martin Jones at 2011-01-05 490
        if (f.revision > 0)
491
            flags |= MethodRevisioned;
67ad051 by Lars Knoll at 2009-03-23 492
        fprintf(out, "    %4d, %4d, %4d, %4d, 0x%02x,\n", strreg(sig),
493
                strreg(arguments), strreg(f.normalizedType), strreg(f.tag), flags);
494
    }
495
}
496
3f80a24 by Martin Jones at 2011-01-05 497
void Generator::generateFunctionRevisions(QList<FunctionDef>& list, const char *functype)
498
{
499
    if (list.count())
500
        fprintf(out, "\n // %ss: revision\n", functype);
501
    for (int i = 0; i < list.count(); ++i) {
502
        const FunctionDef &f = list.at(i);
503
        fprintf(out, "    %4d,\n", f.revision);
504
    }
505
}
506
67ad051 by Lars Knoll at 2009-03-23 507
void Generator::generateProperties()
508
{
509
    //
510
    // Create meta data
511
    //
512
513
    if (cdef->propertyList.count())
514
        fprintf(out, "\n // properties: name, type, flags\n");
515
    for (int i = 0; i < cdef->propertyList.count(); ++i) {
516
        const PropertyDef &p = cdef->propertyList.at(i);
517
        uint flags = Invalid;
518
        if (!isVariantType(p.type)) {
519
            flags |= EnumOrFlag;
4acabb3 by Kent Hansen at 2009-06-05 520
        } else if (!isQRealType(p.type)) {
67ad051 by Lars Knoll at 2009-03-23 521
            flags |= qvariant_nameToType(p.type) << 24;
522
        }
523
        if (!p.read.isEmpty())
524
            flags |= Readable;
525
        if (!p.write.isEmpty()) {
526
            flags |= Writable;
527
            if (p.stdCppSet())
528
                flags |= StdCppSet;
529
        }
530
        if (!p.reset.isEmpty())
531
            flags |= Resettable;
532
533
//         if (p.override)
534
//             flags |= Override;
535
536
        if (p.designable.isEmpty())
537
            flags |= ResolveDesignable;
538
        else if (p.designable != "false")
539
            flags |= Designable;
540
541
        if (p.scriptable.isEmpty())
542
            flags |= ResolveScriptable;
543
        else if (p.scriptable != "false")
544
            flags |= Scriptable;
545
546
        if (p.stored.isEmpty())
547
            flags |= ResolveStored;
548
        else if (p.stored != "false")
549
            flags |= Stored;
550
551
        if (p.editable.isEmpty())
552
            flags |= ResolveEditable;
553
        else if (p.editable != "false")
554
            flags |= Editable;
555
556
        if (p.user.isEmpty())
557
            flags |= ResolveUser;
558
        else if (p.user != "false")
559
            flags |= User;
560
561
        if (p.notifyId != -1)
562
            flags |= Notify;
563
3f80a24 by Martin Jones at 2011-01-05 564
        if (p.revision > 0)
565
            flags |= Revisioned;
566
29202f0 by Aaron Kennedy at 2009-07-31 567
        if (p.constant)
568
            flags |= Constant;
7ebd202 by Aaron Kennedy at 2009-07-31 569
        if (p.final)
570
            flags |= Final;
29202f0 by Aaron Kennedy at 2009-07-31 571
4acabb3 by Kent Hansen at 2009-06-05 572
        fprintf(out, "    %4d, %4d, ",
573
                strreg(p.name),
574
                strreg(p.type));
575
        if (!(flags >> 24) && isQRealType(p.type))
576
            fprintf(out, "(QMetaType::QReal << 24) | ");
577
        fprintf(out, "0x%.8x,\n", flags);
67ad051 by Lars Knoll at 2009-03-23 578
    }
579
580
    if(cdef->notifyableProperties) {
581
        fprintf(out, "\n // properties: notify_signal_id\n");
582
        for (int i = 0; i < cdef->propertyList.count(); ++i) {
583
            const PropertyDef &p = cdef->propertyList.at(i);
584
            if(p.notifyId == -1)
585
                fprintf(out, "    %4d,\n",
586
                        0);
587
            else
588
                fprintf(out, "    %4d,\n",
589
                        p.notifyId);
590
        }
591
    }
3f80a24 by Martin Jones at 2011-01-05 592
    if (cdef->revisionedProperties) {
593
        fprintf(out, "\n // properties: revision\n");
594
        for (int i = 0; i < cdef->propertyList.count(); ++i) {
595
            const PropertyDef &p = cdef->propertyList.at(i);
596
            fprintf(out, "    %4d,\n", p.revision);
597
        }
598
    }
67ad051 by Lars Knoll at 2009-03-23 599
}
600
601
void Generator::generateEnums(int index)
602
{
603
    if (cdef->enumDeclarations.isEmpty())
604
        return;
605
606
    fprintf(out, "\n // enums: name, flags, count, data\n");
607
    index += 4 * cdef->enumList.count();
608
    int i;
609
    for (i = 0; i < cdef->enumList.count(); ++i) {
610
        const EnumDef &e = cdef->enumList.at(i);
611
        fprintf(out, "    %4d, 0x%.1x, %4d, %4d,\n",
612
                 strreg(e.name),
613
                 cdef->enumDeclarations.value(e.name) ? 1 : 0,
614
                 e.values.count(),
615
                 index);
616
        index += e.values.count() * 2;
617
    }
618
619
    fprintf(out, "\n // enum data: key, value\n");
620
    for (i = 0; i < cdef->enumList.count(); ++i) {
621
        const EnumDef &e = cdef->enumList.at(i);
622
        for (int j = 0; j < e.values.count(); ++j) {
623
            const QByteArray &val = e.values.at(j);
624
            fprintf(out, "    %4d, uint(%s::%s),\n",
625
                    strreg(val),
626
                    cdef->qualified.constData(),
627
                    val.constData());
628
        }
629
    }
630
}
631
632
void Generator::generateMetacall()
633
{
634
    bool isQObject = (cdef->classname == "QObject");
635
636
    fprintf(out, "\nint %s::qt_metacall(QMetaObject::Call _c, int _id, void **_a)\n{\n",
637
             cdef->qualified.constData());
638
639
    if (!purestSuperClass.isEmpty() && !isQObject) {
640
        QByteArray superClass = purestSuperClass;
641
        // workaround for VC6
642
        if (superClass.contains("::")) {
643
            fprintf(out, "    typedef %s QMocSuperClass;\n", superClass.constData());
644
            superClass = "QMocSuperClass";
645
        }
646
        fprintf(out, "    _id = %s::qt_metacall(_c, _id, _a);\n", superClass.constData());
647
    }
648
649
    fprintf(out, "    if (_id < 0)\n        return _id;\n");
650
    fprintf(out, "    ");
651
652
    bool needElse = false;
653
    QList<FunctionDef> methodList;
654
    methodList += cdef->signalList;
655
    methodList += cdef->slotList;
656
    methodList += cdef->methodList;
657
658
    if (methodList.size()) {
659
        needElse = true;
660
        fprintf(out, "if (_c == QMetaObject::InvokeMetaMethod) {\n        ");
661
        fprintf(out, "switch (_id) {\n");
662
        for (int methodindex = 0; methodindex < methodList.size(); ++methodindex) {
663
            const FunctionDef &f = methodList.at(methodindex);
664
            fprintf(out, "        case %d: ", methodindex);
665
            if (f.normalizedType.size())
666
                fprintf(out, "{ %s _r = ", noRef(f.normalizedType).constData());
667
            if (f.inPrivateClass.size())
668
                fprintf(out, "%s->", f.inPrivateClass.constData());
669
            fprintf(out, "%s(", f.name.constData());
670
            int offset = 1;
671
            for (int j = 0; j < f.arguments.count(); ++j) {
672
                const ArgumentDef &a = f.arguments.at(j);
673
                if (j)
674
                    fprintf(out, ",");
675
                fprintf(out, "(*reinterpret_cast< %s>(_a[%d]))",a.typeNameForCast.constData(), offset++);
676
            }
677
            fprintf(out, ");");
678
            if (f.normalizedType.size())
679
                fprintf(out, "\n            if (_a[0]) *reinterpret_cast< %s*>(_a[0]) = _r; } ",
680
                        noRef(f.normalizedType).constData());
681
            fprintf(out, " break;\n");
682
        }
683
        fprintf(out, "        default: ;\n");
684
        fprintf(out, "        }\n");
685
    }
686
    if (methodList.size())
687
        fprintf(out, "        _id -= %d;\n    }", methodList.size());
688
689
    if (cdef->propertyList.size()) {
690
        bool needGet = false;
691
        bool needTempVarForGet = false;
692
        bool needSet = false;
693
        bool needReset = false;
694
        bool needDesignable = false;
695
        bool needScriptable = false;
696
        bool needStored = false;
697
        bool needEditable = false;
698
        bool needUser = false;
699
        for (int i = 0; i < cdef->propertyList.size(); ++i) {
700
            const PropertyDef &p = cdef->propertyList.at(i);
701
            needGet |= !p.read.isEmpty();
702
            if (!p.read.isEmpty())
703
                needTempVarForGet |= (p.gspec != PropertyDef::PointerSpec
704
                                      && p.gspec != PropertyDef::ReferenceSpec);
705
706
            needSet |= !p.write.isEmpty();
707
            needReset |= !p.reset.isEmpty();
708
            needDesignable |= p.designable.endsWith(')');
709
            needScriptable |= p.scriptable.endsWith(')');
710
            needStored |= p.stored.endsWith(')');
711
            needEditable |= p.editable.endsWith(')');
712
            needUser |= p.user.endsWith(')');
713
        }
714
        fprintf(out, "\n#ifndef QT_NO_PROPERTIES\n     ");
715
716
        if (needElse)
717
            fprintf(out, " else ");
718
        fprintf(out, "if (_c == QMetaObject::ReadProperty) {\n");
719
        if (needGet) {
720
            if (needTempVarForGet)
721
                fprintf(out, "        void *_v = _a[0];\n");
722
            fprintf(out, "        switch (_id) {\n");
723
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
724
                const PropertyDef &p = cdef->propertyList.at(propindex);
725
                if (p.read.isEmpty())
726
                    continue;
78e1a86 by Alexis Menard at 2010-03-19 727
                QByteArray prefix;
728
                if (p.inPrivateClass.size()) {
729
                    prefix = p.inPrivateClass;
730
                    prefix.append("->");
731
                }
67ad051 by Lars Knoll at 2009-03-23 732
                if (p.gspec == PropertyDef::PointerSpec)
78e1a86 by Alexis Menard at 2010-03-19 733
                    fprintf(out, "        case %d: _a[0] = const_cast<void*>(reinterpret_cast<const void*>(%s%s())); break;\n",
734
                            propindex, prefix.constData(), p.read.constData());
67ad051 by Lars Knoll at 2009-03-23 735
                else if (p.gspec == PropertyDef::ReferenceSpec)
78e1a86 by Alexis Menard at 2010-03-19 736
                    fprintf(out, "        case %d: _a[0] = const_cast<void*>(reinterpret_cast<const void*>(&%s%s())); break;\n",
737
                            propindex, prefix.constData(), p.read.constData());
67ad051 by Lars Knoll at 2009-03-23 738
                else if (cdef->enumDeclarations.value(p.type, false))
78e1a86 by Alexis Menard at 2010-03-19 739
                    fprintf(out, "        case %d: *reinterpret_cast<int*>(_v) = QFlag(%s%s()); break;\n",
740
                            propindex, prefix.constData(), p.read.constData());
67ad051 by Lars Knoll at 2009-03-23 741
                else
78e1a86 by Alexis Menard at 2010-03-19 742
                    fprintf(out, "        case %d: *reinterpret_cast< %s*>(_v) = %s%s(); break;\n",
743
                            propindex, p.type.constData(), prefix.constData(), p.read.constData());
67ad051 by Lars Knoll at 2009-03-23 744
            }
745
            fprintf(out, "        }\n");
746
        }
747
748
        fprintf(out,
749
                "        _id -= %d;\n"
750
                "    }", cdef->propertyList.count());
751
752
        fprintf(out, " else ");
753
        fprintf(out, "if (_c == QMetaObject::WriteProperty) {\n");
754
755
        if (needSet) {
756
            fprintf(out, "        void *_v = _a[0];\n");
757
            fprintf(out, "        switch (_id) {\n");
758
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
759
                const PropertyDef &p = cdef->propertyList.at(propindex);
760
                if (p.write.isEmpty())
761
                    continue;
78e1a86 by Alexis Menard at 2010-03-19 762
                QByteArray prefix;
763
                if (p.inPrivateClass.size()) {
764
                    prefix = p.inPrivateClass;
765
                    prefix.append("->");
766
                }
67ad051 by Lars Knoll at 2009-03-23 767
                if (cdef->enumDeclarations.value(p.type, false)) {
78e1a86 by Alexis Menard at 2010-03-19 768
                    fprintf(out, "        case %d: %s%s(QFlag(*reinterpret_cast<int*>(_v))); break;\n",
769
                            propindex, prefix.constData(), p.write.constData());
67ad051 by Lars Knoll at 2009-03-23 770
                } else {
78e1a86 by Alexis Menard at 2010-03-19 771
                    fprintf(out, "        case %d: %s%s(*reinterpret_cast< %s*>(_v)); break;\n",
772
                            propindex, prefix.constData(), p.write.constData(), p.type.constData());
67ad051 by Lars Knoll at 2009-03-23 773
                }
774
            }
775
            fprintf(out, "        }\n");
776
        }
777
778
        fprintf(out,
779
                "        _id -= %d;\n"
780
                "    }", cdef->propertyList.count());
781
782
        fprintf(out, " else ");
783
        fprintf(out, "if (_c == QMetaObject::ResetProperty) {\n");
784
        if (needReset) {
785
            fprintf(out, "        switch (_id) {\n");
786
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
787
                const PropertyDef &p = cdef->propertyList.at(propindex);
788
                if (!p.reset.endsWith(')'))
789
                    continue;
78e1a86 by Alexis Menard at 2010-03-19 790
                QByteArray prefix;
791
                if (p.inPrivateClass.size()) {
792
                    prefix = p.inPrivateClass;
793
                    prefix.append("->");
794
                }
795
                fprintf(out, "        case %d: %s%s; break;\n",
796
                        propindex, prefix.constData(), p.reset.constData());
67ad051 by Lars Knoll at 2009-03-23 797
            }
798
            fprintf(out, "        }\n");
799
        }
800
        fprintf(out,
801
                "        _id -= %d;\n"
802
                "    }", cdef->propertyList.count());
803
804
        fprintf(out, " else ");
805
        fprintf(out, "if (_c == QMetaObject::QueryPropertyDesignable) {\n");
806
        if (needDesignable) {
807
            fprintf(out, "        bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
808
            fprintf(out, "        switch (_id) {\n");
809
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
810
                const PropertyDef &p = cdef->propertyList.at(propindex);
811
                if (!p.designable.endsWith(')'))
812
                    continue;
813
                fprintf(out, "        case %d: *_b = %s; break;\n",
814
                         propindex, p.designable.constData());
815
            }
816
            fprintf(out, "        }\n");
817
        }
818
        fprintf(out,
819
                "        _id -= %d;\n"
820
                "    }", cdef->propertyList.count());
821
822
        fprintf(out, " else ");
823
        fprintf(out, "if (_c == QMetaObject::QueryPropertyScriptable) {\n");
824
        if (needScriptable) {
825
            fprintf(out, "        bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
826
            fprintf(out, "        switch (_id) {\n");
827
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
828
                const PropertyDef &p = cdef->propertyList.at(propindex);
829
                if (!p.scriptable.endsWith(')'))
830
                    continue;
831
                fprintf(out, "        case %d: *_b = %s; break;\n",
832
                         propindex, p.scriptable.constData());
833
            }
834
            fprintf(out, "        }\n");
835
        }
836
        fprintf(out,
837
                "        _id -= %d;\n"
838
                "    }", cdef->propertyList.count());
839
840
        fprintf(out, " else ");
841
        fprintf(out, "if (_c == QMetaObject::QueryPropertyStored) {\n");
842
        if (needStored) {
843
            fprintf(out, "        bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
844
            fprintf(out, "        switch (_id) {\n");
845
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
846
                const PropertyDef &p = cdef->propertyList.at(propindex);
847
                if (!p.stored.endsWith(')'))
848
                    continue;
849
                fprintf(out, "        case %d: *_b = %s; break;\n",
850
                         propindex, p.stored.constData());
851
            }
852
            fprintf(out, "        }\n");
853
        }
854
        fprintf(out,
855
                "        _id -= %d;\n"
856
                "    }", cdef->propertyList.count());
857
858
        fprintf(out, " else ");
859
        fprintf(out, "if (_c == QMetaObject::QueryPropertyEditable) {\n");
860
        if (needEditable) {
861
            fprintf(out, "        bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
862
            fprintf(out, "        switch (_id) {\n");
863
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
864
                const PropertyDef &p = cdef->propertyList.at(propindex);
865
                if (!p.editable.endsWith(')'))
866
                    continue;
867
                fprintf(out, "        case %d: *_b = %s; break;\n",
868
                         propindex, p.editable.constData());
869
            }
870
            fprintf(out, "        }\n");
871
        }
872
        fprintf(out,
873
                "        _id -= %d;\n"
874
                "    }", cdef->propertyList.count());
875
876
877
        fprintf(out, " else ");
878
        fprintf(out, "if (_c == QMetaObject::QueryPropertyUser) {\n");
879
        if (needUser) {
880
            fprintf(out, "        bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
881
            fprintf(out, "        switch (_id) {\n");
882
            for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
883
                const PropertyDef &p = cdef->propertyList.at(propindex);
884
                if (!p.user.endsWith(')'))
885
                    continue;
886
                fprintf(out, "        case %d: *_b = %s; break;\n",
887
                         propindex, p.user.constData());
888
            }
889
            fprintf(out, "        }\n");
890
        }
891
        fprintf(out,
892
                "        _id -= %d;\n"
893
                "    }", cdef->propertyList.count());
894
895
896
        fprintf(out, "\n#endif // QT_NO_PROPERTIES");
897
    }
898
    if (methodList.size() || cdef->signalList.size() || cdef->propertyList.size())
899
        fprintf(out, "\n    ");
900
    fprintf(out,"return _id;\n}\n");
901
}
902
903
void Generator::generateStaticMetacall(const QByteArray &prefix)
904
{
905
    bool isQObject = (cdef->classname == "QObject");
906
907
    fprintf(out, "static int %s_qt_static_metacall(QMetaObject::Call _c, int _id, void **_a)\n{\n",
908
            prefix.constData());
909
910
    fprintf(out, "    if (_c == QMetaObject::CreateInstance) {\n");
911
    fprintf(out, "        switch (_id) {\n");
912
    for (int ctorindex = 0; ctorindex < cdef->constructorList.count(); ++ctorindex) {
913
        fprintf(out, "        case %d: { %s *_r = new %s(", ctorindex,
c938ec0 by Kent Hansen at 2009-06-25 914
                cdef->qualified.constData(), cdef->qualified.constData());
67ad051 by Lars Knoll at 2009-03-23 915
        const FunctionDef &f = cdef->constructorList.at(ctorindex);
916
        int offset = 1;
917
        for (int j = 0; j < f.arguments.count(); ++j) {
918
            const ArgumentDef &a = f.arguments.at(j);
919
            if (j)
920
                fprintf(out, ",");
921
            fprintf(out, "(*reinterpret_cast< %s>(_a[%d]))", a.typeNameForCast.constData(), offset++);
922
        }
923
        fprintf(out, ");\n");
924
        fprintf(out, "            if (_a[0]) *reinterpret_cast<QObject**>(_a[0]) = _r; } break;\n");
925
    }
926
    fprintf(out, "        }\n");
927
    fprintf(out, "        _id -= %d;\n", cdef->constructorList.count());
928
    fprintf(out, "        return _id;\n");
929
    fprintf(out, "    }\n");
930
931
    if (!isQObject)
c938ec0 by Kent Hansen at 2009-06-25 932
        fprintf(out, "    _id = %s::staticMetaObject.superClass()->static_metacall(_c, _id, _a);\n", cdef->qualified.constData());
67ad051 by Lars Knoll at 2009-03-23 933
934
    fprintf(out, "    if (_id < 0)\n        return _id;\n");
935
936
    fprintf(out, "    return _id;\n");
937
    fprintf(out, "}\n\n");
938
}
939
940
void Generator::generateSignal(FunctionDef *def,int index)
941
{
942
    if (def->wasCloned || def->isAbstract)
943
        return;
944
    fprintf(out, "\n// SIGNAL %d\n%s %s::%s(",
945
            index, def->type.name.constData(), cdef->qualified.constData(), def->name.constData());
946
947
    QByteArray thisPtr = "this";
948
    const char *constQualifier = "";
949
950
    if (def->isConst) {
951
        thisPtr = "const_cast< ";
952
        thisPtr += cdef->qualified;
953
        thisPtr += " *>(this)";
954
        constQualifier = "const";
955
    }
956
957
    if (def->arguments.isEmpty() && def->normalizedType.isEmpty()) {
958
        fprintf(out, ")%s\n{\n"
959
                "    QMetaObject::activate(%s, &staticMetaObject, %d, 0);\n"
960
                "}\n", constQualifier, thisPtr.constData(), index);
961
        return;
962
    }
963
964
    int offset = 1;
965
    for (int j = 0; j < def->arguments.count(); ++j) {
966
        const ArgumentDef &a = def->arguments.at(j);
967
        if (j)
968
            fprintf(out, ", ");
969
        fprintf(out, "%s _t%d%s", a.type.name.constData(), offset++, a.rightType.constData());
970
    }
971
    fprintf(out, ")%s\n{\n", constQualifier);
972
    if (def->type.name.size() && def->normalizedType.size())
973
        fprintf(out, "    %s _t0;\n", noRef(def->normalizedType).constData());
974
975
    fprintf(out, "    void *_a[] = { ");
976
    if (def->normalizedType.isEmpty()) {
977
        fprintf(out, "0");
978
    } else {
979
        if (def->returnTypeIsVolatile)
980
             fprintf(out, "const_cast<void*>(reinterpret_cast<const volatile void*>(&_t0))");
981
        else
982
             fprintf(out, "const_cast<void*>(reinterpret_cast<const void*>(&_t0))");
983
    }
984
    int i;
985
    for (i = 1; i < offset; ++i)
986
        if (def->arguments.at(i - 1).type.isVolatile)
987
            fprintf(out, ", const_cast<void*>(reinterpret_cast<const volatile void*>(&_t%d))", i);
988
        else
989
            fprintf(out, ", const_cast<void*>(reinterpret_cast<const void*>(&_t%d))", i);
990
    fprintf(out, " };\n");
919b723 by Olivier Goffart at 2009-08-19 991
    fprintf(out, "    QMetaObject::activate(%s, &staticMetaObject, %d, _a);\n", thisPtr.constData(), index);
67ad051 by Lars Knoll at 2009-03-23 992
    if (def->normalizedType.size())
993
        fprintf(out, "    return _t0;\n");
994
    fprintf(out, "}\n");
995
}
996
997
//
998
// Functions used when generating QMetaObject directly
999
//
1000
// Much of this code is copied from the corresponding
1001
// C++ code-generating functions; we can change the
1002
// two generators so that more of the code is shared.
1003
// The key difference from the C++ code generator is
1004
// that instead of calling fprintf(), we append bytes
1005
// to a buffer.
1006
//
1007
1008
QMetaObject *Generator::generateMetaObject(bool ignoreProperties)
1009
{
1010
//
1011
// build the data array
1012
//
1013
1014
    // filter out undeclared enumerators and sets
1015
    {
1016
        QList<EnumDef> enumList;
1017
        for (int i = 0; i < cdef->enumList.count(); ++i) {
1018
            EnumDef def = cdef->enumList.at(i);
1019
            if (cdef->enumDeclarations.contains(def.name)) {
1020
                enumList += def;
1021
            }
1022
            QByteArray alias = cdef->flagAliases.value(def.name);
1023
            if (cdef->enumDeclarations.contains(alias)) {
1024
                def.name = alias;
1025
                enumList += def;
1026
            }
1027
        }
1028
        cdef->enumList = enumList;
1029
    }
1030
1031
    int index = 10;
1032
    meta_data
1033
        << 1 // revision
1034
        << strreg(cdef->qualified) // classname
1035
        << cdef->classInfoList.count() << (cdef->classInfoList.count() ? index : 0) // classinfo
1036
        ;
1037
    index += cdef->classInfoList.count() * 2;
1038
1039
    int methodCount = cdef->signalList.count() + cdef->slotList.count() + cdef->methodList.count();
1040
    meta_data << methodCount << (methodCount ? index : 0); // methods
1041
    index += methodCount * 5;
1042
    if (!ignoreProperties) {
1043
        meta_data << cdef->propertyList.count() << (cdef->propertyList.count() ? index : 0); // properties
1044
        index += cdef->propertyList.count() * 3;
1045
    } else {
1046
        meta_data << 0 << 0; // properties
1047
    }
1048
    meta_data << cdef->enumList.count() << (cdef->enumList.count() ? index : 0); // enums/sets
1049
1050
//
1051
// Build classinfo array
1052
//
1053
    _generateClassInfos();
1054
1055
//
1056
// Build signals array first, otherwise the signal indices would be wrong
1057
//
1058
    _generateFunctions(cdef->signalList, MethodSignal);
1059
1060
//
1061
// Build slots array
1062
//
1063
    _generateFunctions(cdef->slotList, MethodSlot);
1064
1065
//
1066
// Build method array
1067
//
1068
    _generateFunctions(cdef->methodList, MethodMethod);
1069
1070
1071
//
1072
// Build property array
1073
//
1074
    if (!ignoreProperties)
1075
        _generateProperties();
1076
1077
//
1078
// Build enums array
1079
//
1080
    _generateEnums(index);
1081
1082
//
1083
// Terminate data array
1084
//
1085
    meta_data << 0;
1086
1087
//
1088
// Build stringdata array
1089
//
1090
    QVector<char> string_data;
1091
    for (int i = 0; i < strings.size(); ++i) {
1092
        const char *s = strings.at(i).constData();
1093
        char c;
1094
        do {
1095
            c = *(s++);
1096
            string_data << c;
1097
        } while (c != '\0');
1098
    }
1099
1100
//
1101
// Finally create and initialize the static meta object
1102
//
1103
    const int meta_object_offset = 0;
1104
    const int meta_object_size = sizeof(QMetaObject);
1105
    const int meta_data_offset = meta_object_offset + meta_object_size;
1106
    const int meta_data_size = meta_data.count() * sizeof(uint);
1107
    const int string_data_offset = meta_data_offset + meta_data_size;
1108
    const int string_data_size = string_data.count();
1109
    const int total_size = string_data_offset + string_data_size;
1110
1111
    char *blob = new char[total_size];
1112
1113
    char *string_data_output = blob + string_data_offset;
1114
    const char *string_data_src = string_data.constData();
1115
    for (int i = 0; i < string_data.count(); ++i)
1116
        string_data_output[i] = string_data_src[i];
1117
1118
    uint *meta_data_output = reinterpret_cast<uint *>(blob + meta_data_offset);
1119
    const uint *meta_data_src = meta_data.constData();
1120
    for (int i = 0; i < meta_data.count(); ++i)
1121
        meta_data_output[i] = meta_data_src[i];
1122
1123
    QMetaObject *meta_object = new (blob + meta_object_offset)QMetaObject;
1124
    meta_object->d.superdata = 0;
1125
    meta_object->d.stringdata = string_data_output;
1126
    meta_object->d.data = meta_data_output;
1127
    meta_object->d.extradata = 0;
1128
    return meta_object;
1129
}
1130
1131
void Generator::_generateClassInfos()
1132
{
1133
    for (int i = 0; i < cdef->classInfoList.size(); ++i) {
1134
        const ClassInfoDef &c = cdef->classInfoList.at(i);
1135
        meta_data << strreg(c.name) << strreg(c.value);
1136
    }
1137
}
1138
1139
void Generator::_generateFunctions(QList<FunctionDef> &list, int type)
1140
{
1141
    for (int i = 0; i < list.count(); ++i) {
1142
        const FunctionDef &f = list.at(i);
1143
1144
        QByteArray sig = f.name + '(';
1145
        QByteArray arguments;
1146
1147
        for (int j = 0; j < f.arguments.count(); ++j) {
1148
            const ArgumentDef &a = f.arguments.at(j);
1149
            if (j) {
f390f9b by Thierry Bastian at 2009-05-28 1150
                sig += ',';
1151
                arguments += ',';
67ad051 by Lars Knoll at 2009-03-23 1152
            }
1153
            sig += a.normalizedType;
1154
            arguments += a.name;
1155
        }
1156
        sig += ')';
1157
1158
        char flags = type;
1159
        if (f.access == FunctionDef::Private)
1160
            flags |= AccessPrivate;
1161
        else if (f.access == FunctionDef::Public)
1162
            flags |= AccessPublic;
1163
        else if (f.access == FunctionDef::Protected)
1164
            flags |= AccessProtected;
1165
        if (f.access == FunctionDef::Private)
1166
            flags |= AccessPrivate;
1167
        else if (f.access == FunctionDef::Public)
1168
            flags |= AccessPublic;
1169
        else if (f.access == FunctionDef::Protected)
1170
            flags |= AccessProtected;
1171
        if (f.isCompat)
1172
            flags |= MethodCompatibility;
1173
        if (f.wasCloned)
1174
            flags |= MethodCloned;
1175
        if (f.isScriptable)
1176
            flags |= MethodScriptable;
1177
1178
        meta_data << strreg(sig)
1179
                  << strreg(arguments)
1180
                  << strreg(f.normalizedType)
1181
                  << strreg(f.tag)
1182
                  << flags;
1183
    }
1184
}
1185
1186
void Generator::_generateEnums(int index)
1187
{
1188
    index += 4 * cdef->enumList.count();
1189
    int i;
1190
    for (i = 0; i < cdef->enumList.count(); ++i) {
1191
        const EnumDef &e = cdef->enumList.at(i);
1192
        meta_data << strreg(e.name) << (cdef->enumDeclarations.value(e.name) ? 1 : 0)
1193
                  << e.values.count() << index;
1194
        index += e.values.count() * 2;
1195
    }
1196
1197
    for (i = 0; i < cdef->enumList.count(); ++i) {
1198
        const EnumDef &e = cdef->enumList.at(i);
1199
        for (int j = 0; j < e.values.count(); ++j) {
1200
            const QByteArray &val = e.values.at(j);
1201
            meta_data << strreg(val) << 0; // we don't know the value itself
1202
        }
1203
    }
1204
}
1205
1206
void Generator::_generateProperties()
1207
{
1208
    //
1209
    // specify get function, for compatibiliy we accept functions
1210
    // returning pointers, or const char * for QByteArray.
1211
    //
1212
    for (int i = 0; i < cdef->propertyList.count(); ++i) {
1213
        PropertyDef &p = cdef->propertyList[i];
1214
        if (p.read.isEmpty())
1215
            continue;
1216
        for (int j = 0; j < cdef->publicList.count(); ++j) {
1217
            const FunctionDef &f = cdef->publicList.at(j);
1218
            if (f.name != p.read)
1219
                continue;
1220
            if (!f.isConst) // get  functions must be const
1221
                continue;
1222
            if (f.arguments.size()) // and must not take any arguments
1223
                continue;
1224
            PropertyDef::Specification spec = PropertyDef::ValueSpec;
1225
            QByteArray tmp = f.normalizedType;
1226
            if (p.type == "QByteArray" && tmp == "const char *")
1227
                    tmp = "QByteArray";
1228
            if (tmp.left(6) == "const ")
1229
                tmp = tmp.mid(6);
1230
            if (p.type != tmp && tmp.endsWith('*')) {
1231
                tmp.chop(1);
1232
                spec = PropertyDef::PointerSpec;
1233
            } else if (f.type.name.endsWith('&')) { // raw type, not normalized type
1234
                spec = PropertyDef::ReferenceSpec;
1235
            }
1236
            if (p.type != tmp)
1237
                continue;
1238
            p.gspec = spec;
1239
            break;
1240
        }
1241
    }
1242
1243
1244
    //
1245
    // Create meta data
1246
    //
1247
1248
    for (int i = 0; i < cdef->propertyList.count(); ++i) {
1249
        const PropertyDef &p = cdef->propertyList.at(i);
1250
        uint flags = Invalid;
1251
        if (!isVariantType(p.type)) {
1252
            flags |= EnumOrFlag;
1253
        } else {
1254
            flags |= qvariant_nameToType(p.type) << 24;
1255
        }
1256
        if (!p.read.isEmpty())
1257
            flags |= Readable;
1258
        if (!p.write.isEmpty()) {
1259
            flags |= Writable;
1260
            if (p.stdCppSet())
1261
                flags |= StdCppSet;
1262
        }
1263
        if (!p.reset.isEmpty())
1264
            flags |= Resettable;
1265
1266
//         if (p.override)
1267
//             flags |= Override;
1268
1269
        if (p.designable.isEmpty())
1270
            flags |= ResolveDesignable;
1271
        else if (p.designable != "false")
1272
            flags |= Designable;
1273
1274
        if (p.scriptable.isEmpty())
1275
            flags |= ResolveScriptable;
1276
        else if (p.scriptable != "false")
1277
            flags |= Scriptable;
1278
1279
        if (p.stored.isEmpty())
1280
            flags |= ResolveStored;
1281
        else if (p.stored != "false")
1282
            flags |= Stored;
1283
1284
        if (p.editable.isEmpty())
1285
            flags |= ResolveEditable;
1286
        else if (p.editable != "false")
1287
            flags |= Editable;
1288
1289
        if (p.user.isEmpty())
1290
            flags |= ResolveUser;
1291
        else if (p.user != "false")
1292
            flags |= User;
1293
1294
        meta_data << strreg(p.name) << strreg(p.type) << flags;
1295
    }
1296
}
1297
1298
QT_END_NAMESPACE