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