~ubuntu-branches/ubuntu/gutsy/sip4-qt3/gutsy

« back to all changes in this revision

Viewing changes to sipgen/gencode.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-24 23:38:46 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070524233846-1qx27pdt889yg9t5
Tags: 4.6-1ubuntu1
* Merge from Debian unstable.
* Remaining Ubuntu changes:
  - Build a python-sip4-dbg package.
  - Build the normal extension with -O2, the debug extension with -O0.
  - Set Ubuntu maintainer address.
* Ubuntu changes dropped:
  - python2.5 support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * The code generator module for SIP.
3
3
 *
4
 
 * Copyright (c) 2006
 
4
 * Copyright (c) 2007
5
5
 *      Riverbank Computing Limited <info@riverbankcomputing.co.uk>
6
6
 * 
7
7
 * This file is part of SIP.
24
24
#include "sip.h"
25
25
 
26
26
 
 
27
/*
 
28
 * These must match the values of SIP_TYPE_FLAGS_SHIFT and SIP_TYPE_FLAGS_MASK
 
29
 * in siplib/sip.h.
 
30
 */
 
31
#define TYPE_FLAGS_SHIFT        8
 
32
#define TYPE_FLAGS_MASK         0x0f00
 
33
 
 
34
 
27
35
/* Control what generateSingleArg() actually generates. */
28
36
typedef enum {
29
37
    Call,
34
42
 
35
43
/* An entry in the sorted array of methods. */
36
44
typedef struct {
37
 
    memberDef   *md;                /* The method. */
38
 
    int         is_static;          /* Set if all overloads are static. */
 
45
    memberDef *md;                      /* The method. */
 
46
    int is_static;                      /* Set if all overloads are static. */
39
47
} sortedMethTab;
40
48
 
41
49
 
42
 
static int currentLineNr;           /* Current output line number. */
43
 
static char *currentFileName;       /* Current output file name. */
44
 
static int previousLineNr;          /* Previous output line number. */
45
 
static char *previousFileName;      /* Previous output file name. */
46
 
static int exceptions;              /* Set if exceptions are enabled. */
47
 
static int tracing;                 /* Set if tracing is enabled. */
48
 
static int generating_c;            /* Set if generating C. */
49
 
static int release_gil;             /* Set if always releasing the GIL. */
 
50
static int currentLineNr;               /* Current output line number. */
 
51
static char *currentFileName;           /* Current output file name. */
 
52
static int previousLineNr;              /* Previous output line number. */
 
53
static char *previousFileName;          /* Previous output file name. */
 
54
static int exceptions;                  /* Set if exceptions are enabled. */
 
55
static int tracing;                     /* Set if tracing is enabled. */
 
56
static int generating_c;                /* Set if generating C. */
 
57
static int release_gil;                 /* Set if always releasing the GIL. */
50
58
static const char *prcode_last = NULL;  /* The last prcode format string. */
51
 
static int prcode_xml = FALSE;      /* Set if prcode is XML aware. */
 
59
static int prcode_xml = FALSE;          /* Set if prcode is XML aware. */
52
60
 
53
61
 
54
62
static void generateDocumentation(sipSpec *, char *);
184
192
static void generateMappedTypeFromVoid(mappedTypeDef *mtd, const char *cname,
185
193
        const char *vname, FILE *fp);
186
194
static int generateSubClassConvertors(sipSpec *pt, FILE *fp);
 
195
static void generateRegisterMetaType(classDef *cd, FILE *fp);
187
196
 
188
197
 
189
198
/*
512
521
"#define sipExportSymbol             sipAPI_%s->api_export_symbol\n"
513
522
"#define sipImportSymbol             sipAPI_%s->api_import_symbol\n"
514
523
"#define sipRegisterIntTypes         sipAPI_%s->api_register_int_types\n"
 
524
"#define sipParseSignature           sipAPI_%s->api_parse_signature\n"
 
525
"#define sipFindClass                sipAPI_%s->api_find_class\n"
 
526
"#define sipFindNamedEnum            sipAPI_%s->api_find_named_enum\n"
 
527
"#define sipString_AsChar            sipAPI_%s->api_string_as_char\n"
 
528
"#define sipUnicode_AsWChar          sipAPI_%s->api_unicode_as_wchar\n"
 
529
"#define sipUnicode_AsWString        sipAPI_%s->api_unicode_as_wstring\n"
 
530
        ,mname
 
531
        ,mname
 
532
        ,mname
 
533
        ,mname
 
534
        ,mname
 
535
        ,mname
515
536
        ,mname
516
537
        ,mname
517
538
        ,mname
620
641
        ,mname
621
642
        ,mname,mname);
622
643
 
623
 
    for (mld = pt->allimports; mld != NULL; mld = mld->next)
 
644
    for (mld = pt->module->allimports; mld != NULL; mld = mld->next)
624
645
        prcode(fp,
625
646
"extern const sipExportedModuleDef *sipModuleAPI_%s_%s;\n"
626
647
            ,mname,mld->module->name);
627
648
 
 
649
    if (optQ_OBJECT4(pt))
 
650
        prcode(fp,
 
651
"\n"
 
652
"typedef const QMetaObject *(*sip_qt_metaobject_func)(sipWrapper *,sipWrapperType *,const QMetaObject *);\n"
 
653
"extern sip_qt_metaobject_func sip_%s_qt_metaobject;\n"
 
654
"\n"
 
655
"typedef int (*sip_qt_metacall_func)(sipWrapper *,sipWrapperType *,QMetaObject::Call,int,void **);\n"
 
656
"extern sip_qt_metacall_func sip_%s_qt_metacall;\n"
 
657
            , mname
 
658
            , mname);
 
659
 
628
660
    /*
629
661
     * Note that we don't forward declare the virtual handlers.  This is
630
662
     * because we would need to #include everything needed for their
708
740
 
709
741
    generateUsedIncludes(pt->used, FALSE, fp);
710
742
 
 
743
    /*
 
744
     * If there should be a Qt support API then generate stubs values for the
 
745
     * optional parts.  These should be undefined in %ModuleCode if a C++
 
746
     * implementation is provided.
 
747
     */
 
748
    if (pt->qobjclass >= 0)
 
749
        prcode(fp,
 
750
"\n"
 
751
"#define sipQtIsQtSignal                     0\n"
 
752
"#define sipQtCreateUniversalSignalShortcut  0\n"
 
753
"#define sipQtCreateUniversalSignal          0\n"
 
754
"#define sipQtFindUniversalSignalShortcut    0\n"
 
755
"#define sipQtFindUniversalSignal            0\n"
 
756
"#define sipQtEmitSignalShortcut             0\n"
 
757
"#define sipQtEmitSignal                     0\n"
 
758
            );
 
759
 
711
760
    /* Define the names. */
712
761
    noIntro = TRUE;
713
762
 
1062
1111
 
1063
1112
        for (td = pt->typedefs; td != NULL; td = td->next)
1064
1113
        {
1065
 
            char *mname, *sat;
 
1114
            char *tdmname, *sat;
1066
1115
            scopedNameDef *fqname;
1067
1116
 
1068
1117
            if (td->module != pt->module)
1069
1118
                continue;
1070
1119
 
1071
1120
            fqname = NULL;
1072
 
            mname = NULL;
 
1121
            tdmname = NULL;
1073
1122
            sat = "unknown";
1074
1123
 
1075
1124
            switch (td->type.atype)
1086
1135
                sat = (td->type.nrderefs == 0 ? "uchar" : "ustring");
1087
1136
                break;
1088
1137
 
 
1138
            case wstring_type:
 
1139
                sat = (td->type.nrderefs == 0 ? "wchar" : "wstring");
 
1140
                break;
 
1141
 
1089
1142
            case short_type:
1090
1143
                sat = "short";
1091
1144
                break;
1145
1198
                    sat = "enum";
1146
1199
 
1147
1200
                    if (td->type.u.ed->module != pt->module)
1148
 
                        mname = td->type.u.ed->module->name;
 
1201
                        tdmname = td->type.u.ed->module->fullname;
1149
1202
                }
1150
1203
                break;
1151
1204
 
1154
1207
                fqname = classFQCName(td->type.u.cd);
1155
1208
 
1156
1209
                if (td->type.u.cd->iff->module != pt->module)
1157
 
                    mname = td->type.u.cd->iff->module->name;
 
1210
                    tdmname = td->type.u.cd->iff->module->fullname;
1158
1211
                break;
1159
1212
 
1160
1213
            case mapped_type:
1162
1215
                fqname = td->type.u.mtd->iff->fqcname;
1163
1216
 
1164
1217
                if (td->type.u.mtd->iff->module != pt->module)
1165
 
                    mname = td->type.u.mtd->iff->module->name;
 
1218
                    tdmname = td->type.u.mtd->iff->module->fullname;
1166
1219
                break;
1167
1220
            }
