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
|
/*
* The XML and API file generator module for SIP.
*
* Copyright (c) 2019 Riverbank Computing Limited <info@riverbankcomputing.com>
*
* This file is part of SIP.
*
* This copy of SIP is licensed for use under the terms of the SIP License
* Agreement. See the file LICENSE for more details.
*
* This copy of SIP may also used under the terms of the GNU General Public
* License v2 or v3 as published by the Free Software Foundation which can be
* found in the files LICENSE-GPL2 and LICENSE-GPL3 included in this package.
*
* SIP is supplied WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdio.h>
#include <string.h>
#include "sip.h"
#define XML_VERSION_NR 0 /* The schema version number. */
/* Icon numbers. The values are those used by the eric IDE. */
#define CLASS_ID 1
#define METHOD_ID 4
#define VARIABLE_ID 7
#define ENUM_ID 10
static void apiEnums(sipSpec *pt, moduleDef *mod, classDef *scope, FILE *fp);
static void apiVars(sipSpec *pt, moduleDef *mod, classDef *scope, FILE *fp);
static void apiCtor(sipSpec *pt, moduleDef *mod, classDef *scope, ctorDef *ct,
FILE *fp);
static void apiOverload(sipSpec *pt, moduleDef *mod, classDef *scope,
overDef *od, FILE *fp);
static int apiArgument(sipSpec *pt, argDef *ad, int out, int need_comma,
int names, int defaults, int in_str, FILE *fp);
static void xmlClass(sipSpec *pt, moduleDef *mod, classDef *cd, FILE *fp);
static void xmlEnums(sipSpec *pt, moduleDef *mod, classDef *scope, int indent,
FILE *fp);
static void xmlVars(sipSpec *pt, moduleDef *mod, classDef *scope, int indent,
FILE *fp);
static void xmlFunction(sipSpec *pt, moduleDef *mod, classDef *scope,
memberDef *md, overDef *oloads, int indent, FILE *fp);
static void xmlCtor(sipSpec *pt, moduleDef *mod, classDef *scope, ctorDef *ct,
int indent, FILE *fp);
static void xmlOverload(sipSpec *pt, moduleDef *mod, classDef *scope,
memberDef *md, overDef *od, classDef *xtnds, int stat, int indent,
FILE *fp);
static void xmlCppSignature(FILE *fp, signatureDef *sd, int is_const);
static void xmlArgument(sipSpec *pt, moduleDef *mod, argDef *ad, int out,
KwArgs kwargs, int res_xfer, int indent, FILE *fp);
static void xmlType(sipSpec *pt, moduleDef *mod, argDef *ad, int out,
KwArgs kwargs, FILE *fp);
static void xmlIndent(int indent, FILE *fp);
static void xmlRealName(scopedNameDef *fqcname, const char *member, FILE *fp);
static void xmlRealScopedName(classDef *scope, const char *cppname, FILE *fp);
static const char *pyType(sipSpec *pt, argDef *ad, classDef **scope);
static void exportPythonSignature(sipSpec *pt, FILE *fp, signatureDef *sd,
int names, int defaults, int in_str, int is_signal);
static void restPyEnumMember(enumMemberDef *emd, FILE *fp);
static void restPyAttribute(moduleDef *mod, classDef *scope, nameDef *name,
FILE *fp);
static int restValue(sipSpec *pt, valueDef *value, FILE *fp);
static const char *reflectedSlot(slotType st);
static int hasCppSignature(signatureDef *sd);
/*
* Generate the API file.
*/
void generateAPI(sipSpec *pt, moduleDef *mod, const char *apiFile)
{
overDef *od;
classDef *cd;
FILE *fp;
/* Generate the file. */
if ((fp = fopen(apiFile, "w")) == NULL)
fatal("Unable to create file \"%s\"\n", apiFile);
apiEnums(pt, mod, NULL, fp);
apiVars(pt, mod, NULL, fp);
for (od = mod->overs; od != NULL; od = od->next)
{
if (od->common->module != mod)
continue;
if (od->common->slot != no_slot)
continue;
apiOverload(pt, mod, NULL, od, fp);
}
for (cd = pt->classes; cd != NULL; cd = cd->next)
{
ctorDef *ct;
if (cd->iff->module != mod)
continue;
if (isExternal(cd))
continue;
apiEnums(pt, mod, cd, fp);
apiVars(pt, mod, cd, fp);
for (ct = cd->ctors; ct != NULL; ct = ct->next)
{
if (isPrivateCtor(ct))
continue;
apiCtor(pt, mod, cd, ct, fp);
}
for (od = cd->overs; od != NULL; od = od->next)
{
if (isPrivate(od))
continue;
if (od->common->slot != no_slot)
continue;
apiOverload(pt, mod, cd, od, fp);
}
}
fclose(fp);
}
/*
* Generate an API ctor.
*/
static void apiCtor(sipSpec *pt, moduleDef *mod, classDef *scope, ctorDef *ct,
FILE *fp)
{
int need_comma, a;
/* Do the callable type form. */
fprintf(fp, "%s.", mod->name);
prScopedPythonName(fp, scope->ecd, scope->pyname->text);
fprintf(fp, "?%d(", CLASS_ID);
need_comma = FALSE;
for (a = 0; a < ct->pysig.nrArgs; ++a)
{
argDef *ad = &ct->pysig.args[a];
need_comma = apiArgument(pt, ad, FALSE, need_comma, TRUE, TRUE, FALSE,
fp);
}
fprintf(fp, ")\n");
/* Do the call __init__ form. */
fprintf(fp, "%s.", mod->name);
prScopedPythonName(fp, scope->ecd, scope->pyname->text);
fprintf(fp, ".__init__?%d(self", CLASS_ID);
for (a = 0; a < ct->pysig.nrArgs; ++a)
apiArgument(pt, &ct->pysig.args[a], FALSE, TRUE, TRUE, TRUE, FALSE,
fp);
fprintf(fp, ")\n");
}
/*
* Generate the APIs for all the enums in a scope.
*/
static void apiEnums(sipSpec *pt, moduleDef *mod, classDef *scope, FILE *fp)
{
enumDef *ed;
for (ed = pt->enums; ed != NULL; ed = ed->next)
{
enumMemberDef *emd;
if (ed->module != mod)
continue;
if (ed->ecd != scope)
continue;
if (ed->pyname != NULL)
{
fprintf(fp, "%s.", mod->name);
prScopedPythonName(fp, ed->ecd, ed->pyname->text);
fprintf(fp, "?%d\n", ENUM_ID);
}
for (emd = ed->members; emd != NULL; emd = emd->next)
{
fprintf(fp, "%s.", mod->name);
prScopedPythonName(fp, ed->ecd, emd->pyname->text);
fprintf(fp, "?%d\n", ENUM_ID);
}
}
}
/*
* Generate the APIs for all the variables in a scope.
*/
static void apiVars(sipSpec *pt, moduleDef *mod, classDef *scope, FILE *fp)
{
varDef *vd;
for (vd = pt->vars; vd != NULL; vd = vd->next)
{
if (vd->module != mod)
continue;
if (vd->ecd != scope)
continue;
fprintf(fp, "%s.", mod->name);
prScopedPythonName(fp, vd->ecd, vd->pyname->text);
fprintf(fp, "?%d\n", VARIABLE_ID);
}
}
/*
* Generate a single API overload.
*/
static void apiOverload(sipSpec *pt, moduleDef *mod, classDef *scope,
overDef *od, FILE *fp)
{
fprintf(fp, "%s.", mod->name);
prScopedPythonName(fp, scope, od->common->pyname->text);
fprintf(fp, "?%d", METHOD_ID);
exportPythonSignature(pt, fp, &od->pysig, TRUE, TRUE, FALSE, FALSE);
fprintf(fp, "\n");
}
/*
* Generate the API for an argument.
*/
static int apiArgument(sipSpec *pt, argDef *ad, int out, int need_comma,
int names, int defaults, int in_str, FILE *fp)
{
const char *tname;
classDef *tscope;
if (isArraySize(ad))
return need_comma;
if ((tname = pyType(pt, ad, &tscope)) == NULL)
return need_comma;
if (need_comma)
fprintf(fp, ", ");
prScopedPythonName(fp, tscope, tname);
/*
* Handle the default value is required, but ignore it if it is an output
* only argument.
*/
if (defaults && ad->defval && !out)
{
if (names && ad->name != NULL)
fprintf(fp, " %s", ad->name->text);
fprintf(fp, "=");
prDefaultValue(ad, in_str, fp);
}
return TRUE;
}
/*
* Generate the XML export file.
*/
void generateXML(sipSpec *pt, moduleDef *mod, const char *xmlFile)
{
FILE *fp;
classDef *cd;
memberDef *md;
if ((fp = fopen(xmlFile, "w")) == NULL)
fatal("Unable to create file \"%s\"\n", xmlFile);
fprintf(fp, "<?xml version=\"1.0\"?>\n");
fprintf(fp, "<Module version=\"%u\" name=\"%s\">\n",
XML_VERSION_NR, mod->name);
/*
* Note that we don't yet handle mapped types, templates or exceptions.
*/
for (cd = pt->classes; cd != NULL; cd = cd->next)
{
if (cd->iff->module != mod)
continue;
if (isExternal(cd))
continue;
xmlClass(pt, mod, cd, fp);
}
for (cd = mod->proxies; cd != NULL; cd = cd->next)
xmlClass(pt, mod, cd, fp);
xmlEnums(pt, mod, NULL, 1, fp);
xmlVars(pt, mod, NULL, 1, fp);
for (md = mod->othfuncs; md != NULL; md = md->next)
xmlFunction(pt, mod, NULL, md, mod->overs, 1, fp);
fprintf(fp, "</Module>\n");
fclose(fp);
}
/*
* Generate a 'realname' attribute containing a fully qualified C/C++ name of
* an object.
*/
static void xmlRealScopedName(classDef *scope, const char *cppname, FILE *fp)
{
const char *sep = "";
fprintf(fp, " realname=\"");
if (scope != NULL)
{
scopedNameDef *snd;
for (snd = removeGlobalScope(classFQCName(scope)); snd != NULL; snd = snd->next)
{
fprintf(fp, "%s%s", sep, snd->name);
sep = "::";
}
}
fprintf(fp, "%s%s\"", sep, cppname);
}
/*
* Generate a 'realname' attribute containing a fully qualified C/C++ name.
*/
static void xmlRealName(scopedNameDef *fqcname, const char *member, FILE *fp)
{
const char *sep = "";
scopedNameDef *snd;
fprintf(fp, " realname=\"");
for (snd = removeGlobalScope(fqcname); snd != NULL; snd = snd->next)
{
fprintf(fp, "%s%s", sep, snd->name);
sep = "::";
}
if (member != NULL)
fprintf(fp, "::%s", member);
fprintf(fp, "\"");
}
/*
* Generate the XML for a class.
*/
static void xmlClass(sipSpec *pt, moduleDef *mod, classDef *cd, FILE *fp)
{
int indent = 1;
ctorDef *ct;
memberDef *md;
if (isOpaque(cd))
{
xmlIndent(indent, fp);
fprintf(fp, "<OpaqueClass name=\"");
prScopedPythonName(fp, cd->ecd, cd->pyname->text);
fprintf(fp, "\"/>\n");
return;
}
if (!isHiddenNamespace(cd))
{
xmlIndent(indent++, fp);
fprintf(fp, "<Class name=\"");
prScopedPythonName(fp, cd->ecd, cd->pyname->text);
fprintf(fp, "\"");
xmlRealName(classFQCName(cd), NULL, fp);
if (cd->picklecode != NULL)
fprintf(fp, " pickle=\"1\"");
if (cd->convtocode != NULL)
fprintf(fp, " convert=\"1\"");
if (cd->convfromcode != NULL)
fprintf(fp, " convertfrom=\"1\"");
if (cd->real != NULL)
fprintf(fp, " extends=\"%s\"", cd->real->iff->module->name);
if (cd->pyqt_flags_enums != NULL)
{
const char *sep;
stringList *sl;
fprintf(fp, " flagsenums=\"");
sep = "";
for (sl = cd->pyqt_flags_enums; sl != NULL; sl = sl->next)
{
fprintf(fp, "%s%s", sep, sl->s);
sep = " ";
}
fprintf(fp, "\"");
}
if (cd->supers != NULL)
{
classList *cl;
fprintf(fp, " inherits=\"");
for (cl = cd->supers; cl != NULL; cl = cl->next)
{
if (cl != cd->supers)
fprintf(fp, " ");
restPyClass(cl->cd, fp);
}
fprintf(fp, "\"");
}
fprintf(fp, ">\n");
}
for (ct = cd->ctors; ct != NULL; ct = ct->next)
{
if (isPrivateCtor(ct))
continue;
xmlCtor(pt, mod, cd, ct, indent, fp);
}
xmlEnums(pt, mod, cd, indent, fp);
xmlVars(pt, mod, cd, indent, fp);
for (md = cd->members; md != NULL; md = md->next)
xmlFunction(pt, mod, cd, md, cd->overs, indent, fp);
if (!isHiddenNamespace(cd))
{
xmlIndent(--indent, fp);
fprintf(fp, "</Class>\n");
}
}
/*
* Generate the XML for all the enums in a scope.
*/
static void xmlEnums(sipSpec *pt, moduleDef *mod, classDef *scope, int indent,
FILE *fp)
{
enumDef *ed;
for (ed = pt->enums; ed != NULL; ed = ed->next)
{
if (ed->module != mod)
continue;
if (ed->ecd != scope)
continue;
if (ed->pyname != NULL)
{
enumMemberDef *emd;
xmlIndent(indent++, fp);
fprintf(fp, "<Enum name=\"");
prScopedPythonName(fp, ed->ecd, ed->pyname->text);
fprintf(fp, "\"");
xmlRealName(ed->fqcname, NULL, fp);
fprintf(fp, ">\n");
for (emd = ed->members; emd != NULL; emd = emd->next)
{
xmlIndent(indent, fp);
fprintf(fp, "<EnumMember name=\"");
prScopedPythonName(fp, ed->ecd, ed->pyname->text);
fprintf(fp, ".%s\"", emd->pyname->text);
xmlRealName(ed->fqcname, emd->cname, fp);
fprintf(fp, "/>\n");
}
xmlIndent(--indent, fp);
fprintf(fp, "</Enum>\n");
}
else
{
enumMemberDef *emd;
for (emd = ed->members; emd != NULL; emd = emd->next)
{
xmlIndent(indent, fp);
fprintf(fp, "<Member name=\"");
prScopedPythonName(fp, ed->ecd, emd->pyname->text);
fprintf(fp, "\"");
xmlRealScopedName(scope, emd->cname, fp);
fprintf(fp, " const=\"1\" typename=\"int\"/>\n");
}
}
}
}
/*
* Generate the XML for all the variables in a scope.
*/
static void xmlVars(sipSpec *pt, moduleDef *mod, classDef *scope, int indent,
FILE *fp)
{
varDef *vd;
for (vd = pt->vars; vd != NULL; vd = vd->next)
{
if (vd->module != mod)
continue;
if (vd->ecd != scope)
continue;
xmlIndent(indent, fp);
fprintf(fp, "<Member name=\"");
prScopedPythonName(fp, vd->ecd, vd->pyname->text);
fprintf(fp, "\"");
xmlRealName(vd->fqcname, NULL, fp);
if (isConstArg(&vd->type) || scope == NULL)
fprintf(fp, " const=\"1\"");
if (isStaticVar(vd))
fprintf(fp, " static=\"1\"");
xmlType(pt, mod, &vd->type, FALSE, NoKwArgs, fp);
fprintf(fp, "/>\n");
}
}
/*
* Generate the XML for a ctor.
*/
static void xmlCtor(sipSpec *pt, moduleDef *mod, classDef *scope, ctorDef *ct,
int indent, FILE *fp)
{
int a;
xmlIndent(indent++, fp);
fprintf(fp, "<Function name=\"");
prScopedPythonName(fp, scope, "__init__");
fprintf(fp, "\"");
xmlRealScopedName(scope, "__init__", fp);
if (hasCppSignature(ct->cppsig))
{
fprintf(fp, " cppsig=\"");
xmlCppSignature(fp, ct->cppsig, FALSE);
fprintf(fp, "\"");
}
/* Handle the trivial case. */
if (ct->pysig.nrArgs == 0)
{
fprintf(fp, "/>\n");
return;
}
fprintf(fp, ">\n");
for (a = 0; a < ct->pysig.nrArgs; ++a)
{
argDef *ad = &ct->pysig.args[a];
if (isInArg(ad))
xmlArgument(pt, mod, ad, FALSE, ct->kwargs, FALSE, indent, fp);
if (isOutArg(ad))
xmlArgument(pt, mod, ad, TRUE, ct->kwargs, FALSE, indent, fp);
}
xmlIndent(--indent, fp);
fprintf(fp, "</Function>\n");
}
/*
* Generate the XML for a function.
*/
static void xmlFunction(sipSpec *pt, moduleDef *mod, classDef *scope,
memberDef *md, overDef *oloads, int indent, FILE *fp)
{
overDef *od;
for (od = oloads; od != NULL; od = od->next)
{
int isstat;
classDef *xtnds;
if (od->common != md)
continue;
if (isPrivate(od))
continue;
if (isSignal(od))
{
int a;
xmlIndent(indent++, fp);
fprintf(fp, "<Signal name=\"");
prScopedPythonName(fp, scope, md->pyname->text);
fprintf(fp, "\"");
xmlRealScopedName(scope, od->cppname, fp);
if (hasCppSignature(od->cppsig))
{
fprintf(fp, " cppsig=\"");
xmlCppSignature(fp, od->cppsig, FALSE);
fprintf(fp, "\"");
}
/* Handle the trivial case. */
if (od->pysig.nrArgs == 0)
{
fprintf(fp, "/>\n");
continue;
}
fprintf(fp, ">\n");
for (a = 0; a < od->pysig.nrArgs; ++a)
{
argDef *ad = &od->pysig.args[a];
xmlArgument(pt, mod, ad, FALSE, od->kwargs, FALSE, indent, fp);
}
xmlIndent(--indent, fp);
fprintf(fp, "</Signal>\n");
continue;
}
xtnds = NULL;
isstat = (scope == NULL || scope->iff->type == namespace_iface || isStatic(od));
if (scope == NULL && md->slot != no_slot && od->pysig.args[0].atype == class_type)
{
xtnds = od->pysig.args[0].u.cd;
isstat = FALSE;
}
xmlOverload(pt, mod, scope, md, od, xtnds, isstat, indent, fp);
}
}
/*
* Generate the XML for an overload.
*/
static void xmlOverload(sipSpec *pt, moduleDef *mod, classDef *scope,
memberDef *md, overDef *od, classDef *xtnds, int stat, int indent,
FILE *fp)
{
const char *name, *cppname = od->cppname;
int a, no_res;
xmlIndent(indent++, fp);
fprintf(fp, "<Function name=\"");
if (isReflected(od))
{
if ((name = reflectedSlot(md->slot)) != NULL)
cppname = name;
else
name = md->pyname->text;
}
else
{
name = md->pyname->text;
}
prScopedPythonName(fp, scope, name);
fprintf(fp, "\"");
xmlRealScopedName(scope, cppname, fp);
if (hasCppSignature(od->cppsig))
{
fprintf(fp, " cppsig=\"");
xmlCppSignature(fp, od->cppsig, isConst(od));
fprintf(fp, "\"");
}
if (isAbstract(od))
fprintf(fp, " abstract=\"1\"");
if (stat)
fprintf(fp, " static=\"1\"");
if (isSlot(od))
fprintf(fp, " slot=\"1\"");
if (isVirtual(od))
{
fprintf(fp, " virtual=\"1\"");
}
if (xtnds != NULL)
{
fprintf(fp, " extends=\"");
prScopedPythonName(fp, xtnds->ecd, xtnds->pyname->text);
fprintf(fp, "\"");
}
/* An empty type hint specifies a void return. */
if (od->pysig.result.typehint_out != NULL && od->pysig.result.typehint_out->raw_hint[0] == '\0')
no_res = TRUE;
else
no_res = (od->pysig.result.atype == void_type && od->pysig.result.nrderefs == 0);
/* Handle the trivial case. */
if (no_res && od->pysig.nrArgs == 0)
{
fprintf(fp, "/>\n");
return;
}
fprintf(fp, ">\n");
if (!no_res)
xmlArgument(pt, mod, &od->pysig.result, TRUE, NoKwArgs,
isResultTransferredBack(od), indent, fp);
for (a = 0; a < od->pysig.nrArgs; ++a)
{
argDef *ad = &od->pysig.args[a];
/*
* Ignore the first argument of non-reflected number slots and the
* second argument of reflected number slots.
*/
if (isNumberSlot(md) && od->pysig.nrArgs == 2)
if ((a == 0 && !isReflected(od)) || (a == 1 && isReflected(od)))
continue;
if (isInArg(ad))
xmlArgument(pt, mod, ad, FALSE, od->kwargs, FALSE, indent, fp);
if (isOutArg(ad))
xmlArgument(pt, mod, ad, TRUE, od->kwargs, FALSE, indent, fp);
}
xmlIndent(--indent, fp);
fprintf(fp, "</Function>\n");
}
/*
* Return TRUE if there is a C/C++ signature.
*/
static int hasCppSignature(signatureDef *sd)
{
int a;
if (sd == NULL)
return FALSE;
/*
* See if there are any arguments that could only have come from
* handwritten code.
*/
for (a = 0; a < sd->nrArgs; ++a)
{
switch (sd->args[a].atype)
{
case pyobject_type:
case pytuple_type:
case pylist_type:
case pydict_type:
case pycallable_type:
case pyslice_type:
case pytype_type:
case pybuffer_type:
case capsule_type:
return FALSE;
default:
break;
}
}
return TRUE;
}
/*
* Generate the XML for a C++ signature.
*/
static void xmlCppSignature(FILE *fp, signatureDef *sd, int is_const)
{
int a;
prcode(fp, "%M");
normaliseArgs(sd);
prcode(fp, "(");
for (a = 0; a < sd->nrArgs; ++a)
{
argDef *ad = &sd->args[a];
if (a > 0)
prcode(fp, ",");
generateBaseType(NULL, ad, TRUE, STRIP_GLOBAL, fp);
}
prcode(fp, ")%s", (is_const ? " const" : ""));
restoreArgs(sd);
prcode(fp, "%M");
}
/*
* Generate the XML for an argument.
*/
static void xmlArgument(sipSpec *pt, moduleDef *mod, argDef *ad, int out,
KwArgs kwargs, int res_xfer, int indent, FILE *fp)
{
if (isArraySize(ad))
return;
xmlIndent(indent, fp);
fprintf(fp, "<%s", (out ? "Return" : "Argument"));
xmlType(pt, mod, ad, out, kwargs, fp);
if (!out)
{
if (isAllowNone(ad))
fprintf(fp, " allownone=\"1\"");
if (isDisallowNone(ad))
fprintf(fp, " disallownone=\"1\"");
if (isTransferred(ad))
fprintf(fp, " transfer=\"to\"");
else if (isThisTransferred(ad))
fprintf(fp, " transfer=\"this\"");
}
if (res_xfer || isTransferredBack(ad))
fprintf(fp, " transfer=\"back\"");
fprintf(fp, "/>\n");
}
/*
* Generate the XML for a type.
*/
static void xmlType(sipSpec *pt, moduleDef *mod, argDef *ad, int out,
KwArgs kwargs, FILE *fp)
{
const char *type_name;
classDef *type_scope;
typeHintDef *thd;
fprintf(fp, " typename=\"");
/* Handle the argument name. */
if (!out && ad->name != NULL)
{
if (kwargs == AllKwArgs || (kwargs == OptionalKwArgs && ad->defval != NULL))
fprintf(fp, "%s: ", ad->name->text);
}
/* Use any explicit type hint unless the argument is constrained. */
thd = (out ? ad->typehint_out : (isConstrained(ad) ? NULL : ad->typehint_in));
if (thd != NULL)
{
pyiTypeHint(pt, thd, mod, out, NULL, FALSE, TRUE, fp);
}
else
{
switch (ad->atype)
{
case class_type:
restPyClass(ad->u.cd, fp);
break;
case enum_type:
if (ad->u.ed->pyname != NULL)
restPyEnum(ad->u.ed, fp);
else
fprintf(fp, "int");
break;
case mapped_type:
/* There would normally be a type hint. */
fprintf(fp, "unknown-type");
break;
default:
if ((type_name = pyType(pt, ad, &type_scope)) != NULL)
prScopedPythonName(fp, type_scope, type_name);
}
}
if (!out && ad->name != NULL && ad->defval != NULL)
{
fprintf(fp, " = ");
/*
* Try and convert the value to a reST reference. We don't try very
* hard but will get most cases.
*/
if (!restValue(pt, ad->defval, fp))
prDefaultValue(ad, FALSE, fp);
}
fprintf(fp, "\"");
}
/*
* Generate the indentation for a line.
*/
static void xmlIndent(int indent, FILE *fp)
{
while (indent-- > 0)
fprintf(fp, " ");
}
/*
* Get the Python representation of a type.
*/
static const char *pyType(sipSpec *pt, argDef *ad, classDef **scope)
{
const char *type_name;
*scope = NULL;
/* For classes and mapped types we need the default implementation. */
if (ad->atype == class_type || ad->atype == mapped_type)
{
classDef *def_cd = NULL;
mappedTypeDef *def_mtd = NULL;
ifaceFileDef *iff;
if (ad->atype == class_type)
{
iff = ad->u.cd->iff;
if (iff->api_range == NULL)
{
/* There is only one implementation. */
def_cd = ad->u.cd;
iff = NULL;
}
}
else
{
iff = ad->u.mtd->iff;
if (iff->api_range == NULL)
{
/* There is only one implementation. */
def_mtd = ad->u.mtd;
iff = NULL;
}
}
if (iff != NULL)
{
int def_api;
/* Find the default implementation. */
def_api = findAPI(pt, iff->api_range->api_name->text)->from;
for (iff = iff->first_alt; iff != NULL; iff = iff->next_alt)
{
apiVersionRangeDef *avd = iff->api_range;
if (avd->from > 0 && avd->from > def_api)
continue;
if (avd->to > 0 && avd->to <= def_api)
continue;
/* It's within range. */
break;
}
/* Find the corresponding class or mapped type. */
for (def_cd = pt->classes; def_cd != NULL; def_cd = def_cd->next)
if (def_cd->iff == iff)
break;
if (def_cd == NULL)
for (def_mtd = pt->mappedtypes; def_mtd != NULL; def_mtd = def_mtd->next)
if (def_mtd->iff == iff)
break;
}
/* Now handle the correct implementation. */
if (def_cd != NULL)
{
*scope = def_cd->ecd;
type_name = def_cd->pyname->text;
}
else
{
if (def_mtd != NULL && def_mtd->pyname != NULL)
type_name = def_mtd->pyname->text;
else
type_name = "unknown-type";
}
return type_name;
}
switch (ad->atype)
{
case capsule_type:
type_name = scopedNameTail(ad->u.cap);
break;
case struct_type:
case void_type:
type_name = "sip.voidptr";
break;
case enum_type:
if (ad->u.ed->pyname != NULL)
{
type_name = ad->u.ed->pyname->text;
*scope = ad->u.ed->ecd;
}
else
type_name = "int";
break;
case ustring_type:
/* Correct for Python v3. */
type_name = "bytes";
break;
case string_type:
case sstring_type:
case wstring_type:
case ascii_string_type:
case latin1_string_type:
case utf8_string_type:
type_name = isArray(ad) ? "bytes" : "str";
break;
case byte_type:
case sbyte_type:
case ubyte_type:
case ushort_type:
case uint_type:
case long_type:
case longlong_type:
case ulong_type:
case ulonglong_type:
case short_type:
case int_type:
case cint_type:
case ssize_type:
case size_type:
type_name = "int";
break;
case float_type:
case cfloat_type:
case double_type:
case cdouble_type:
type_name = "float";
break;
case bool_type:
case cbool_type:
type_name = "bool";
break;
case pyobject_type:
type_name = "object";
break;
case pytuple_type:
type_name = "tuple";
break;
case pylist_type:
type_name = "list";
break;
case pydict_type:
type_name = "dict";
break;
case pycallable_type:
type_name = "callable";
break;
case pyslice_type:
type_name = "slice";
break;
case pytype_type:
type_name = "type";
break;
case pybuffer_type:
type_name = "buffer";
break;
case ellipsis_type:
type_name = "...";
break;
default:
type_name = NULL;
}
return type_name;
}
/*
* Generate a Python signature.
*/
static void exportPythonSignature(sipSpec *pt, FILE *fp, signatureDef *sd,
int names, int defaults, int in_str, int is_signal)
{
int need_comma = FALSE, is_res, nr_out, a;
if (is_signal)
{
if (sd->nrArgs != 0)
fprintf(fp, "[");
}
else
{
fprintf(fp, "(");
}
nr_out = 0;
for (a = 0; a < sd->nrArgs; ++a)
{
argDef *ad = &sd->args[a];
if (isOutArg(ad))
++nr_out;
if (!isInArg(ad))
continue;
need_comma = apiArgument(pt, ad, FALSE, need_comma, names, defaults,
in_str, fp);
}
if (is_signal)
{
if (sd->nrArgs != 0)
fprintf(fp, "]");
}
else
{
fprintf(fp, ")");
}
is_res = !(sd->result.atype == void_type && sd->result.nrderefs == 0);
if (is_res || nr_out > 0)
{
fprintf(fp, " -> ");
if ((is_res && nr_out > 0) || nr_out > 1)
fprintf(fp, "(");
if (is_res)
need_comma = apiArgument(pt, &sd->result, TRUE, FALSE, FALSE,
FALSE, in_str, fp);
else
need_comma = FALSE;
for (a = 0; a < sd->nrArgs; ++a)
{
argDef *ad = &sd->args[a];
if (isOutArg(ad))
/* We don't want the name in the result tuple. */
need_comma = apiArgument(pt, ad, TRUE, need_comma, FALSE,
FALSE, in_str, fp);
}
if ((is_res && nr_out > 0) || nr_out > 1)
fprintf(fp, ")");
}
}
/*
* Generate a fully qualified class name as a reST reference.
*/
void restPyClass(classDef *cd, FILE *fp)
{
fprintf(fp, ":sip:ref:`~%s.", cd->iff->module->fullname->text);
prScopedPythonName(fp, cd->ecd, cd->pyname->text);
fprintf(fp, "`");
}
/*
* Generate a fully qualified enum name as a reST reference.
*/
void restPyEnum(enumDef *ed, FILE *fp)
{
fprintf(fp, ":sip:ref:`~%s.", ed->module->fullname->text);
prScopedPythonName(fp, ed->ecd, ed->pyname->text);
fprintf(fp, "`");
}
/*
* Generate a fully qualified attribute name as a reST reference.
*/
static void restPyEnumMember(enumMemberDef *emd, FILE *fp)
{
fprintf(fp, ":sip:ref:`~%s.", emd->ed->module->fullname->text);
prScopedPythonName(fp, emd->ed->ecd, emd->ed->pyname->text);
fprintf(fp, ".%s`", emd->pyname->text);
}
/*
* Generate a fully qualified attribute name as a reST reference.
*/
static void restPyAttribute(moduleDef *mod, classDef *scope, nameDef *name,
FILE *fp)
{
fprintf(fp, ":sip:ref:`~%s.", mod->fullname->text);
prScopedPythonName(fp, scope, name->text);
fprintf(fp, "`");
}
/*
* Generate a reST reference for a scoped name if possible. Return TRUE if
* something was generated.
*/
static int restValue(sipSpec *pt, valueDef *value, FILE *fp)
{
const char *name;
scopedNameDef *target, *scope, *snd;
varDef *vd;
enumDef *ed;
/* The value must be a scoped name and we don't handle expressions. */
if (value->vtype != scoped_value || value->next != NULL)
return FALSE;
target = value->u.vscp;
/* See if it is an attribute. */
for (vd = pt->vars; vd != NULL; vd = vd->next)
if (compareScopedNames(vd->fqcname, target) == 0)
{
restPyAttribute(vd->module, vd->ecd, vd->pyname, fp);
return TRUE;
}
/* Get the name and scope. */
name = scopedNameTail(target);
scope = NULL;
for (snd = target; snd->name != name; snd = snd->next)
appendScopedName(&scope, text2scopePart(snd->name));
/* See if it is an enum member. */
for (ed = pt->enums; ed != NULL; ed = ed->next)
{
enumMemberDef *emd;
/*
* Look for the member name first before working out if it is the
* correct enum.
*/
for (emd = ed->members; emd != NULL; emd = emd->next)
{
if (strcmp(emd->cname, name) == 0)
{
if (isScopedEnum(ed))
{
/*
* It's a scoped enum so the fully qualified name of the
* enum must match the scope of the name.
*/
if (scope != NULL && compareScopedNames(ed->fqcname, scope) == 0)
{
restPyEnumMember(emd, fp);
freeScopedName(scope);
return TRUE;
}
}
else
{
/*
* It's a traditional enum so the scope of the enum must
* match the scope of the name.
*/
if ((ed->ecd == NULL && scope == NULL) || (ed->ecd != NULL && scope != NULL && compareScopedNames(ed->ecd->iff->fqcname, scope) == 0))
{
if (ed->fqcname == NULL)
restPyAttribute(ed->module, ed->ecd, emd->pyname,
fp);
else
restPyEnumMember(emd, fp);
freeScopedName(scope);
return TRUE;
}
}
break;
}
}
}
freeScopedName(scope);
return FALSE;
}
/*
* Return the name of the reflected version of a slot or NULL if it doesn't
* have one.
*/
static const char *reflectedSlot(slotType st)
{
switch (st)
{
case add_slot:
return "__radd__";
case sub_slot:
return "__rsub__";
case mul_slot:
return "__rmul__";
case matmul_slot:
return "__rmatmul__";
case truediv_slot:
return "__rtruediv__";
case floordiv_slot:
return "__rfloordiv__";
case mod_slot:
return "__rmod__";
case lshift_slot:
return "__rlshift__";
case rshift_slot:
return "__rrshift__";
case and_slot:
return "__rand__";
case or_slot:
return "__ror__";
case xor_slot:
return "__rxor__";
default:
break;
}
return NULL;
}
|