1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
|
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the tools applications of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "generator.h"
#include "outputrevision.h"
#include "utils.h"
#include <QtCore/qmetatype.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonvalue.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qplugin.h>
#include <stdio.h>
#include <private/qmetaobject_p.h> //for the flags.
QT_BEGIN_NAMESPACE
uint nameToBuiltinType(const QByteArray &name)
{
if (name.isEmpty())
return 0;
uint tp = QMetaType::type(name.constData());
return tp < uint(QMetaType::User) ? tp : uint(QMetaType::UnknownType);
}
/*
Returns \c true if the type is a built-in type.
*/
bool isBuiltinType(const QByteArray &type)
{
int id = QMetaType::type(type.constData());
if (id == QMetaType::UnknownType)
return false;
return (id < QMetaType::User);
}
static const char *metaTypeEnumValueString(int type)
{
#define RETURN_METATYPENAME_STRING(MetaTypeName, MetaTypeId, RealType) \
case QMetaType::MetaTypeName: return #MetaTypeName;
switch (type) {
QT_FOR_EACH_STATIC_TYPE(RETURN_METATYPENAME_STRING)
}
#undef RETURN_METATYPENAME_STRING
return 0;
}
Generator::Generator(ClassDef *classDef, const QList<QByteArray> &metaTypes, const QHash<QByteArray, QByteArray> &knownQObjectClasses, const QHash<QByteArray, QByteArray> &knownGadgets, FILE *outfile)
: out(outfile), cdef(classDef), metaTypes(metaTypes), knownQObjectClasses(knownQObjectClasses)
, knownGadgets(knownGadgets)
{
if (cdef->superclassList.size())
purestSuperClass = cdef->superclassList.constFirst().first;
}
static inline int lengthOfEscapeSequence(const QByteArray &s, int i)
{
if (s.at(i) != '\\' || i >= s.length() - 1)
return 1;
const int startPos = i;
++i;
char ch = s.at(i);
if (ch == 'x') {
++i;
while (i < s.length() && is_hex_char(s.at(i)))
++i;
} else if (is_octal_char(ch)) {
while (i < startPos + 4
&& i < s.length()
&& is_octal_char(s.at(i))) {
++i;
}
} else { // single character escape sequence
i = qMin(i + 1, s.length());
}
return i - startPos;
}
void Generator::strreg(const QByteArray &s)
{
if (!strings.contains(s))
strings.append(s);
}
int Generator::stridx(const QByteArray &s)
{
int i = strings.indexOf(s);
Q_ASSERT_X(i != -1, Q_FUNC_INFO, "We forgot to register some strings");
return i;
}
// Returns the sum of all parameters (including return type) for the given
// \a list of methods. This is needed for calculating the size of the methods'
// parameter type/name meta-data.
static int aggregateParameterCount(const QVector<FunctionDef> &list)
{
int sum = 0;
for (int i = 0; i < list.count(); ++i)
sum += list.at(i).arguments.count() + 1; // +1 for return type
return sum;
}
bool Generator::registerableMetaType(const QByteArray &propertyType)
{
if (metaTypes.contains(propertyType))
return true;
if (propertyType.endsWith('*')) {
QByteArray objectPointerType = propertyType;
// The objects container stores class names, such as 'QState', 'QLabel' etc,
// not 'QState*', 'QLabel*'. The propertyType does contain the '*', so we need
// to chop it to find the class type in the known QObjects list.
objectPointerType.chop(1);
if (knownQObjectClasses.contains(objectPointerType))
return true;
}
static const QVector<QByteArray> smartPointers = QVector<QByteArray>()
#define STREAM_SMART_POINTER(SMART_POINTER) << #SMART_POINTER
QT_FOR_EACH_AUTOMATIC_TEMPLATE_SMART_POINTER(STREAM_SMART_POINTER)
#undef STREAM_SMART_POINTER
;
for (const QByteArray &smartPointer : smartPointers) {
if (propertyType.startsWith(smartPointer + "<") && !propertyType.endsWith("&"))
return knownQObjectClasses.contains(propertyType.mid(smartPointer.size() + 1, propertyType.size() - smartPointer.size() - 1 - 1));
}
static const QVector<QByteArray> oneArgTemplates = QVector<QByteArray>()
#define STREAM_1ARG_TEMPLATE(TEMPLATENAME) << #TEMPLATENAME
QT_FOR_EACH_AUTOMATIC_TEMPLATE_1ARG(STREAM_1ARG_TEMPLATE)
#undef STREAM_1ARG_TEMPLATE
;
for (const QByteArray &oneArgTemplateType : oneArgTemplates) {
if (propertyType.startsWith(oneArgTemplateType + "<") && propertyType.endsWith(">")) {
const int argumentSize = propertyType.size() - oneArgTemplateType.size() - 1
// The closing '>'
- 1
// templates inside templates have an extra whitespace char to strip.
- (propertyType.at(propertyType.size() - 2) == ' ' ? 1 : 0 );
const QByteArray templateArg = propertyType.mid(oneArgTemplateType.size() + 1, argumentSize);
return isBuiltinType(templateArg) || registerableMetaType(templateArg);
}
}
return false;
}
/* returns \c true if name and qualifiedName refers to the same name.
* If qualified name is "A::B::C", it returns \c true for "C", "B::C" or "A::B::C" */
static bool qualifiedNameEquals(const QByteArray &qualifiedName, const QByteArray &name)
{
if (qualifiedName == name)
return true;
int index = qualifiedName.indexOf("::");
if (index == -1)
return false;
return qualifiedNameEquals(qualifiedName.mid(index+2), name);
}
void Generator::generateCode()
{
bool isQt = (cdef->classname == "Qt");
bool isQObject = (cdef->classname == "QObject");
bool isConstructible = !cdef->constructorList.isEmpty();
// filter out undeclared enumerators and sets
{
QVector<EnumDef> enumList;
for (int i = 0; i < cdef->enumList.count(); ++i) {
EnumDef def = cdef->enumList.at(i);
if (cdef->enumDeclarations.contains(def.name)) {
enumList += def;
}
QByteArray alias = cdef->flagAliases.value(def.name);
if (cdef->enumDeclarations.contains(alias)) {
def.name = alias;
enumList += def;
}
}
cdef->enumList = enumList;
}
//
// Register all strings used in data section
//
strreg(cdef->qualified);
registerClassInfoStrings();
registerFunctionStrings(cdef->signalList);
registerFunctionStrings(cdef->slotList);
registerFunctionStrings(cdef->methodList);
registerFunctionStrings(cdef->constructorList);
registerPropertyStrings();
registerEnumStrings();
QByteArray qualifiedClassNameIdentifier = cdef->qualified;
qualifiedClassNameIdentifier.replace(':', '_');
//
// Build stringdata struct
//
const int constCharArraySizeLimit = 65535;
fprintf(out, "struct qt_meta_stringdata_%s_t {\n", qualifiedClassNameIdentifier.constData());
fprintf(out, " QByteArrayData data[%d];\n", strings.size());
{
int stringDataLength = 0;
int stringDataCounter = 0;
for (int i = 0; i < strings.size(); ++i) {
int thisLength = strings.at(i).length() + 1;
stringDataLength += thisLength;
if (stringDataLength / constCharArraySizeLimit) {
// save previous stringdata and start computing the next one.
fprintf(out, " char stringdata%d[%d];\n", stringDataCounter++, stringDataLength - thisLength);
stringDataLength = thisLength;
}
}
fprintf(out, " char stringdata%d[%d];\n", stringDataCounter, stringDataLength);
}
fprintf(out, "};\n");
// Macro that expands into a QByteArrayData. The offset member is
// calculated from 1) the offset of the actual characters in the
// stringdata.stringdata member, and 2) the stringdata.data index of the
// QByteArrayData being defined. This calculation relies on the
// QByteArrayData::data() implementation returning simply "this + offset".
fprintf(out, "#define QT_MOC_LITERAL(idx, ofs, len) \\\n"
" Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER_WITH_OFFSET(len, \\\n"
" qptrdiff(offsetof(qt_meta_stringdata_%s_t, stringdata0) + ofs \\\n"
" - idx * sizeof(QByteArrayData)) \\\n"
" )\n",
qualifiedClassNameIdentifier.constData());
fprintf(out, "static const qt_meta_stringdata_%s_t qt_meta_stringdata_%s = {\n",
qualifiedClassNameIdentifier.constData(), qualifiedClassNameIdentifier.constData());
fprintf(out, " {\n");
{
int idx = 0;
for (int i = 0; i < strings.size(); ++i) {
const QByteArray &str = strings.at(i);
fprintf(out, "QT_MOC_LITERAL(%d, %d, %d)", i, idx, str.length());
if (i != strings.size() - 1)
fputc(',', out);
const QByteArray comment = str.length() > 32 ? str.left(29) + "..." : str;
fprintf(out, " // \"%s\"\n", comment.constData());
idx += str.length() + 1;
for (int j = 0; j < str.length(); ++j) {
if (str.at(j) == '\\') {
int cnt = lengthOfEscapeSequence(str, j) - 1;
idx -= cnt;
j += cnt;
}
}
}
fprintf(out, "\n },\n");
}
//
// Build stringdata array
//
fprintf(out, " \"");
int col = 0;
int len = 0;
int stringDataLength = 0;
for (int i = 0; i < strings.size(); ++i) {
QByteArray s = strings.at(i);
len = s.length();
stringDataLength += len + 1;
if (stringDataLength >= constCharArraySizeLimit) {
fprintf(out, "\",\n \"");
stringDataLength = len + 1;
col = 0;
} else if (i)
fputs("\\0", out); // add \0 at the end of each string
if (col && col + len >= 72) {
fprintf(out, "\"\n \"");
col = 0;
} else if (len && s.at(0) >= '0' && s.at(0) <= '9') {
fprintf(out, "\"\"");
len += 2;
}
int idx = 0;
while (idx < s.length()) {
if (idx > 0) {
col = 0;
fprintf(out, "\"\n \"");
}
int spanLen = qMin(70, s.length() - idx);
// don't cut escape sequences at the end of a line
int backSlashPos = s.lastIndexOf('\\', idx + spanLen - 1);
if (backSlashPos >= idx) {
int escapeLen = lengthOfEscapeSequence(s, backSlashPos);
spanLen = qBound(spanLen, backSlashPos + escapeLen - idx, s.length() - idx);
}
fprintf(out, "%.*s", spanLen, s.constData() + idx);
idx += spanLen;
col += spanLen;
}
col += len + 2;
}
// Terminate stringdata struct
fprintf(out, "\"\n};\n");
fprintf(out, "#undef QT_MOC_LITERAL\n\n");
//
// build the data array
//
int index = MetaObjectPrivateFieldCount;
fprintf(out, "static const uint qt_meta_data_%s[] = {\n", qualifiedClassNameIdentifier.constData());
fprintf(out, "\n // content:\n");
fprintf(out, " %4d, // revision\n", int(QMetaObjectPrivate::OutputRevision));
fprintf(out, " %4d, // classname\n", stridx(cdef->qualified));
fprintf(out, " %4d, %4d, // classinfo\n", cdef->classInfoList.count(), cdef->classInfoList.count() ? index : 0);
index += cdef->classInfoList.count() * 2;
int methodCount = cdef->signalList.count() + cdef->slotList.count() + cdef->methodList.count();
fprintf(out, " %4d, %4d, // methods\n", methodCount, methodCount ? index : 0);
index += methodCount * 5;
if (cdef->revisionedMethods)
index += methodCount;
int paramsIndex = index;
int totalParameterCount = aggregateParameterCount(cdef->signalList)
+ aggregateParameterCount(cdef->slotList)
+ aggregateParameterCount(cdef->methodList)
+ aggregateParameterCount(cdef->constructorList);
index += totalParameterCount * 2 // types and parameter names
- methodCount // return "parameters" don't have names
- cdef->constructorList.count(); // "this" parameters don't have names
fprintf(out, " %4d, %4d, // properties\n", cdef->propertyList.count(), cdef->propertyList.count() ? index : 0);
index += cdef->propertyList.count() * 3;
if(cdef->notifyableProperties)
index += cdef->propertyList.count();
if (cdef->revisionedProperties)
index += cdef->propertyList.count();
fprintf(out, " %4d, %4d, // enums/sets\n", cdef->enumList.count(), cdef->enumList.count() ? index : 0);
int enumsIndex = index;
for (int i = 0; i < cdef->enumList.count(); ++i)
index += 4 + (cdef->enumList.at(i).values.count() * 2);
fprintf(out, " %4d, %4d, // constructors\n", isConstructible ? cdef->constructorList.count() : 0,
isConstructible ? index : 0);
int flags = 0;
if (cdef->hasQGadget) {
// Ideally, all the classes could have that flag. But this broke classes generated
// by qdbusxml2cpp which generate code that require that we call qt_metacall for properties
flags |= PropertyAccessInStaticMetaCall;
}
fprintf(out, " %4d, // flags\n", flags);
fprintf(out, " %4d, // signalCount\n", cdef->signalList.count());
//
// Build classinfo array
//
generateClassInfos();
//
// Build signals array first, otherwise the signal indices would be wrong
//
generateFunctions(cdef->signalList, "signal", MethodSignal, paramsIndex);
//
// Build slots array
//
generateFunctions(cdef->slotList, "slot", MethodSlot, paramsIndex);
//
// Build method array
//
generateFunctions(cdef->methodList, "method", MethodMethod, paramsIndex);
//
// Build method version arrays
//
if (cdef->revisionedMethods) {
generateFunctionRevisions(cdef->signalList, "signal");
generateFunctionRevisions(cdef->slotList, "slot");
generateFunctionRevisions(cdef->methodList, "method");
}
//
// Build method parameters array
//
generateFunctionParameters(cdef->signalList, "signal");
generateFunctionParameters(cdef->slotList, "slot");
generateFunctionParameters(cdef->methodList, "method");
if (isConstructible)
generateFunctionParameters(cdef->constructorList, "constructor");
//
// Build property array
//
generateProperties();
//
// Build enums array
//
generateEnums(enumsIndex);
//
// Build constructors array
//
if (isConstructible)
generateFunctions(cdef->constructorList, "constructor", MethodConstructor, paramsIndex);
//
// Terminate data array
//
fprintf(out, "\n 0 // eod\n};\n\n");
//
// Generate internal qt_static_metacall() function
//
const bool hasStaticMetaCall = !isQt &&
(cdef->hasQObject || !cdef->methodList.isEmpty()
|| !cdef->propertyList.isEmpty() || !cdef->constructorList.isEmpty());
if (hasStaticMetaCall)
generateStaticMetacall();
//
// Build extra array
//
QList<QByteArray> extraList;
QHash<QByteArray, QByteArray> knownExtraMetaObject = knownGadgets;
knownExtraMetaObject.unite(knownQObjectClasses);
for (int i = 0; i < cdef->propertyList.count(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
if (isBuiltinType(p.type))
continue;
if (p.type.contains('*') || p.type.contains('<') || p.type.contains('>'))
continue;
int s = p.type.lastIndexOf("::");
if (s <= 0)
continue;
QByteArray unqualifiedScope = p.type.left(s);
// The scope may be a namespace for example, so it's only safe to include scopes that are known QObjects (QTBUG-2151)
QHash<QByteArray, QByteArray>::ConstIterator scopeIt;
QByteArray thisScope = cdef->qualified;
do {
int s = thisScope.lastIndexOf("::");
thisScope = thisScope.left(s);
QByteArray currentScope = thisScope.isEmpty() ? unqualifiedScope : thisScope + "::" + unqualifiedScope;
scopeIt = knownExtraMetaObject.constFind(currentScope);
} while (!thisScope.isEmpty() && scopeIt == knownExtraMetaObject.constEnd());
if (scopeIt == knownExtraMetaObject.constEnd())
continue;
const QByteArray &scope = *scopeIt;
if (scope == "Qt")
continue;
if (qualifiedNameEquals(cdef->qualified, scope))
continue;
if (!extraList.contains(scope))
extraList += scope;
}
// QTBUG-20639 - Accept non-local enums for QML signal/slot parameters.
// Look for any scoped enum declarations, and add those to the list
// of extra/related metaobjects for this object.
for (auto it = cdef->enumDeclarations.keyBegin(),
end = cdef->enumDeclarations.keyEnd(); it != end; ++it) {
const QByteArray &enumKey = *it;
int s = enumKey.lastIndexOf("::");
if (s > 0) {
QByteArray scope = enumKey.left(s);
if (scope != "Qt" && !qualifiedNameEquals(cdef->qualified, scope) && !extraList.contains(scope))
extraList += scope;
}
}
if (!extraList.isEmpty()) {
fprintf(out, "static const QMetaObject * const qt_meta_extradata_%s[] = {\n ", qualifiedClassNameIdentifier.constData());
for (int i = 0; i < extraList.count(); ++i) {
fprintf(out, " &%s::staticMetaObject,\n", extraList.at(i).constData());
}
fprintf(out, " Q_NULLPTR\n};\n\n");
}
//
// Finally create and initialize the static meta object
//
if (isQt)
fprintf(out, "const QMetaObject QObject::staticQtMetaObject = {\n");
else
fprintf(out, "const QMetaObject %s::staticMetaObject = {\n", cdef->qualified.constData());
if (isQObject)
fprintf(out, " { Q_NULLPTR, ");
else if (cdef->superclassList.size() && (!cdef->hasQGadget || knownGadgets.contains(purestSuperClass)))
fprintf(out, " { &%s::staticMetaObject, ", purestSuperClass.constData());
else
fprintf(out, " { Q_NULLPTR, ");
fprintf(out, "qt_meta_stringdata_%s.data,\n"
" qt_meta_data_%s, ", qualifiedClassNameIdentifier.constData(),
qualifiedClassNameIdentifier.constData());
if (hasStaticMetaCall)
fprintf(out, " qt_static_metacall, ");
else
fprintf(out, " Q_NULLPTR, ");
if (extraList.isEmpty())
fprintf(out, "Q_NULLPTR, ");
else
fprintf(out, "qt_meta_extradata_%s, ", qualifiedClassNameIdentifier.constData());
fprintf(out, "Q_NULLPTR}\n};\n\n");
if(isQt)
return;
if (!cdef->hasQObject)
return;
fprintf(out, "\nconst QMetaObject *%s::metaObject() const\n{\n return QObject::d_ptr->metaObject ? QObject::d_ptr->dynamicMetaObject() : &staticMetaObject;\n}\n",
cdef->qualified.constData());
//
// Generate smart cast function
//
fprintf(out, "\nvoid *%s::qt_metacast(const char *_clname)\n{\n", cdef->qualified.constData());
fprintf(out, " if (!_clname) return Q_NULLPTR;\n");
fprintf(out, " if (!strcmp(_clname, qt_meta_stringdata_%s.stringdata0))\n"
" return static_cast<void*>(const_cast< %s*>(this));\n",
qualifiedClassNameIdentifier.constData(), cdef->classname.constData());
for (int i = 1; i < cdef->superclassList.size(); ++i) { // for all superclasses but the first one
if (cdef->superclassList.at(i).second == FunctionDef::Private)
continue;
const char *cname = cdef->superclassList.at(i).first.constData();
fprintf(out, " if (!strcmp(_clname, \"%s\"))\n return static_cast< %s*>(const_cast< %s*>(this));\n",
cname, cname, cdef->classname.constData());
}
for (int i = 0; i < cdef->interfaceList.size(); ++i) {
const QVector<ClassDef::Interface> &iface = cdef->interfaceList.at(i);
for (int j = 0; j < iface.size(); ++j) {
fprintf(out, " if (!strcmp(_clname, %s))\n return ", iface.at(j).interfaceId.constData());
for (int k = j; k >= 0; --k)
fprintf(out, "static_cast< %s*>(", iface.at(k).className.constData());
fprintf(out, "const_cast< %s*>(this)%s;\n",
cdef->classname.constData(), QByteArray(j+1, ')').constData());
}
}
if (!purestSuperClass.isEmpty() && !isQObject) {
QByteArray superClass = purestSuperClass;
fprintf(out, " return %s::qt_metacast(_clname);\n", superClass.constData());
} else {
fprintf(out, " return Q_NULLPTR;\n");
}
fprintf(out, "}\n");
//
// Generate internal qt_metacall() function
//
generateMetacall();
//
// Generate internal signal functions
//
for (int signalindex = 0; signalindex < cdef->signalList.size(); ++signalindex)
generateSignal(&cdef->signalList[signalindex], signalindex);
//
// Generate plugin meta data
//
generatePluginMetaData();
}
void Generator::registerClassInfoStrings()
{
for (int i = 0; i < cdef->classInfoList.size(); ++i) {
const ClassInfoDef &c = cdef->classInfoList.at(i);
strreg(c.name);
strreg(c.value);
}
}
void Generator::generateClassInfos()
{
if (cdef->classInfoList.isEmpty())
return;
fprintf(out, "\n // classinfo: key, value\n");
for (int i = 0; i < cdef->classInfoList.size(); ++i) {
const ClassInfoDef &c = cdef->classInfoList.at(i);
fprintf(out, " %4d, %4d,\n", stridx(c.name), stridx(c.value));
}
}
void Generator::registerFunctionStrings(const QVector<FunctionDef>& list)
{
for (int i = 0; i < list.count(); ++i) {
const FunctionDef &f = list.at(i);
strreg(f.name);
if (!isBuiltinType(f.normalizedType))
strreg(f.normalizedType);
strreg(f.tag);
int argsCount = f.arguments.count();
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &a = f.arguments.at(j);
if (!isBuiltinType(a.normalizedType))
strreg(a.normalizedType);
strreg(a.name);
}
}
}
void Generator::generateFunctions(const QVector<FunctionDef>& list, const char *functype, int type, int ¶msIndex)
{
if (list.isEmpty())
return;
fprintf(out, "\n // %ss: name, argc, parameters, tag, flags\n", functype);
for (int i = 0; i < list.count(); ++i) {
const FunctionDef &f = list.at(i);
QByteArray comment;
unsigned char flags = type;
if (f.access == FunctionDef::Private) {
flags |= AccessPrivate;
comment.append("Private");
} else if (f.access == FunctionDef::Public) {
flags |= AccessPublic;
comment.append("Public");
} else if (f.access == FunctionDef::Protected) {
flags |= AccessProtected;
comment.append("Protected");
}
if (f.isCompat) {
flags |= MethodCompatibility;
comment.append(" | MethodCompatibility");
}
if (f.wasCloned) {
flags |= MethodCloned;
comment.append(" | MethodCloned");
}
if (f.isScriptable) {
flags |= MethodScriptable;
comment.append(" | isScriptable");
}
if (f.revision > 0) {
flags |= MethodRevisioned;
comment.append(" | MethodRevisioned");
}
int argc = f.arguments.count();
fprintf(out, " %4d, %4d, %4d, %4d, 0x%02x /* %s */,\n",
stridx(f.name), argc, paramsIndex, stridx(f.tag), flags, comment.constData());
paramsIndex += 1 + argc * 2;
}
}
void Generator::generateFunctionRevisions(const QVector<FunctionDef>& list, const char *functype)
{
if (list.count())
fprintf(out, "\n // %ss: revision\n", functype);
for (int i = 0; i < list.count(); ++i) {
const FunctionDef &f = list.at(i);
fprintf(out, " %4d,\n", f.revision);
}
}
void Generator::generateFunctionParameters(const QVector<FunctionDef>& list, const char *functype)
{
if (list.isEmpty())
return;
fprintf(out, "\n // %ss: parameters\n", functype);
for (int i = 0; i < list.count(); ++i) {
const FunctionDef &f = list.at(i);
fprintf(out, " ");
// Types
int argsCount = f.arguments.count();
for (int j = -1; j < argsCount; ++j) {
if (j > -1)
fputc(' ', out);
const QByteArray &typeName = (j < 0) ? f.normalizedType : f.arguments.at(j).normalizedType;
generateTypeInfo(typeName, /*allowEmptyName=*/f.isConstructor);
fputc(',', out);
}
// Parameter names
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &arg = f.arguments.at(j);
fprintf(out, " %4d,", stridx(arg.name));
}
fprintf(out, "\n");
}
}
void Generator::generateTypeInfo(const QByteArray &typeName, bool allowEmptyName)
{
Q_UNUSED(allowEmptyName);
if (isBuiltinType(typeName)) {
int type;
const char *valueString;
if (typeName == "qreal") {
type = QMetaType::UnknownType;
valueString = "QReal";
} else {
type = nameToBuiltinType(typeName);
valueString = metaTypeEnumValueString(type);
}
if (valueString) {
fprintf(out, "QMetaType::%s", valueString);
} else {
Q_ASSERT(type != QMetaType::UnknownType);
fprintf(out, "%4d", type);
}
} else {
Q_ASSERT(!typeName.isEmpty() || allowEmptyName);
fprintf(out, "0x%.8x | %d", IsUnresolvedType, stridx(typeName));
}
}
void Generator::registerPropertyStrings()
{
for (int i = 0; i < cdef->propertyList.count(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
strreg(p.name);
if (!isBuiltinType(p.type))
strreg(p.type);
}
}
void Generator::generateProperties()
{
//
// Create meta data
//
if (cdef->propertyList.count())
fprintf(out, "\n // properties: name, type, flags\n");
for (int i = 0; i < cdef->propertyList.count(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
uint flags = Invalid;
if (!isBuiltinType(p.type))
flags |= EnumOrFlag;
if (!p.member.isEmpty() && !p.constant)
flags |= Writable;
if (!p.read.isEmpty() || !p.member.isEmpty())
flags |= Readable;
if (!p.write.isEmpty()) {
flags |= Writable;
if (p.stdCppSet())
flags |= StdCppSet;
}
if (!p.reset.isEmpty())
flags |= Resettable;
// if (p.override)
// flags |= Override;
if (p.designable.isEmpty())
flags |= ResolveDesignable;
else if (p.designable != "false")
flags |= Designable;
if (p.scriptable.isEmpty())
flags |= ResolveScriptable;
else if (p.scriptable != "false")
flags |= Scriptable;
if (p.stored.isEmpty())
flags |= ResolveStored;
else if (p.stored != "false")
flags |= Stored;
if (p.editable.isEmpty())
flags |= ResolveEditable;
else if (p.editable != "false")
flags |= Editable;
if (p.user.isEmpty())
flags |= ResolveUser;
else if (p.user != "false")
flags |= User;
if (p.notifyId != -1)
flags |= Notify;
if (p.revision > 0)
flags |= Revisioned;
if (p.constant)
flags |= Constant;
if (p.final)
flags |= Final;
fprintf(out, " %4d, ", stridx(p.name));
generateTypeInfo(p.type);
fprintf(out, ", 0x%.8x,\n", flags);
}
if(cdef->notifyableProperties) {
fprintf(out, "\n // properties: notify_signal_id\n");
for (int i = 0; i < cdef->propertyList.count(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
if(p.notifyId == -1)
fprintf(out, " %4d,\n",
0);
else
fprintf(out, " %4d,\n",
p.notifyId);
}
}
if (cdef->revisionedProperties) {
fprintf(out, "\n // properties: revision\n");
for (int i = 0; i < cdef->propertyList.count(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
fprintf(out, " %4d,\n", p.revision);
}
}
}
void Generator::registerEnumStrings()
{
for (int i = 0; i < cdef->enumList.count(); ++i) {
const EnumDef &e = cdef->enumList.at(i);
strreg(e.name);
for (int j = 0; j < e.values.count(); ++j)
strreg(e.values.at(j));
}
}
void Generator::generateEnums(int index)
{
if (cdef->enumDeclarations.isEmpty())
return;
fprintf(out, "\n // enums: name, flags, count, data\n");
index += 4 * cdef->enumList.count();
int i;
for (i = 0; i < cdef->enumList.count(); ++i) {
const EnumDef &e = cdef->enumList.at(i);
fprintf(out, " %4d, 0x%.1x, %4d, %4d,\n",
stridx(e.name),
cdef->enumDeclarations.value(e.name) ? 1 : 0,
e.values.count(),
index);
index += e.values.count() * 2;
}
fprintf(out, "\n // enum data: key, value\n");
for (i = 0; i < cdef->enumList.count(); ++i) {
const EnumDef &e = cdef->enumList.at(i);
for (int j = 0; j < e.values.count(); ++j) {
const QByteArray &val = e.values.at(j);
QByteArray code = cdef->qualified.constData();
if (e.isEnumClass)
code += "::" + e.name;
code += "::" + val;
fprintf(out, " %4d, uint(%s),\n",
stridx(val), code.constData());
}
}
}
void Generator::generateMetacall()
{
bool isQObject = (cdef->classname == "QObject");
fprintf(out, "\nint %s::qt_metacall(QMetaObject::Call _c, int _id, void **_a)\n{\n",
cdef->qualified.constData());
if (!purestSuperClass.isEmpty() && !isQObject) {
QByteArray superClass = purestSuperClass;
fprintf(out, " _id = %s::qt_metacall(_c, _id, _a);\n", superClass.constData());
}
fprintf(out, " if (_id < 0)\n return _id;\n");
fprintf(out, " ");
bool needElse = false;
QVector<FunctionDef> methodList;
methodList += cdef->signalList;
methodList += cdef->slotList;
methodList += cdef->methodList;
if (methodList.size()) {
needElse = true;
fprintf(out, "if (_c == QMetaObject::InvokeMetaMethod) {\n");
fprintf(out, " if (_id < %d)\n", methodList.size());
fprintf(out, " qt_static_metacall(this, _c, _id, _a);\n");
fprintf(out, " _id -= %d;\n }", methodList.size());
fprintf(out, " else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {\n");
fprintf(out, " if (_id < %d)\n", methodList.size());
if (methodsWithAutomaticTypesHelper(methodList).isEmpty())
fprintf(out, " *reinterpret_cast<int*>(_a[0]) = -1;\n");
else
fprintf(out, " qt_static_metacall(this, _c, _id, _a);\n");
fprintf(out, " _id -= %d;\n }", methodList.size());
}
if (cdef->propertyList.size()) {
bool needDesignable = false;
bool needScriptable = false;
bool needStored = false;
bool needEditable = false;
bool needUser = false;
for (int i = 0; i < cdef->propertyList.size(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
needDesignable |= p.designable.endsWith(')');
needScriptable |= p.scriptable.endsWith(')');
needStored |= p.stored.endsWith(')');
needEditable |= p.editable.endsWith(')');
needUser |= p.user.endsWith(')');
}
fprintf(out, "\n#ifndef QT_NO_PROPERTIES\n ");
if (needElse)
fprintf(out, "else ");
fprintf(out,
"if (_c == QMetaObject::ReadProperty || _c == QMetaObject::WriteProperty\n"
" || _c == QMetaObject::ResetProperty || _c == QMetaObject::RegisterPropertyMetaType) {\n"
" qt_static_metacall(this, _c, _id, _a);\n"
" _id -= %d;\n }", cdef->propertyList.count());
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::QueryPropertyDesignable) {\n");
if (needDesignable) {
fprintf(out, " bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (!p.designable.endsWith(')'))
continue;
fprintf(out, " case %d: *_b = %s; break;\n",
propindex, p.designable.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out,
" _id -= %d;\n"
" }", cdef->propertyList.count());
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::QueryPropertyScriptable) {\n");
if (needScriptable) {
fprintf(out, " bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (!p.scriptable.endsWith(')'))
continue;
fprintf(out, " case %d: *_b = %s; break;\n",
propindex, p.scriptable.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out,
" _id -= %d;\n"
" }", cdef->propertyList.count());
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::QueryPropertyStored) {\n");
if (needStored) {
fprintf(out, " bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (!p.stored.endsWith(')'))
continue;
fprintf(out, " case %d: *_b = %s; break;\n",
propindex, p.stored.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out,
" _id -= %d;\n"
" }", cdef->propertyList.count());
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::QueryPropertyEditable) {\n");
if (needEditable) {
fprintf(out, " bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (!p.editable.endsWith(')'))
continue;
fprintf(out, " case %d: *_b = %s; break;\n",
propindex, p.editable.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out,
" _id -= %d;\n"
" }", cdef->propertyList.count());
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::QueryPropertyUser) {\n");
if (needUser) {
fprintf(out, " bool *_b = reinterpret_cast<bool*>(_a[0]);\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (!p.user.endsWith(')'))
continue;
fprintf(out, " case %d: *_b = %s; break;\n",
propindex, p.user.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out,
" _id -= %d;\n"
" }", cdef->propertyList.count());
fprintf(out, "\n#endif // QT_NO_PROPERTIES");
}
if (methodList.size() || cdef->signalList.size() || cdef->propertyList.size())
fprintf(out, "\n ");
fprintf(out,"return _id;\n}\n");
}
QMultiMap<QByteArray, int> Generator::automaticPropertyMetaTypesHelper()
{
QMultiMap<QByteArray, int> automaticPropertyMetaTypes;
for (int i = 0; i < cdef->propertyList.size(); ++i) {
const QByteArray propertyType = cdef->propertyList.at(i).type;
if (registerableMetaType(propertyType) && !isBuiltinType(propertyType))
automaticPropertyMetaTypes.insert(propertyType, i);
}
return automaticPropertyMetaTypes;
}
QMap<int, QMultiMap<QByteArray, int> > Generator::methodsWithAutomaticTypesHelper(const QVector<FunctionDef> &methodList)
{
QMap<int, QMultiMap<QByteArray, int> > methodsWithAutomaticTypes;
for (int i = 0; i < methodList.size(); ++i) {
const FunctionDef &f = methodList.at(i);
for (int j = 0; j < f.arguments.count(); ++j) {
const QByteArray argType = f.arguments.at(j).normalizedType;
if (registerableMetaType(argType) && !isBuiltinType(argType))
methodsWithAutomaticTypes[i].insert(argType, j);
}
}
return methodsWithAutomaticTypes;
}
void Generator::generateStaticMetacall()
{
fprintf(out, "void %s::qt_static_metacall(QObject *_o, QMetaObject::Call _c, int _id, void **_a)\n{\n",
cdef->qualified.constData());
bool needElse = false;
bool isUsed_a = false;
if (!cdef->constructorList.isEmpty()) {
fprintf(out, " if (_c == QMetaObject::CreateInstance) {\n");
fprintf(out, " switch (_id) {\n");
for (int ctorindex = 0; ctorindex < cdef->constructorList.count(); ++ctorindex) {
fprintf(out, " case %d: { %s *_r = new %s(", ctorindex,
cdef->classname.constData(), cdef->classname.constData());
const FunctionDef &f = cdef->constructorList.at(ctorindex);
int offset = 1;
int argsCount = f.arguments.count();
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &a = f.arguments.at(j);
if (j)
fprintf(out, ",");
fprintf(out, "(*reinterpret_cast< %s>(_a[%d]))", a.typeNameForCast.constData(), offset++);
}
if (f.isPrivateSignal) {
if (argsCount > 0)
fprintf(out, ", ");
fprintf(out, "%s", QByteArray("QPrivateSignal()").constData());
}
fprintf(out, ");\n");
fprintf(out, " if (_a[0]) *reinterpret_cast<%s**>(_a[0]) = _r; } break;\n",
cdef->hasQGadget ? "void" : "QObject");
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
fprintf(out, " }");
needElse = true;
isUsed_a = true;
}
QVector<FunctionDef> methodList;
methodList += cdef->signalList;
methodList += cdef->slotList;
methodList += cdef->methodList;
if (!methodList.isEmpty()) {
if (needElse)
fprintf(out, " else ");
else
fprintf(out, " ");
fprintf(out, "if (_c == QMetaObject::InvokeMetaMethod) {\n");
if (cdef->hasQObject) {
#ifndef QT_NO_DEBUG
fprintf(out, " Q_ASSERT(staticMetaObject.cast(_o));\n");
#endif
fprintf(out, " %s *_t = static_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
} else {
fprintf(out, " %s *_t = reinterpret_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
}
fprintf(out, " Q_UNUSED(_t)\n");
fprintf(out, " switch (_id) {\n");
for (int methodindex = 0; methodindex < methodList.size(); ++methodindex) {
const FunctionDef &f = methodList.at(methodindex);
Q_ASSERT(!f.normalizedType.isEmpty());
fprintf(out, " case %d: ", methodindex);
if (f.normalizedType != "void")
fprintf(out, "{ %s _r = ", noRef(f.normalizedType).constData());
fprintf(out, "_t->");
if (f.inPrivateClass.size())
fprintf(out, "%s->", f.inPrivateClass.constData());
fprintf(out, "%s(", f.name.constData());
int offset = 1;
int argsCount = f.arguments.count();
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &a = f.arguments.at(j);
if (j)
fprintf(out, ",");
fprintf(out, "(*reinterpret_cast< %s>(_a[%d]))",a.typeNameForCast.constData(), offset++);
isUsed_a = true;
}
if (f.isPrivateSignal) {
if (argsCount > 0)
fprintf(out, ", ");
fprintf(out, "%s", "QPrivateSignal()");
}
fprintf(out, ");");
if (f.normalizedType != "void") {
fprintf(out, "\n if (_a[0]) *reinterpret_cast< %s*>(_a[0]) = _r; } ",
noRef(f.normalizedType).constData());
isUsed_a = true;
}
fprintf(out, " break;\n");
}
fprintf(out, " default: ;\n");
fprintf(out, " }\n");
fprintf(out, " }");
needElse = true;
QMap<int, QMultiMap<QByteArray, int> > methodsWithAutomaticTypes = methodsWithAutomaticTypesHelper(methodList);
if (!methodsWithAutomaticTypes.isEmpty()) {
fprintf(out, " else if (_c == QMetaObject::RegisterMethodArgumentMetaType) {\n");
fprintf(out, " switch (_id) {\n");
fprintf(out, " default: *reinterpret_cast<int*>(_a[0]) = -1; break;\n");
QMap<int, QMultiMap<QByteArray, int> >::const_iterator it = methodsWithAutomaticTypes.constBegin();
const QMap<int, QMultiMap<QByteArray, int> >::const_iterator end = methodsWithAutomaticTypes.constEnd();
for ( ; it != end; ++it) {
fprintf(out, " case %d:\n", it.key());
fprintf(out, " switch (*reinterpret_cast<int*>(_a[1])) {\n");
fprintf(out, " default: *reinterpret_cast<int*>(_a[0]) = -1; break;\n");
auto jt = it->begin();
const auto jend = it->end();
while (jt != jend) {
fprintf(out, " case %d:\n", jt.value());
const QByteArray &lastKey = jt.key();
++jt;
if (jt == jend || jt.key() != lastKey)
fprintf(out, " *reinterpret_cast<int*>(_a[0]) = qRegisterMetaType< %s >(); break;\n", lastKey.constData());
}
fprintf(out, " }\n");
fprintf(out, " break;\n");
}
fprintf(out, " }\n");
fprintf(out, " }");
isUsed_a = true;
}
}
if (!cdef->signalList.isEmpty()) {
Q_ASSERT(needElse); // if there is signal, there was method.
fprintf(out, " else if (_c == QMetaObject::IndexOfMethod) {\n");
fprintf(out, " int *result = reinterpret_cast<int *>(_a[0]);\n");
fprintf(out, " void **func = reinterpret_cast<void **>(_a[1]);\n");
bool anythingUsed = false;
for (int methodindex = 0; methodindex < cdef->signalList.size(); ++methodindex) {
const FunctionDef &f = cdef->signalList.at(methodindex);
if (f.wasCloned || !f.inPrivateClass.isEmpty() || f.isStatic)
continue;
anythingUsed = true;
fprintf(out, " {\n");
fprintf(out, " typedef %s (%s::*_t)(",f.type.rawName.constData() , cdef->classname.constData());
int argsCount = f.arguments.count();
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &a = f.arguments.at(j);
if (j)
fprintf(out, ", ");
fprintf(out, "%s", QByteArray(a.type.name + ' ' + a.rightType).constData());
}
if (f.isPrivateSignal) {
if (argsCount > 0)
fprintf(out, ", ");
fprintf(out, "%s", "QPrivateSignal");
}
if (f.isConst)
fprintf(out, ") const;\n");
else
fprintf(out, ");\n");
fprintf(out, " if (*reinterpret_cast<_t *>(func) == static_cast<_t>(&%s::%s)) {\n",
cdef->classname.constData(), f.name.constData());
fprintf(out, " *result = %d;\n", methodindex);
fprintf(out, " return;\n");
fprintf(out, " }\n }\n");
}
if (!anythingUsed)
fprintf(out, " Q_UNUSED(result);\n Q_UNUSED(func);\n");
fprintf(out, " }");
needElse = true;
}
const QMultiMap<QByteArray, int> automaticPropertyMetaTypes = automaticPropertyMetaTypesHelper();
if (!automaticPropertyMetaTypes.isEmpty()) {
if (needElse)
fprintf(out, " else ");
else
fprintf(out, " ");
fprintf(out, "if (_c == QMetaObject::RegisterPropertyMetaType) {\n");
fprintf(out, " switch (_id) {\n");
fprintf(out, " default: *reinterpret_cast<int*>(_a[0]) = -1; break;\n");
auto it = automaticPropertyMetaTypes.begin();
const auto end = automaticPropertyMetaTypes.end();
while (it != end) {
fprintf(out, " case %d:\n", it.value());
const QByteArray &lastKey = it.key();
++it;
if (it == end || it.key() != lastKey)
fprintf(out, " *reinterpret_cast<int*>(_a[0]) = qRegisterMetaType< %s >(); break;\n", lastKey.constData());
}
fprintf(out, " }\n");
fprintf(out, " }\n");
isUsed_a = true;
needElse = true;
}
if (!cdef->propertyList.empty()) {
bool needGet = false;
bool needTempVarForGet = false;
bool needSet = false;
bool needReset = false;
for (int i = 0; i < cdef->propertyList.size(); ++i) {
const PropertyDef &p = cdef->propertyList.at(i);
needGet |= !p.read.isEmpty() || !p.member.isEmpty();
if (!p.read.isEmpty() || !p.member.isEmpty())
needTempVarForGet |= (p.gspec != PropertyDef::PointerSpec
&& p.gspec != PropertyDef::ReferenceSpec);
needSet |= !p.write.isEmpty() || (!p.member.isEmpty() && !p.constant);
needReset |= !p.reset.isEmpty();
}
fprintf(out, "\n#ifndef QT_NO_PROPERTIES\n ");
if (needElse)
fprintf(out, "else ");
fprintf(out, "if (_c == QMetaObject::ReadProperty) {\n");
if (needGet) {
if (cdef->hasQObject) {
#ifndef QT_NO_DEBUG
fprintf(out, " Q_ASSERT(staticMetaObject.cast(_o));\n");
#endif
fprintf(out, " %s *_t = static_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
} else {
fprintf(out, " %s *_t = reinterpret_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
}
fprintf(out, " Q_UNUSED(_t)\n");
if (needTempVarForGet)
fprintf(out, " void *_v = _a[0];\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (p.read.isEmpty() && p.member.isEmpty())
continue;
QByteArray prefix = "_t->";
if (p.inPrivateClass.size()) {
prefix += p.inPrivateClass + "->";
}
if (p.gspec == PropertyDef::PointerSpec)
fprintf(out, " case %d: _a[0] = const_cast<void*>(reinterpret_cast<const void*>(%s%s())); break;\n",
propindex, prefix.constData(), p.read.constData());
else if (p.gspec == PropertyDef::ReferenceSpec)
fprintf(out, " case %d: _a[0] = const_cast<void*>(reinterpret_cast<const void*>(&%s%s())); break;\n",
propindex, prefix.constData(), p.read.constData());
else if (cdef->enumDeclarations.value(p.type, false))
fprintf(out, " case %d: *reinterpret_cast<int*>(_v) = QFlag(%s%s()); break;\n",
propindex, prefix.constData(), p.read.constData());
else if (!p.read.isEmpty())
fprintf(out, " case %d: *reinterpret_cast< %s*>(_v) = %s%s(); break;\n",
propindex, p.type.constData(), prefix.constData(), p.read.constData());
else
fprintf(out, " case %d: *reinterpret_cast< %s*>(_v) = %s%s; break;\n",
propindex, p.type.constData(), prefix.constData(), p.member.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out, " }");
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::WriteProperty) {\n");
if (needSet) {
if (cdef->hasQObject) {
#ifndef QT_NO_DEBUG
fprintf(out, " Q_ASSERT(staticMetaObject.cast(_o));\n");
#endif
fprintf(out, " %s *_t = static_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
} else {
fprintf(out, " %s *_t = reinterpret_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
}
fprintf(out, " Q_UNUSED(_t)\n");
fprintf(out, " void *_v = _a[0];\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (p.constant)
continue;
if (p.write.isEmpty() && p.member.isEmpty())
continue;
QByteArray prefix = "_t->";
if (p.inPrivateClass.size()) {
prefix += p.inPrivateClass + "->";
}
if (cdef->enumDeclarations.value(p.type, false)) {
fprintf(out, " case %d: %s%s(QFlag(*reinterpret_cast<int*>(_v))); break;\n",
propindex, prefix.constData(), p.write.constData());
} else if (!p.write.isEmpty()) {
fprintf(out, " case %d: %s%s(*reinterpret_cast< %s*>(_v)); break;\n",
propindex, prefix.constData(), p.write.constData(), p.type.constData());
} else {
fprintf(out, " case %d:\n", propindex);
fprintf(out, " if (%s%s != *reinterpret_cast< %s*>(_v)) {\n",
prefix.constData(), p.member.constData(), p.type.constData());
fprintf(out, " %s%s = *reinterpret_cast< %s*>(_v);\n",
prefix.constData(), p.member.constData(), p.type.constData());
if (!p.notify.isEmpty() && p.notifyId != -1) {
const FunctionDef &f = cdef->signalList.at(p.notifyId);
if (f.arguments.size() == 0)
fprintf(out, " Q_EMIT _t->%s();\n", p.notify.constData());
else if (f.arguments.size() == 1 && f.arguments.at(0).normalizedType == p.type)
fprintf(out, " Q_EMIT _t->%s(%s%s);\n",
p.notify.constData(), prefix.constData(), p.member.constData());
}
fprintf(out, " }\n");
fprintf(out, " break;\n");
}
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out, " }");
fprintf(out, " else ");
fprintf(out, "if (_c == QMetaObject::ResetProperty) {\n");
if (needReset) {
if (cdef->hasQObject) {
#ifndef QT_NO_DEBUG
fprintf(out, " Q_ASSERT(staticMetaObject.cast(_o));\n");
#endif
fprintf(out, " %s *_t = static_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
} else {
fprintf(out, " %s *_t = reinterpret_cast<%s *>(_o);\n", cdef->classname.constData(), cdef->classname.constData());
}
fprintf(out, " Q_UNUSED(_t)\n");
fprintf(out, " switch (_id) {\n");
for (int propindex = 0; propindex < cdef->propertyList.size(); ++propindex) {
const PropertyDef &p = cdef->propertyList.at(propindex);
if (!p.reset.endsWith(')'))
continue;
QByteArray prefix = "_t->";
if (p.inPrivateClass.size()) {
prefix += p.inPrivateClass + "->";
}
fprintf(out, " case %d: %s%s; break;\n",
propindex, prefix.constData(), p.reset.constData());
}
fprintf(out, " default: break;\n");
fprintf(out, " }\n");
}
fprintf(out, " }");
fprintf(out, "\n#endif // QT_NO_PROPERTIES");
needElse = true;
}
if (needElse)
fprintf(out, "\n");
if (methodList.isEmpty()) {
fprintf(out, " Q_UNUSED(_o);\n");
if (cdef->constructorList.isEmpty() && automaticPropertyMetaTypes.isEmpty() && methodsWithAutomaticTypesHelper(methodList).isEmpty()) {
fprintf(out, " Q_UNUSED(_id);\n");
fprintf(out, " Q_UNUSED(_c);\n");
}
}
if (!isUsed_a)
fprintf(out, " Q_UNUSED(_a);\n");
fprintf(out, "}\n\n");
}
void Generator::generateSignal(FunctionDef *def,int index)
{
if (def->wasCloned || def->isAbstract)
return;
fprintf(out, "\n// SIGNAL %d\n%s %s::%s(",
index, def->type.name.constData(), cdef->qualified.constData(), def->name.constData());
QByteArray thisPtr = "this";
const char *constQualifier = "";
if (def->isConst) {
thisPtr = "const_cast< " + cdef->qualified + " *>(this)";
constQualifier = "const";
}
Q_ASSERT(!def->normalizedType.isEmpty());
if (def->arguments.isEmpty() && def->normalizedType == "void") {
if (def->isPrivateSignal)
fprintf(out, "QPrivateSignal");
fprintf(out, ")%s\n{\n"
" QMetaObject::activate(%s, &staticMetaObject, %d, Q_NULLPTR);\n"
"}\n", constQualifier, thisPtr.constData(), index);
return;
}
int offset = 1;
for (int j = 0; j < def->arguments.count(); ++j) {
const ArgumentDef &a = def->arguments.at(j);
if (j)
fprintf(out, ", ");
fprintf(out, "%s _t%d%s", a.type.name.constData(), offset++, a.rightType.constData());
}
if (def->isPrivateSignal) {
if (!def->arguments.isEmpty())
fprintf(out, ", ");
fprintf(out, "QPrivateSignal");
}
fprintf(out, ")%s\n{\n", constQualifier);
if (def->type.name.size() && def->normalizedType != "void") {
QByteArray returnType = noRef(def->normalizedType);
if (returnType.endsWith('*')) {
fprintf(out, " %s _t0 = 0;\n", returnType.constData());
} else {
fprintf(out, " %s _t0 = %s();\n", returnType.constData(), returnType.constData());
}
}
fprintf(out, " void *_a[] = { ");
if (def->normalizedType == "void") {
fprintf(out, "Q_NULLPTR");
} else {
if (def->returnTypeIsVolatile)
fprintf(out, "const_cast<void*>(reinterpret_cast<const volatile void*>(&_t0))");
else
fprintf(out, "const_cast<void*>(reinterpret_cast<const void*>(&_t0))");
}
int i;
for (i = 1; i < offset; ++i)
if (def->arguments.at(i - 1).type.isVolatile)
fprintf(out, ", const_cast<void*>(reinterpret_cast<const volatile void*>(&_t%d))", i);
else
fprintf(out, ", const_cast<void*>(reinterpret_cast<const void*>(&_t%d))", i);
fprintf(out, " };\n");
fprintf(out, " QMetaObject::activate(%s, &staticMetaObject, %d, _a);\n", thisPtr.constData(), index);
if (def->normalizedType != "void")
fprintf(out, " return _t0;\n");
fprintf(out, "}\n");
}
static void writePluginMetaData(FILE *out, const QJsonObject &data)
{
const QJsonDocument doc(data);
fputs("\nQT_PLUGIN_METADATA_SECTION\n"
"static const unsigned char qt_pluginMetaData[] = {\n"
" 'Q', 'T', 'M', 'E', 'T', 'A', 'D', 'A', 'T', 'A', ' ', ' ',\n ", out);
#if 0
fprintf(out, "\"%s\";\n", doc.toJson().constData());
#else
const QByteArray binary = doc.toBinaryData();
const int last = binary.size() - 1;
for (int i = 0; i < last; ++i) {
uchar c = (uchar)binary.at(i);
if (c < 0x20 || c >= 0x7f)
fprintf(out, " 0x%02x,", c);
else if (c == '\'' || c == '\\')
fprintf(out, " '\\%c',", c);
else
fprintf(out, " '%c', ", c);
if (!((i + 1) % 8))
fputs("\n ", out);
}
fprintf(out, " 0x%02x\n};\n", (uchar)binary.at(last));
#endif
}
void Generator::generatePluginMetaData()
{
if (cdef->pluginData.iid.isEmpty())
return;
// Write plugin meta data #ifdefed QT_NO_DEBUG with debug=false,
// true, respectively.
QJsonObject data;
const QString debugKey = QStringLiteral("debug");
data.insert(QStringLiteral("IID"), QLatin1String(cdef->pluginData.iid.constData()));
data.insert(QStringLiteral("className"), QLatin1String(cdef->classname.constData()));
data.insert(QStringLiteral("version"), (int)QT_VERSION);
data.insert(debugKey, QJsonValue(false));
data.insert(QStringLiteral("MetaData"), cdef->pluginData.metaData.object());
// Add -M args from the command line:
for (auto it = cdef->pluginData.metaArgs.cbegin(), end = cdef->pluginData.metaArgs.cend(); it != end; ++it)
data.insert(it.key(), it.value());
fputs("\nQT_PLUGIN_METADATA_SECTION const uint qt_section_alignment_dummy = 42;\n\n"
"#ifdef QT_NO_DEBUG\n", out);
writePluginMetaData(out, data);
fputs("\n#else // QT_NO_DEBUG\n", out);
data.remove(debugKey);
data.insert(debugKey, QJsonValue(true));
writePluginMetaData(out, data);
fputs("#endif // QT_NO_DEBUG\n\n", out);
// 'Use' all namespaces.
int pos = cdef->qualified.indexOf("::");
for ( ; pos != -1 ; pos = cdef->qualified.indexOf("::", pos + 2) )
fprintf(out, "using namespace %s;\n", cdef->qualified.left(pos).constData());
fprintf(out, "QT_MOC_EXPORT_PLUGIN(%s, %s)\n\n",
cdef->qualified.constData(), cdef->classname.constData());
}
QT_END_NAMESPACE
|