1168
1221
 
1174
1227
            else
1175
1228
                prcode(fp, ", NULL");
1176
1229
 
1177
 
            if (mname != NULL)
1178
 
                prcode(fp, ", \"%s\"", mname);
 
1230
            if (tdmname != NULL)
 
1231
                prcode(fp, ", \"%s\"", tdmname);
1179
1232
            else
1180
1233
                prcode(fp, ", NULL");
1181
1234
 
1212
1265
            );
1213
1266
    }
1214
1267
 
1215
 
    if (pt->allimports != NULL)
 
1268
    if (pt->module->allimports != NULL)
1216
1269
    {
1217
1270
        prcode(fp,
1218
1271
"\n"
1221
1274
"static sipImportedModuleDef importsTable[] = {\n"
1222
1275
            );
1223
1276
 
1224
 
        for (mld = pt->allimports; mld != NULL; mld = mld->next)
 
1277
        for (mld = pt->module->allimports; mld != NULL; mld = mld->next)
1225
1278
            prcode(fp,
1226
1279
"    {\"%s\", %d, NULL},\n"
1227
1280
                , mld->module->fullname, mld->module->version);
1338
1391
"\n"
1339
1392
"\n"
1340
1393
"/* This defines the Qt support API. */\n"
 
1394
"\n"
1341
1395
"static sipQtAPI qtAPI = {\n"
1342
1396
"    &typesTable[%d],\n"
1343
1397
"    sipQtIsQtSignal,\n"
1355
1409
"    sipQtSignalsBlocked,\n"
1356
1410
"    sipQtGetSender,\n"
1357
1411
"    sipQtForgetSender,\n"
1358
 
"    sipQtSameSignalSlotName\n"
 
1412
"    sipQtSameSignalSlotName,\n"
 
1413
"    sipQtFindConnection\n"
1359
1414
"};\n"
1360
1415
            ,pt->qobjclass);
1361
1416
 
1394
1449
        , mname
1395
1450
        , pt->module->fullname
1396
1451
        , pt->module->version
1397
 
        , pt->allimports != NULL ? "importsTable" : "NULL"
 
1452
        , pt->module->allimports != NULL ? "importsTable" : "NULL"
1398
1453
        , pt->qobjclass >= 0 ? "&qtAPI" : "NULL"
1399
1454
        , pt->module->nrclasses
1400
1455
        , pt->module->nrclasses > 0 ? "typesTable" : "NULL"
1432
1487
"const sipAPIDef *sipAPI_%s;\n"
1433
1488
        ,mname);
1434
1489
 
1435
 
    for (mld = pt->allimports; mld != NULL; mld = mld->next)
 
1490
    for (mld = pt->module->allimports; mld != NULL; mld = mld->next)
1436
1491
        prcode(fp,
1437
1492
"const sipExportedModuleDef *sipModuleAPI_%s_%s;\n"
1438
1493
            ,mname,mld->module->name);
1439
1494
 
 
1495
    if (optQ_OBJECT4(pt))
 
1496
        prcode(fp,
 
1497
"\n"
 
1498
"sip_qt_metaobject_func sip_%s_qt_metaobject;\n"
 
1499
"sip_qt_metacall_func sip_%s_qt_metacall;\n"
 
1500
            , mname
 
1501
            , mname);
 
1502
 
1440
1503
    /* Generate the Python module initialisation function. */
1441
1504
    prcode(fp,
1442
1505
"\n"
1508
1571
 
1509
1572
    noIntro = TRUE;
1510
1573
 
1511
 
    for (mld = pt->allimports; mld != NULL; mld = mld->next)
 
1574
    for (mld = pt->module->allimports; mld != NULL; mld = mld->next)
1512
1575
    {
1513
1576
        if (noIntro)
1514
1577
        {
1556
1619
            , xd->pyname, xd->exceptionnr);
1557
1620
    }
1558
1621
 
 
1622
    /* Generate any Qt metatype registration calls. */
 
1623
    if (optRegisterTypes(pt))
 
1624
        for (cd = pt->classes; cd != NULL; cd = cd->next)
 
1625
        {
 
1626
            if (cd->iff->module != pt->module)
 
1627
                continue;
 
1628
 
 
1629
            generateRegisterMetaType(cd, fp);
 
1630
        }
 
1631
 
1559
1632
    /* Generate the post-initialisation code. */
1560
1633
    generateCppCodeBlock(pt->postinitcode,fp);
1561
1634
 
 
1635
    /*
 
1636
     * This has to be done after the post-initialisation code in case this
 
1637
     * module is exporting the symbol.
 
1638
     */
 
1639
    if (optQ_OBJECT4(pt))
 
1640
        prcode(fp,
 
1641
"\n"
 
1642
"    sip_%s_qt_metaobject = (sip_qt_metaobject_func)sipImportSymbol(\"qtcore_qt_metaobject\");\n"
 
1643
"    sip_%s_qt_metacall = (sip_qt_metacall_func)sipImportSymbol(\"qtcore_qt_metacall\");\n"
 
1644
            , mname
 
1645
            , mname);
 
1646
 
1562
1647
    prcode(fp,
1563
1648
"}\n"
1564
1649
        );
2160
2245
                , vd->pyname, &vd->type, vd->fqcname);
2161
2246
        else
2162
2247
            prcode(fp,
2163
 
"     {%N, %S},\n"
 
2248
"    {%N, %S},\n"
2164
2249
                , vd->pyname, vd->fqcname);
2165
2250
    }
2166
2251
 
2192
2277
        if (vd->ecd != cd || vd->module != pt->module)
2193
2278
            continue;
2194
2279
 
2195
 
        if (!((vtype == sstring_type || vtype == ustring_type || vtype == string_type) && vd->type.nrderefs == 0))
 
2280
        if (!((vtype == sstring_type || vtype == ustring_type || vtype == string_type || vtype == wstring_type) && vd->type.nrderefs == 0))
2196
2281
            continue;
2197
2282
 
2198
2283
        if (needsHandler(vd))
2251
2336
        if (vd->ecd != cd || vd->module != pt->module)
2252
2337
            continue;
2253
2338
 
2254
 
        if (!((vtype == sstring_type || vtype == ustring_type || vtype == string_type) && vd->type.nrderefs != 0))
 
2339
        if (!((vtype == sstring_type || vtype == ustring_type || vtype == string_type || vtype == wstring_type) && vd->type.nrderefs != 0))
2255
2340
            continue;
2256
2341
 
2257
2342
        if (needsHandler(vd))
3191
3276
 */
3192
3277
static void generateVariableHandler(varDef *vd,FILE *fp)
3193
3278
{
3194
 
    char *ptr;
3195
3279
    argType atype = vd->type.atype;
3196
3280
 
3197
3281
    prcode(fp,
3221
3305
 
3222
3306
    if (vd->getcode == NULL || vd->setcode == NULL)
3223
3307
    {
3224
 
        char name[50];
3225
 
        argDef mod;
3226
 
 
3227
3308
        prcode(fp,
3228
 
"   ");
3229
 
 
3230
 
        mod = vd->type;
3231
 
 
3232
 
        if ((atype == string_type || atype == sstring_type || atype == ustring_type) && vd->type.nrderefs == 0)
3233
 
        {
3234
 
            ptr = "Ptr";
3235
 
            mod.nrderefs = 1;
3236
 
        }
3237
 
        else
3238
 
            ptr = "";
3239
 
 
3240
 
        sprintf(name,"sipVal%s",ptr);
3241
 
 
3242
 
        generateNamedValueType(&mod,name,fp);
3243
 
 
3244
 
        prcode(fp,";\n"
 
3309
"    ");
 
3310
 
 
3311
        generateNamedValueType(&vd->type, "sipVal", fp);
 
3312
 
 
3313
        prcode(fp, ";\n"
3245
3314
            );
3246
3315
    }
3247
3316
 
3285
3354
        int pyobj = FALSE;
3286
3355
 
3287
3356
        prcode(fp,
3288
 
"        ");
3289
 
 
3290
 
        if (ptr[0] == '\0')
3291
 
            prcode(fp,"sipVal");
3292
 
        else
3293
 
            generateNamedValueType(&vd->type,"sipVal",fp);
3294
 
 
3295
 
        prcode(fp," = %s",(((atype == class_type || atype == mapped_type) && vd->type.nrderefs == 0) ? "&" : ""));
 
3357
"        sipVal = %s", (((atype == class_type || atype == mapped_type) && vd->type.nrderefs == 0) ? "&" : ""));
3296
3358
 
3297
3359
        generateVarMember(vd, fp);
3298
3360
 
3299
 
        prcode(fp,";\n"
 
3361
        prcode(fp, ";\n"
3300
3362
"\n"
3301
3363
            );
3302
3364
 
3335
3397
 
3336
3398
            break;
3337
3399
 
 
3400
        case wstring_type:
 
3401
            if (vd->type.nrderefs == 0)
 
3402
                prcode(fp,
 
3403
"        sipPy = PyUnicode_FromWideChar(&sipVal,1);\n"
 
3404
                    );
 
3405
            else
 
3406
                prcode(fp,
 
3407
"        sipPy = PyUnicode_FromWideChar(sipVal,(SIP_SSIZE_T)wcslen(sipVal));\n"
 
3408
                    );
 
3409
 
 
3410
            break;
 
3411
 
3338
3412
        case float_type:
3339
3413
        case cfloat_type:
3340
3414
            prcode(fp,
3490
3564
        }
3491
3565
        else
3492
3566
        {
3493
 
            if (atype == sstring_type || atype == ustring_type || atype == string_type)
 
3567
            if ((atype == sstring_type || atype == ustring_type || atype == string_type || atype == wstring_type) && vd->type.nrderefs != 0)
3494
3568
            {
3495
 
                if (vd->type.nrderefs == 0)
3496
 
                    deref = "*";
3497
 
 
3498
3569
                prcode(fp,
3499
3570
"\n"
3500
 
"    if (sipVal%s == NULL)\n"
3501
 
                    ,ptr);
 
3571
"    if (sipVal == NULL)\n"
 
3572
                    );
3502
3573
            }
3503
3574
            else
3504
3575
                prcode(fp,
3536
3607
 
3537
3608
        generateVarMember(vd, fp);
3538
3609
 
3539
 
        prcode(fp," = %ssipVal%s;\n"
3540
 
            ,deref,ptr);
 
3610
        prcode(fp, " = %ssipVal;\n"
 
3611
            , deref);
 
3612
 
 
3613
        /* Note that wchar_t * leaks here. */
3541
3614
 
3542
3615
        if (might_be_temp)
3543
3616
            prcode(fp,
3601
3674
static int generateObjToCppConversion(argDef *ad,FILE *fp)
3602
3675
{
3603
3676
    int might_be_temp = FALSE;
3604
 
    char *fmt = NULL;
 
3677
    char *rhs = NULL;
 
3678
 
 
3679
    prcode(fp,
 
3680
"    sipVal = ");
3605
3681
 
3606
3682
    switch (ad->atype)
3607
3683
    {
3611
3687
 
3612
3688
            if (generating_c)
3613
3689
            {
3614
 
                prcode(fp,
3615
 
"    sipVal = (%b *)", ad);
 
3690
                prcode(fp, "(%b *)", ad);
3616
3691
                tail = "";
3617
3692
            }
3618
3693
            else
3619
3694
            {
3620
 
                prcode(fp,
3621
 
"    sipVal = reinterpret_cast<%b *>(", ad);
 
3695
                prcode(fp, "reinterpret_cast<%b *>(", ad);
3622
3696
                tail = ")";
3623
3697
            }
3624
3698
 
3642
3716
 
3643
3717
            if (generating_c)
3644
3718
            {
3645
 
                prcode(fp,
3646
 
"    sipVal = (%b *)", ad);
 
3719
                prcode(fp, "(%b *)", ad);
3647
3720
                tail = "";
3648
3721
            }
3649
3722
            else
3650
3723
            {
3651
 
                prcode(fp,
3652
 
"    sipVal = reinterpret_cast<%b *>(", ad);
 
3724
                prcode(fp, "reinterpret_cast<%b *>(", ad);
3653
3725
                tail = ")";
3654
3726
            }
3655
3727
 
3667
3739
        break;
3668
3740
 
3669
3741
    case enum_type:
3670
 
        prcode(fp,
3671
 
"    sipVal = (%E)PyInt_AsLong(sipPy);\n"
3672
 
            ,ad->u.ed);
 
3742
        prcode(fp, "(%E)PyInt_AsLong(sipPy);\n"
 
3743
            , ad->u.ed);
3673
3744
        break;
3674
3745
 
3675
3746
    case sstring_type:
3676
3747
        if (ad->nrderefs == 0)
3677
 
            fmt = "\tsipValPtr = (signed char *)PyString_AsString(sipPy);\n";
 
3748
            rhs = "(signed char)sipString_AsChar(sipPy)";
3678
3749
        else
3679
 
            fmt = "\tsipVal = (signed char *)PyString_AsString(sipPy);\n";
 
3750
            rhs = "(signed char *)PyString_AsString(sipPy)";
3680
3751
        break;
3681
3752
 
3682
3753
    case ustring_type:
3683
3754
        if (ad->nrderefs == 0)
3684
 
            fmt = "\tsipValPtr = (unsigned char *)PyString_AsString(sipPy);\n";
 
3755
            rhs = "(unsigned char)sipString_AsChar(sipPy)";
3685
3756
        else
3686
 
            fmt = "\tsipVal = (unsigned char *)PyString_AsString(sipPy);\n";
 
3757
            rhs = "(unsigned char *)PyString_AsString(sipPy)";
3687
3758
        break;
3688
3759
 
3689
3760
    case string_type:
3690
3761
        if (ad->nrderefs == 0)
3691
 
            fmt = "\tsipValPtr = PyString_AsString(sipPy);\n";
3692
 
        else
3693
 
            fmt = "\tsipVal = PyString_AsString(sipPy);\n";
 
3762
            rhs = "sipString_AsChar(sipPy)";
 
3763
        else
 
3764
            rhs = "PyString_AsString(sipPy)";
 
3765
        break;
 
3766
 
 
3767
    case wstring_type:
 
3768
        if (ad->nrderefs == 0)
 
3769
            rhs = "sipUnicode_AsWChar(sipPy)";
 
3770
        else
 
3771
            rhs = "sipUnicode_AsWString(sipPy)";
3694
3772
        break;
3695
3773
 
3696
3774
    case float_type:
3697
3775
    case cfloat_type:
3698
 
        fmt = "\tsipVal = (float)PyFloat_AsDouble(sipPy);\n";
 
3776
        rhs = "(float)PyFloat_AsDouble(sipPy)";
3699
3777
        break;
3700
3778
 
3701
3779
    case double_type:
3702
3780
    case cdouble_type:
3703
 
        fmt = "\tsipVal = PyFloat_AsDouble(sipPy);\n";
 
3781
        rhs = "PyFloat_AsDouble(sipPy)";
3704
3782
        break;
3705
3783
 
3706
3784
    case bool_type:
3707
3785
    case cbool_type:
3708
 
        fmt = "\tsipVal = (bool)PyInt_AsLong(sipPy);\n";
 
3786
        rhs = "(bool)PyInt_AsLong(sipPy)";
3709
3787
        break;
3710
3788
 
3711
3789
    case ushort_type:
3712
 
        fmt = "\tsipVal = (unsigned short)sipLong_AsUnsignedLong(sipPy);\n";
 
3790
        rhs = "(unsigned short)sipLong_AsUnsignedLong(sipPy)";
3713
3791
        break;
3714
3792
 
3715
3793
    case short_type:
3716
 
        fmt = "\tsipVal = (short)PyInt_AsLong(sipPy);\n";
 
3794
        rhs = "(short)PyInt_AsLong(sipPy)";
3717
3795
        break;
3718
3796
 
3719
3797
    case uint_type:
3720
 
        fmt = "\tsipVal = (unsigned)sipLong_AsUnsignedLong(sipPy);\n";
 
3798
        rhs = "(unsigned)sipLong_AsUnsignedLong(sipPy)";
3721
3799
        break;
3722
3800
 
3723
3801
    case int_type:
3724
3802
    case cint_type:
3725
 
        fmt = "\tsipVal = (int)PyInt_AsLong(sipPy);\n";
 
3803
        rhs = "(int)PyInt_AsLong(sipPy)";
3726
3804
        break;
3727
3805
 
3728
3806
    case ulong_type:
3729
 
        fmt = "\tsipVal = sipLong_AsUnsignedLong(sipPy);\n";
 
3807
        rhs = "sipLong_AsUnsignedLong(sipPy)";
3730
3808
        break;
3731
3809
 
3732
3810
    case long_type:
3733
 
        fmt = "\tsipVal = PyLong_AsLong(sipPy);\n";
 
3811
        rhs = "PyLong_AsLong(sipPy)";
3734
3812
        break;
3735
3813
 
3736
3814
    case ulonglong_type:
3737
 
        fmt = "\tsipVal = PyLong_AsUnsignedLongLong(sipPy);\n";
 
3815
        rhs = "PyLong_AsUnsignedLongLong(sipPy)";
3738
3816
        break;
3739
3817
 
3740
3818
    case longlong_type:
3741
 
        fmt = "\tsipVal = PyLong_AsLongLong(sipPy);\n";
 
3819
        rhs = "PyLong_AsLongLong(sipPy)";
3742
3820
        break;
3743
3821
 
3744
3822
    case struct_type:
3745
 
        prcode(fp,
3746
 
"    sipVal = (struct %S *)sipConvertToVoidPtr(sipPy);\n"
3747
 
            ,ad->u.sname);
 
3823
        prcode(fp, "(struct %S *)sipConvertToVoidPtr(sipPy);\n"
 
3824
            , ad->u.sname);
3748
3825
        break;
3749
3826
 
3750
3827
    case void_type:
3751
 
        fmt = "\tsipVal = sipConvertToVoidPtr(sipPy);\n";
 
3828
        rhs = "sipConvertToVoidPtr(sipPy)";
3752
3829
        break;
3753
3830
 
3754
3831
    case pyobject_type:
3758
3835
    case pycallable_type:
3759
3836
    case pyslice_type:
3760
3837
    case pytype_type:
3761
 
        fmt = "\tsipVal = sipPy;\n";
 
3838
        rhs = "sipPy";
3762
3839
        break;
3763
3840
    }
3764
3841
 
3765
 
    if (fmt != NULL)
3766
 
        prcode(fp,"%s",fmt);
 
3842
    if (rhs != NULL)
 
3843
        prcode(fp, "%s;\n"
 
3844
            , rhs);
3767
3845
 
3768
3846
    return might_be_temp;
3769
3847
}
4215
4293
         */
4216
4294
        if (canCreate(cd) || isPublicDtor(cd))
4217
4295
        {
4218
 
            if (release_gil || isReleaseGILDtor(cd))
 
4296
            int rgil = ((release_gil || isReleaseGILDtor(cd)) && !isHoldGILDtor(cd));
 
4297
 
 
4298
            if (rgil)
4219
4299
                prcode(fp,
4220
4300
"    Py_BEGIN_ALLOW_THREADS\n"
4221
4301
"\n"
4239
4319
"    delete reinterpret_cast<%U *>(ptr);\n"
4240
4320
                    , cd);
4241
4321
 
4242
 
            if (release_gil || isReleaseGILDtor(cd))
 
4322
            if (rgil)
4243
4323
                prcode(fp,
4244
4324
"\n"
4245
4325
"    Py_END_ALLOW_THREADS\n"
4329
4409
 
4330
4410
        if (!generating_c)
4331
4411
            prcode(fp,
4332
 
"extern \"C\" {static int getreadbuffer_%C(PyObject *, void *, int, void **);}\n"
 
4412
"extern \"C\" {static SIP_SSIZE_T getreadbuffer_%C(PyObject *, void *, SIP_SSIZE_T, void **);}\n"
4333
4413
                , classFQCName(cd));
4334
4414
 
4335
4415
        prcode(fp,
4336
 
"static int getreadbuffer_%C(PyObject *%s, void *sipCppV, int %s, void **%s)\n"
 
4416
"static SIP_SSIZE_T getreadbuffer_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T %s, void **%s)\n"
4337
4417
"{\n"
4338
 
"   ", classFQCName(cd)
 
4418
"    ", classFQCName(cd)
4339
4419
     , argName("sipSelf", cd->readbufcode)
4340
4420
     , argName("sipSegment", cd->readbufcode)
4341
4421
     , argName("sipPtrPtr", cd->readbufcode));
4343
4423
        generateClassFromVoid(cd, "sipCpp", "sipCppV", fp);
4344
4424
 
4345
4425
        prcode(fp, ";\n"
4346
 
"    int sipRes;\n"
 
4426
"    SIP_SSIZE_T sipRes;\n"
4347
4427
"\n"
4348
4428
            );
4349
4429
 
4365
4445
 
4366
4446
        if (!generating_c)
4367
4447
            prcode(fp,
4368
 
"extern \"C\" {static int getwritebuffer_%C(PyObject *, void *, int, void **);}\n"
 
4448
"extern \"C\" {static SIP_SSIZE_T getwritebuffer_%C(PyObject *, void *, SIP_SSIZE_T, void **);}\n"
4369
4449
                , classFQCName(cd));
4370
4450
 
4371
4451
        prcode(fp,
4372
 
"static int getwritebuffer_%C(PyObject *%s, void *sipCppV, int %s, void **%s)\n"
 
4452
"static SIP_SSIZE_T getwritebuffer_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T %s, void **%s)\n"
4373
4453
"{\n"
4374
 
"   ", classFQCName(cd)
 
4454
"    ", classFQCName(cd)
4375
4455
     , argName("sipSelf", cd->writebufcode)
4376
4456
     , argName("sipSegment", cd->writebufcode)
4377
4457
     , argName("sipPtrPtr", cd->writebufcode));
4379
4459
        generateClassFromVoid(cd, "sipCpp", "sipCppV", fp);
4380
4460
 
4381
4461
        prcode(fp, ";\n"
4382
 
"    int sipRes;\n"
 
4462
"    SIP_SSIZE_T sipRes;\n"
4383
4463
"\n"
4384
4464
            );
4385
4465
 
4401
4481
 
4402
4482
        if (!generating_c)
4403
4483
            prcode(fp,
4404
 
"extern \"C\" {static int getsegcount_%C(PyObject *, void *, int *);}\n"
 
4484
"extern \"C\" {static SIP_SSIZE_T getsegcount_%C(PyObject *, void *, SIP_SSIZE_T *);}\n"
4405
4485
                , classFQCName(cd));
4406
4486
 
4407
4487
        prcode(fp,
4408
 
"static int getsegcount_%C(PyObject *%s, void *sipCppV, int *%s)\n"
 
4488
"static SIP_SSIZE_T getsegcount_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T *%s)\n"
4409
4489
"{\n"
4410
 
"   ", classFQCName(cd)
 
4490
"    ", classFQCName(cd)
4411
4491
     , argName("sipSelf", cd->segcountcode)
4412
4492
     , argName("sipLenPtr", cd->segcountcode));
4413
4493
 
4414
4494
        generateClassFromVoid(cd, "sipCpp", "sipCppV", fp);
4415
4495
 
4416
4496
        prcode(fp, ";\n"
4417
 
"    int sipRes;\n"
 
4497
"    SIP_SSIZE_T sipRes;\n"
4418
4498
"\n"
4419
4499
            );
4420
4500
 
4436
4516
 
4437
4517
        if (!generating_c)
4438
4518
            prcode(fp,
4439
 
"extern \"C\" {static int getcharbuffer_%C(PyObject *, void *, int, void **);}\n"
 
4519
"extern \"C\" {static SIP_SSIZE_T getcharbuffer_%C(PyObject *, void *, SIP_SSIZE_T, void **);}\n"
4440
4520
                , classFQCName(cd));
4441
4521
 
4442
4522
        prcode(fp,
4443
 
"static int getcharbuffer_%C(PyObject *%s, void *sipCppV, int %s, void **%s)\n"
 
4523
"static SIP_SSIZE_T getcharbuffer_%C(PyObject *%s, void *sipCppV, SIP_SSIZE_T %s, void **%s)\n"
4444
4524
"{\n"
4445
 
"   ", classFQCName(cd)
 
4525
"    ", classFQCName(cd)
4446
4526
     , argName("sipSelf", cd->charbufcode)
4447
4527
     , argName("sipSegment", cd->charbufcode)
4448
4528
     , argName("sipPtrPtr", cd->charbufcode));
4450
4530
        generateClassFromVoid(cd, "sipCpp", "sipCppV", fp);
4451
4531
 
4452
4532
        prcode(fp, ";\n"
4453
 
"    int sipRes;\n"
 
4533
"    SIP_SSIZE_T sipRes;\n"
4454
4534
"\n"
4455
4535
            );
4456
4536
 
4645
4725
            );
4646
4726
    }
4647
4727
 
 
4728
    /* The metacall method if required. */
 
4729
    if (isQObjectSubClass(cd) && optQ_OBJECT4(pt))
 
4730
    {
 
4731
        prcode(fp,
 
4732
"\n"
 
4733
"const QMetaObject *sip%C::metaObject() const\n"
 
4734
"{\n"
 
4735
"    return sip_%s_qt_metaobject(sipPySelf,sipClass_%C,%S::metaObject());\n"
 
4736
"}\n"
 
4737
"\n"
 
4738
"int sip%C::qt_metacall(QMetaObject::Call _c,int _id,void **_a)\n"
 
4739
"{\n"
 
4740
"    sip%C::metaObject();\n"
 
4741
"\n"
 
4742
"    _id = %S::qt_metacall(_c,_id,_a);\n"
 
4743
"\n"
 
4744
"    if (_id >= 0)\n"
 
4745
"    {\n"
 
4746
"        SIP_BLOCK_THREADS\n"
 
4747
"        _id = sip_%s_qt_metacall(sipPySelf,sipClass_%C,_c,_id,_a);\n"
 
4748
"        SIP_UNBLOCK_THREADS\n"
 
4749
"    }\n"
 
4750
"\n"
 
4751
"    return _id;\n"
 
4752
"}\n"
 
4753
            , classFQCName(cd)
 
4754
            , pt->module->name, classFQCName(cd), classFQCName(cd)
 
4755
            , classFQCName(cd)
 
4756
            , classFQCName(cd)
 
4757
            , classFQCName(cd)
 
4758
            , pt->module->name, classFQCName(cd));
 
4759
    }
 
4760
 
4648
4761
    /* Generate the virtual catchers. */
4649
4762
 
4650
4763
    virtNr = 0;
4783
4896
/*
4784
4897
 * Generate the catcher for a virtual function.
4785
4898
 */
4786
 
 
4787
 
static void generateVirtualCatcher(sipSpec *pt,classDef *cd,int virtNr,
4788
 
                   virtOverDef *vod,FILE *fp)
 
4899
static void generateVirtualCatcher(sipSpec *pt, classDef *cd, int virtNr,
 
4900
        virtOverDef *vod, FILE *fp)
4789
4901
{
4790
4902
    overDef *od = &vod->o;
4791
4903
    virtHandlerDef *vhd = od->virthandler;
5116
5228
/*
5117
5229
 * Generate the call to a default ctor.
5118
5230
 */
5119
 
static void generateCallDefaultCtor(ctorDef *ct,FILE *fp)
 
5231
static void generateCallDefaultCtor(ctorDef *ct, FILE *fp)
5120
5232
{
5121
5233
    int a;
5122
5234
 
5123
 
    prcode(fp,"(");
 
5235
    prcode(fp, "(");
5124
5236
 
5125
5237
    for (a = 0; a < ct->cppsig->nrArgs; ++a)
5126
5238
    {
5130
5242
            break;
5131
5243
 
5132
5244
        if (a > 0)
5133
 
            prcode(fp,",");
 
5245
            prcode(fp, ",");
5134
5246
 
5135
5247
        /*
5136
5248
         * Do what we can to provide type information to the compiler.
5137
5249
         */
5138
5250
        if (ad->atype == class_type && ad->nrderefs > 0 && !isReference(ad))
5139
 
            prcode(fp,"static_cast<%B>(0)",ad);
 
5251
            prcode(fp, "static_cast<%B>(0)", ad);
5140
5252
        else if (ad->atype == enum_type)
5141
 
            prcode(fp,"static_cast<%E>(0)",ad->u.ed);
 
5253
            prcode(fp, "static_cast<%E>(0)", ad->u.ed);
5142
5254
        else if (ad->atype == float_type || ad->atype == cfloat_type)
5143
 
            prcode(fp,"0.0F");
 
5255
            prcode(fp, "0.0F");
5144
5256
        else if (ad->atype == double_type || ad->atype == cdouble_type)
5145
 
            prcode(fp,"0.0");
 
5257
            prcode(fp, "0.0");
5146
5258
        else if (ad->atype == uint_type)
5147
 
            prcode(fp,"0U");
 
5259
            prcode(fp, "0U");
5148
5260
        else if (ad->atype == long_type || ad->atype == longlong_type)
5149
 
            prcode(fp,"0L");
 
5261
            prcode(fp, "0L");
5150
5262
        else if (ad->atype == ulong_type || ad->atype == ulonglong_type)
5151
 
            prcode(fp,"0UL");
 
5263
            prcode(fp, "0UL");
 
5264
        else if ((ad->atype == ustring_type || ad->atype == sstring_type || ad->atype == string_type) && ad->nrderefs == 0)
 
5265
            prcode(fp, "'\\0'");
 
5266
        else if (ad->atype == wstring_type && ad->nrderefs == 0)
 
5267
            prcode(fp, "L'\\0'");
5152
5268
        else
5153
 
            prcode(fp,"0");
 
5269
            prcode(fp, "0");
5154
5270
    }
5155
5271
 
5156
 
    prcode(fp,")");
 
5272
    prcode(fp, ")");
5157
5273
}
5158
5274
 
5159
5275
 
5174
5290
 
5175
5291
    for (od = vl->cd->overs; od != NULL; od = od->next)
5176
5292
    {
 
5293
        int rgil = ((release_gil || isReleaseGIL(od)) && !isHoldGIL(od));
 
5294
 
5177
5295
        if (od->common != vl->m || !isSignal(od))
5178
5296
            continue;
5179
5297
 
5192
5310
"        {\n"
5193
5311
            );
5194
5312
 
5195
 
        if (release_gil || isReleaseGIL(od))
 
5313
        if (rgil)
5196
5314
            prcode(fp,
5197
5315
"            Py_BEGIN_ALLOW_THREADS\n"
5198
5316
                );
5206
5324
        prcode(fp,");\n"
5207
5325
            );
5208
5326
 
5209
 
        if (release_gil || isReleaseGIL(od))
 
5327
        if (rgil)
5210
5328
            prcode(fp,
5211
5329
"            Py_END_ALLOW_THREADS\n"
5212
5330
                );
5448
5566
    else
5449
5567
    {
5450
5568
        /*
5451
 
         * If we are returning a reference to an instance then we take
5452
 
         * care to handle Python errors but still return a valid C++
5453
 
         * instance.  If we are returning an instance then we take care
5454
 
         * to make a local copy of the instance returned from Python
5455
 
         * before the Python object is garbage collected and the C++
5456
 
         * instance (possibly) destroyed.
 
5569
         * If we are returning a reference to an instance then we take care to
 
5570
         * handle Python errors but still return a valid C++ instance.  If we
 
5571
         * are returning an instance then we take care to make a local copy of
 
5572
         * the instance returned from Python before the Python object is
 
5573
         * garbage collected and the C++ instance (possibly) destroyed.
5457
5574
         */
5458
5575
        if ((res->atype == class_type || res->atype == mapped_type) && res->nrderefs == 0)
5459
5576
            if (isReference(res))
5490
5607
    {
5491
5608
        prcode(fp, "    ");
5492
5609
 
 
5610
        /*
 
5611
         * wchar_t * return values are always on the heap.  To reduce memory
 
5612
         * leaks we keep the last result around until we have a new one.  This
 
5613
         * means that ownership of the return value stays with the function
 
5614
         * returning it - which is consistent with how other types work, even
 
5615
         * thought it may not be what's required in all cases.
 
5616
         */
 
5617
        if (res->atype == wstring_type && res->nrderefs == 1)
 
5618
            prcode(fp, "static ");
 
5619
 
5493
5620
        generateBaseType(&res_noconstref,fp);
5494
5621
 
5495
5622
        prcode(fp," %ssipRes",(isref ? "*" : ""));
5513
5640
 
5514
5641
        prcode(fp,";\n"
5515
5642
            );
 
5643
 
 
5644
        if (res->atype == wstring_type && res->nrderefs == 1)
 
5645
            prcode(fp,
 
5646
"\n"
 
5647
"    if (sipRes)\n"
 
5648
"    {\n"
 
5649
"        // Return any previous result to the heap.\n"
 
5650
"        sipFree(%s);\n"
 
5651
"        sipRes = 0;\n"
 
5652
"    }\n"
 
5653
"\n"
 
5654
                , (isConstArg(res) ? "const_cast<wchar_t *>(sipRes)" : "sipRes"));
5516
5655
    }
5517
5656
 
5518
5657
    if (vhd->virtcode != NULL)
5818
5957
    case string_type:
5819
5958
        return ((ad->nrderefs == 0) ? "c" : "s");
5820
5959
 
 
5960
    case wstring_type:
 
5961
        return ((ad->nrderefs == 0) ? "w" : "x");
 
5962
 
5821
5963
    case enum_type:
5822
5964
        return ((ad->u.ed->fqcname != NULL) ? "E" : "e");
5823
5965
 
5905
6047
 
5906
6048
            break;
5907
6049
 
 
6050
        case wstring_type:
 
6051
            if (ad->nrderefs == 0 || (ad->nrderefs == 1 && isOutArg(ad)))
 
6052
                fmt = "w";
 
6053
            else if (isArray(ad))
 
6054
                fmt = "A";
 
6055
            else
 
6056
                fmt = "x";
 
6057
 
 
6058
            break;
 
6059
 
5908
6060
        case bool_type:
5909
6061
        case cbool_type:
5910
6062
            fmt = "b";
6049
6201
        case sstring_type:
6050
6202
        case ustring_type:
6051
6203
        case string_type:
 
6204
        case wstring_type:
6052
6205
            if (!(ad->nrderefs == 0 || (ad->nrderefs == 1 && isOutArg(ad))))
6053
6206
                --derefs;
6054
6207
 
6336
6489
    if (genused)
6337
6490
        generateUsedIncludes(cd->iff->used, TRUE, fp);
6338
6491
 
6339
 
    if (isOpaque(cd))
6340
 
        prcode(fp,
6341
 
"\n"
6342
 
"class %S;\n"
6343
 
            ,classFQCName(cd));
6344
 
 
6345
6492
    if (cd->iff->type != namespace_iface)
6346
6493
    {
6347
6494
        prcode(fp,
6472
6619
"    %s~sip%C()%X;\n"
6473
6620
            ,(cd->vmembers != NULL ? "virtual " : ""),classFQCName(cd),cd->dtorexceptions);
6474
6621
 
 
6622
    /* The metacall methods if required. */
 
6623
    if (isQObjectSubClass(cd) && optQ_OBJECT4(pt))
 
6624
        prcode(fp,
 
6625
"\n"
 
6626
"    const QMetaObject *metaObject() const;\n"
 
6627
"    int qt_metacall(QMetaObject::Call,int,void **);\n"
 
6628
            );
 
6629
 
6475
6630
    /* The exposure of protected enums. */
6476
6631
 
6477
6632
    generateProtectedEnums(pt,cd,fp);
6702
6857
            case sstring_type:
6703
6858
            case ustring_type:
6704
6859
            case string_type:
 
6860
            case wstring_type:
6705
6861
                if (ad->nrderefs > (isOutArg(ad) ? 0 : 1))
6706
6862
                    ind = "&";
6707
6863
 
6792
6948
        prcode(fp,"unsigned char");
6793
6949
        break;
6794
6950
 
 
6951
    case wstring_type:
 
6952
        prcode(fp,"wchar_t");
 
6953
        break;
 
6954
 
6795
6955
    case signal_type:
6796
6956
    case slot_type:
6797
6957
    case anyslot_type:
6982
7142
    case sstring_type:
6983
7143
    case ustring_type:
6984
7144
    case string_type:
 
7145
    case wstring_type:
6985
7146
        if (!isReference(ad))
6986
7147
            if (ad->nrderefs == 2)
6987
7148
                ad->nrderefs = 1;
7103
7264
static void generateTypeDefinition(sipSpec *pt, classDef *cd, FILE *fp)
7104
7265
{
7105
7266
    char *mname = pt->module->name;
 
7267
    const char *sep;
7106
7268
    int is_slots, nr_methods, nr_enums;
7107
7269
    int is_inst_class, is_inst_voidp, is_inst_char, is_inst_string;
7108
7270
    int is_inst_int, is_inst_long, is_inst_ulong, is_inst_longlong;
7189
7351
"    0,\n"
7190
7352
"    ", mname, classFQCName(cd));
7191
7353
 
7192
 
    if (isAbstractClass(cd) && cd->subbase != NULL)
7193
 
           prcode(fp, "SIP_TYPE_ABSTRACT|SIP_TYPE_SCC,\n");
7194
 
    else if (isAbstractClass(cd))
7195
 
           prcode(fp, "SIP_TYPE_ABSTRACT,\n");
7196
 
    else if (cd->subbase != NULL)
7197
 
           prcode(fp, "SIP_TYPE_SCC,\n");
7198
 
    else
7199
 
           prcode(fp, "0,\n");
 
7354
    sep = "";
 
7355
 
 
7356
    if (cd->userflags)
 
7357
    {
 
7358
        prcode(fp, "%s%x", sep, ((cd->userflags << TYPE_FLAGS_SHIFT) & TYPE_FLAGS_MASK));
 
7359
        sep = "|";
 
7360
    }
 
7361
 
 
7362
    if (isAbstractClass(cd))
 
7363
    {
 
7364
        prcode(fp, "%sSIP_TYPE_ABSTRACT", sep);
 
7365
        sep = "|";
 
7366
    }
 
7367
 
 
7368
    if (cd->subbase != NULL)
 
7369
    {
 
7370
        prcode(fp, "%sSIP_TYPE_SCC", sep);
 
7371
        sep = "|";
 
7372
    }
 
7373
 
 
7374
    if (*sep == '\0')
 
7375
        prcode(fp, "0");
 
7376
 
 
7377
    prcode(fp, ",\n");
7200
7378
 
7201
7379
    if (cd->real != NULL)
7202
7380
        prcode(fp,
7667
7845
 
7668
7846
 
7669
7847
/*
 
7848
 * Generate the code to register a class as a Qt metatype.
 
7849
 */
 
7850
static void generateRegisterMetaType(classDef *cd, FILE *fp)
 
7851
{
 
7852
    int pub_def_ctor, pub_copy_ctor;
 
7853
    ctorDef *ct;
 
7854
 
 
7855
    /*
 
7856
     * We register types with Qt if the class is not abstract, has a public
 
7857
     * default ctor, a public copy ctor, a public dtor and isn't one of the
 
7858
     * internally supported types.
 
7859
     */
 
7860
    if (isAbstractClass(cd))
 
7861
        return;
 
7862
 
 
7863
    if (!isPublicDtor(cd))
 
7864
        return;
 
7865
 
 
7866
    if (classFQCName(cd)->next == NULL)
 
7867
    {
 
7868
        if (strcmp(classBaseName(cd), "QChar") == 0)
 
7869
            return;
 
7870
 
 
7871
        if (strcmp(classBaseName(cd), "QString") == 0)
 
7872
            return;
 
7873
 
 
7874
        if (strcmp(classBaseName(cd), "QByteArray") == 0)
 
7875
            return;
 
7876
    }
 
7877
 
 
7878
    pub_def_ctor = pub_copy_ctor = FALSE;
 
7879
 
 
7880
    for (ct = cd->ctors; ct != NULL; ct = ct->next)
 
7881
    {
 
7882
        if (ct->cppsig == NULL || !isPublicCtor(ct))
 
7883
            continue;
 
7884
 
 
7885
        if (ct->cppsig->nrArgs == 0)
 
7886
            pub_def_ctor = TRUE;
 
7887
        else if (ct->cppsig->nrArgs == 1)
 
7888
        {
 
7889
            argDef *ad = &ct->cppsig->args[0];
 
7890
 
 
7891
            if (ad->atype == class_type && ad->u.cd == cd && isReference(ad) &&
 
7892
                isConstArg(ad) && ad->nrderefs == 0 && ad->defval == NULL)
 
7893
                pub_copy_ctor = TRUE;
 
7894
        }
 
7895
    }
 
7896
 
 
7897
    if (pub_def_ctor && pub_copy_ctor)
 
7898
        prcode(fp,
 
7899
"    qRegisterMetaType<%S>(\"%S\");\n"
 
7900
            , classFQCName(cd), classFQCName(cd));
 
7901
}
 
7902
 
 
7903
 
 
7904
/*
7670
7905
 * Generate the initialisation function or cast operators for the type.
7671
7906
 */
7672
7907
static void generateTypeInit(sipSpec *pt, classDef *cd, FILE *fp)
7673
7908
{
7674
7909
    ctorDef *ct;
7675
 
    int need_self, need_owner, reg_type, pub_def_ctor, pub_copy_ctor;
7676
 
 
7677
 
    /*
7678
 
     * We register types with Qt if enabled, if the class is not abstract, has
7679
 
     * a public default ctor, a public copy ctor, a public dtor and isn't one
7680
 
     * of the internally supported types.
7681
 
     */
7682
 
    reg_type = (optRegisterTypes(pt) && !isAbstractClass(cd) &&
7683
 
            isPublicDtor(cd) &&
7684
 
            (classFQCName(cd)->next != NULL ||
7685
 
             (strcmp(classBaseName(cd), "QChar") != 0 &&
7686
 
              strcmp(classBaseName(cd), "QString") != 0 &&
7687
 
              strcmp(classBaseName(cd), "QByteArray") != 0)));
 
7910
    int need_self, need_owner;
7688
7911
 
7689
7912
    /*
7690
7913
     * See if we need to name the self and owner arguments so that we can
7692
7915
     */
7693
7916
    need_self = (generating_c || hasShadow(cd));
7694
7917
    need_owner = generating_c;
7695
 
    pub_def_ctor = pub_copy_ctor = FALSE;
7696
7918
 
7697
7919
    for (ct = cd->ctors; ct != NULL; ct = ct->next)
7698
7920
    {
7707
7929
                need_owner = TRUE;
7708
7930
                break;
7709
7931
            }
7710
 
 
7711
 
        if (reg_type && ct->cppsig != NULL && isPublicCtor(ct))
7712
 
            if (ct->cppsig->nrArgs == 0)
7713
 
                pub_def_ctor = TRUE;
7714
 
            else if (ct->cppsig->nrArgs == 1)
7715
 
            {
7716
 
                argDef *ad = &ct->cppsig->args[0];
7717
 
 
7718
 
                if (ad->atype == class_type &&
7719
 
                    ad->u.cd == cd &&
7720
 
                    isReference(ad) &&
7721
 
                    isConstArg(ad) &&
7722
 
                    ad->nrderefs == 0 &&
7723
 
                    ad->defval == NULL)
7724
 
                    pub_copy_ctor = TRUE;
7725
 
            }
7726
7932
    }
7727
7933
 
7728
7934
    prcode(fp,
7740
7946
"{\n"
7741
7947
        ,classFQCName(cd),(need_self ? "sipSelf" : ""),(need_owner ? "sipOwner" : ""));
7742
7948
 
7743
 
    if (reg_type && pub_def_ctor && pub_copy_ctor)
7744
 
        prcode(fp,
7745
 
"    static bool sipRegistered = false;\n"
7746
 
"\n"
7747
 
"    if (!sipRegistered)\n"
7748
 
"    {\n"
7749
 
"        qRegisterMetaType<%S>(\"%S\");\n"
7750
 
"        sipRegistered = true;\n"
7751
 
"    }\n"
7752
 
"\n"
7753
 
            , classFQCName(cd), classFQCName(cd));
7754
 
 
7755
7949
    if (hasShadow(cd))
7756
7950
        prcode(fp,
7757
7951
"    sip%C *sipCpp = 0;\n"
8004
8198
            ,classFQCName(cd));
8005
8199
    else
8006
8200
    {
8007
 
        if (release_gil || isReleaseGILCtor(ct))
 
8201
        int rgil = ((release_gil || isReleaseGILCtor(ct)) && !isHoldGILCtor(ct));
 
8202
 
 
8203
        if (rgil)
8008
8204
            prcode(fp,
8009
8205
"            Py_BEGIN_ALLOW_THREADS\n"
8010
8206
                );
8022
8218
        {
8023
8219
            classDef *ocd;
8024
8220
 
8025
 
            /*
8026
 
             * We have to fiddle the type to generate the correct
8027
 
             * code.
8028
 
             */
 
8221
            /* We have to fiddle the type to generate the correct code. */
8029
8222
            ocd = ct->pysig.args[0].u.cd;
8030
8223
            ct->pysig.args[0].u.cd = cd;
8031
8224
            prcode(fp, "a0->operator %B()", &ct->pysig.args[0]);
8039
8232
 
8040
8233
        generateCatch(ct->exceptions, &ct->pysig, fp);
8041
8234
 
8042
 
        if (release_gil || isReleaseGILCtor(ct))
 
8235
        if (rgil)
8043
8236
            prcode(fp,
8044
8237
"            Py_END_ALLOW_THREADS\n"
8045
8238
                );
8540
8733
 
8541
8734
        break;
8542
8735
 
 
8736
    case wstring_type:
 
8737
        if (ad->nrderefs == 0)
 
8738
            prcode(fp,
 
8739
"            %s PyUnicode_FromWideChar(&%s,1);\n"
 
8740
                , prefix, vname);
 
8741
        else
 
8742
            prcode(fp,
 
8743
"            if (%s == NULL)\n"
 
8744
"            {\n"
 
8745
"                Py_INCREF(Py_None);\n"
 
8746
"                return Py_None;\n"
 
8747
"            }\n"
 
8748
"\n"
 
8749
"            %s PyUnicode_FromWideChar(%s,(SIP_SSIZE_T)wcslen(%s));\n"
 
8750
            , vname
 
8751
            , prefix, vname, vname);
 
8752
 
 
8753
        break;
 
8754
 
8543
8755
    case enum_type:
8544
8756
        if (ad->u.ed->fqcname != NULL)
8545
8757
        {
8676
8888
    case string_type:
8677
8889
        return (ad->nrderefs > (isOutArg(ad) ? 1 : 0)) ? 's' : 'c';
8678
8890
 
 
8891
    case wstring_type:
 
8892
        return (ad->nrderefs > (isOutArg(ad) ? 1 : 0)) ? 'x' : 'w';
 
8893
 
8679
8894
    case enum_type:
8680
8895
        return (ad->u.ed->fqcname != NULL) ? 'E' : 'e';
8681
8896
 
8881
9096
        generateCppCodeBlock(od->methodcode,fp);
8882
9097
    else
8883
9098
    {
 
9099
        int rgil = ((release_gil || isReleaseGIL(od)) && !isHoldGIL(od));
 
9100
 
8884
9101
        if (needsNew && generating_c)
8885
9102
        {
8886
9103
            prcode(fp,
8897
9114
                );
8898
9115
        }
8899
9116
 
8900
 
        if (release_gil || isReleaseGIL(od))
 
9117
        if (rgil)
8901
9118
            prcode(fp,
8902
9119
"            Py_BEGIN_ALLOW_THREADS\n"
8903
9120
                );
9098
9315
 
9099
9316
        generateCatch(od->exceptions, &od->pysig, fp);
9100
9317
 
9101
 
        if (release_gil || isReleaseGIL(od))
 
9318
        if (rgil)
9102
9319
            prcode(fp,
9103
9320
"            Py_END_ALLOW_THREADS\n"
9104
9321
                );
9350
9567
 
9351
9568
    if (od != NULL && need_owner)
9352
9569
        prcode(fp,
9353
 
"        sipWrapper *sipOwner;\n"
 
9570
"        sipWrapper *sipOwner = 0;\n"
9354
9571
            );
9355
9572
 
9356
9573
    if (handle_self)
9417
9634
 
9418
9635
            break;
9419
9636
 
 
9637
        case wstring_type:
 
9638
            if (ad->nrderefs == 0 || (isOutArg(ad) && ad->nrderefs == 1))
 
9639
                fmt = "w";
 
9640
            else if (isArray(ad))
 
9641
                fmt = "A";
 
9642
            else
 
9643
                fmt = "x";
 
9644
 
 
9645
            break;
 
9646
 
9420
9647
        case enum_type:
9421
9648
            fmt = (ad->u.ed->fqcname != NULL) ? "E" : "e";
9422
9649
            break;
9704
9931
 
9705
9932
    if (fc == 'J')
9706
9933
    {
9707
 
        if (isThisTransferred(ad) || isGetWrapper(ad))
 
9934
        if (isThisTransferred(ad))
 
9935
            flags |= 0x20;
 
9936
 
 
9937
        if (isGetWrapper(ad))
9708
9938
            flags |= 0x08;
9709
9939
 
9710
9940
        if (ad->u.cd->convtocode == NULL || isConstrained(ad))
9761
9991
    {
9762
9992
        argDef *ad = &sd->args[a];
9763
9993
 
9764
 
        if (isInArg(ad) && hasConvertToCode(ad))
 
9994
        if (!isInArg(ad))
 
9995
            continue;
 
9996
 
 
9997
        if (ad->atype == wstring_type && ad->nrderefs == 1)
 
9998
        {
 
9999
            if (generating_c || !isConstArg(ad))
 
10000
                prcode(fp,
 
10001
"            sipFree(a%d);\n"
 
10002
                    , a);
 
10003
            else
 
10004
                prcode(fp,
 
10005
"            sipFree(const_cast<wchar_t *>(a%d));\n"
 
10006
                    , a);
 
10007
        }
 
10008
        else if (hasConvertToCode(ad))
9765
10009
        {
9766
10010
            const char *fstr, *sstr;
9767
10011