~ubuntu-branches/ubuntu/maverick/sip4-qt3/maverick

« back to all changes in this revision

Viewing changes to sipgen/gencode.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-07-21 10:18:00 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100721101800-9lk0k2t6hx6pq0dh
Tags: 4.10.5-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
131
131
static void generateProtectedEnums(sipSpec *, classDef *, FILE *);
132
132
static void generateProtectedDeclarations(classDef *, FILE *);
133
133
static void generateProtectedDefinitions(classDef *, FILE *);
134
 
static void generateProtectedCallArgs(overDef *od, FILE *fp);
 
134
static void generateProtectedCallArgs(signatureDef *sd, FILE *fp);
135
135
static void generateConstructorCall(classDef *, ctorDef *, int, int,
136
136
        moduleDef *, FILE *);
137
137
static void generateHandleResult(overDef *, int, int, char *, FILE *);
2008
2008
"    /* Export the module and publish it's API. */\n"
2009
2009
"    if (sipExportModule(&sipModuleAPI_%s,SIP_API_MAJOR_NR,SIP_API_MINOR_NR,0) < 0)\n"
2010
2010
"    {\n"
2011
 
"#if PY_VERSION_HEX < 0x03010000\n"
 
2011
"#if !defined(SIP_USE_PYCAPSULE)\n"
2012
2012
"        Py_DECREF(sip_sipmod);\n"
2013
2013
"#endif\n"
2014
2014
"        SIP_MODULE_DISCARD(sipModule);\n"
2034
2034
"    /* Initialise the module now all its dependencies have been set up. */\n"
2035
2035
"    if (sipInitModule(&sipModuleAPI_%s,sipModuleDict) < 0)\n"
2036
2036
"    {\n"
2037
 
"#if PY_VERSION_HEX < 0x03010000\n"
 
2037
"#if !defined(SIP_USE_PYCAPSULE)\n"
2038
2038
"        Py_DECREF(sip_sipmod);\n"
2039
2039
"#endif\n"
2040
2040
"        SIP_MODULE_DISCARD(sipModule);\n"
2095
2095
 
2096
2096
        prcode(fp, ",NULL)) == NULL || PyDict_SetItemString(sipModuleDict,\"%s\",exceptionsTable[%d]) < 0)\n"
2097
2097
"    {\n"
2098
 
"#if PY_VERSION_HEX < 0x03010000\n"
 
2098
"#if !defined(SIP_USE_PYCAPSULE)\n"
2099
2099
"        Py_DECREF(sip_sipmod);\n"
2100
2100
"#endif\n"
2101
2101
"        SIP_MODULE_DISCARD(sipModule);\n"
2215
2215
{
2216
2216
    prcode(fp,
2217
2217
"    /* Get the SIP module's API. */\n"
2218
 
"#if PY_VERSION_HEX >= 0x03010000\n"
 
2218
"#if defined(SIP_USE_PYCAPSULE)\n"
2219
2219
"\n"
2220
2220
        );
2221
2221
 
2295
2295
static void generateSipImportVariables(FILE *fp)
2296
2296
{
2297
2297
    prcode(fp,
2298
 
"#if PY_VERSION_HEX < 0x03010000\n"
 
2298
"#if !defined(SIP_USE_PYCAPSULE)\n"
2299
2299
"    PyObject *sip_sipmod, *sip_capiobj;\n"
2300
2300
"#endif\n"
2301
2301
"\n"
4967
4967
{
4968
4968
    slotType st = md->slot;
4969
4969
 
4970
 
    return (st == len_slot || st == bool_slot || st == contains_slot ||
4971
 
        st == cmp_slot);
 
4970
    return (st == bool_slot || st == contains_slot || st == cmp_slot);
 
4971
}
 
4972
 
 
4973
 
 
4974
/*
 
4975
 * Returns TRUE if the given method is a slot that returns SIP_SSIZE_T.
 
4976
 */
 
4977
int isSSizeReturnSlot(memberDef *md)
 
4978
{
 
4979
    slotType st = md->slot;
 
4980
 
 
4981
    return (st == len_slot);
4972
4982
}
4973
4983
 
4974
4984
 
5088
5098
    {
5089
5099
        ret_int = FALSE;
5090
5100
 
5091
 
        if (isLongReturnSlot(md))
 
5101
        if (isSSizeReturnSlot(md))
 
5102
            ret_type = "SIP_SSIZE_T ";
 
5103
        else if (isLongReturnSlot(md))
5092
5104
            ret_type = "long ";
5093
5105
        else
5094
5106
            ret_type = "PyObject *";
5208
5220
            generateFunctionBody(od, cd, NULL, cd, (ed == NULL && !dontDerefSelf(od)), mod, fp);
5209
5221
 
5210
5222
    if (nr_args > 0)
 
5223
    {
5211
5224
        switch (md->slot)
5212
5225
        {
5213
5226
        case cmp_slot:
5231
5244
            break;
5232
5245
 
5233
5246
        default:
5234
 
            if (nr_args > 0)
 
5247
            if (isNumberSlot(md) || isRichCompareSlot(md) || isInplaceNumberSlot(md))
 
5248
            {
5235
5249
                prcode(fp,
5236
5250
"\n"
5237
5251
"    Py_XDECREF(sipParseErr);\n"
5239
5253
"    if (sipParseErr == Py_None)\n"
5240
5254
"        return NULL;\n"
5241
5255
                    );
 
5256
            }
5242
5257
 
5243
5258
            if (isNumberSlot(md) || isRichCompareSlot(md))
5244
5259
            {
5261
5276
                        , mod->name, slotName(md->slot), fqcname);
5262
5277
            }
5263
5278
            else if (isInplaceNumberSlot(md))
 
5279
            {
5264
5280
                prcode(fp,
5265
5281
"\n"
5266
5282
"    PyErr_Clear();\n"
5268
5284
"    Py_INCREF(Py_NotImplemented);\n"
5269
5285
"    return Py_NotImplemented;\n"
5270
5286
                    );
 
5287
            }
5271
5288
            else
 
5289
            {
5272
5290
                prcode(fp,
5273
5291
"\n"
5274
5292
"    /* Raise an exception if the arguments couldn't be parsed. */\n"
5277
5295
"    return %s;\n"
5278
5296
                    , pyname, md->pyname
5279
5297
                    ,ret_int ? "-1" : "0");
 
5298
            }
5280
5299
        }
 
5300
    }
5281
5301
 
5282
5302
    prcode(fp,
5283
5303
"}\n"
5967
5987
 
5968
5988
    for (ct = cd->ctors; ct != NULL; ct = ct->next)
5969
5989
    {
5970
 
        char *prefix;
5971
 
        int a;
5972
5990
        ctorDef *dct;
5973
5991
 
5974
5992
        if (isPrivateCtor(ct))
5993
6011
 
5994
6012
        prcode(fp,")%X: %S(",ct->exceptions,classFQCName(cd));
5995
6013
 
5996
 
        prefix = "";
5997
 
 
5998
 
        for (a = 0; a < ct->cppsig->nrArgs; ++a)
5999
 
        {
6000
 
            prcode(fp,"%sa%d",prefix,a);
6001
 
            prefix = ",";
6002
 
        }
 
6014
        generateProtectedCallArgs(ct->cppsig, fp);
6003
6015
 
6004
6016
        prcode(fp,"), sipPySelf(0)\n"
6005
6017
"{\n"
6241
6253
    overDef *od = &vod->o;
6242
6254
    argDef *res;
6243
6255
    apiVersionRangeDef *avr;
6244
 
    const char *indent;
6245
6256
 
6246
6257
    normaliseArgs(od->cppsig);
6247
6258
 
6276
6287
 
6277
6288
    restoreArgs(od->cppsig);
6278
6289
 
6279
 
    if (isNewThread(od))
6280
 
        prcode(fp,
6281
 
"    SIP_BLOCK_THREADS\n"
6282
 
"\n"
6283
 
            );
6284
 
 
6285
6290
    prcode(fp,
6286
6291
"    sip_gilstate_t sipGILState;\n"
6287
6292
"    PyObject *meth;\n"
6307
6312
"\n"
6308
6313
        ,od->common->pyname);
6309
6314
 
6310
 
    if (isNewThread(od))
6311
 
    {
6312
 
        indent = "        ";
6313
 
 
6314
 
        prcode(fp,
6315
 
"    if (meth)\n"
6316
 
"    {\n"
6317
 
"        sipStartThread();\n"
6318
 
            );
6319
 
    }
6320
 
    else
6321
 
    {
6322
 
        indent = "    ";
6323
 
 
6324
 
        prcode(fp,
 
6315
    prcode(fp,
6325
6316
"    if (!meth)\n"
6326
 
            );
6327
 
 
6328
 
        if (isAbstract(od))
6329
 
            generateVirtHandlerErrorReturn(res, "        ", fp);
6330
 
        else
6331
 
        {
6332
 
            int a;
6333
 
 
6334
 
            if (res == NULL)
6335
 
                prcode(fp,
 
6317
        );
 
6318
 
 
6319
    if (isAbstract(od))
 
6320
        generateVirtHandlerErrorReturn(res, "        ", fp);
 
6321
    else
 
6322
    {
 
6323
        int a;
 
6324
 
 
6325
        if (res == NULL)
 
6326
            prcode(fp,
6336
6327
"    {\n"
6337
6328
"        ");
6338
 
            else
6339
 
                prcode(fp,
 
6329
        else
 
6330
            prcode(fp,
6340
6331
"        return ");
6341
6332
 
6342
 
            generateUnambiguousClass(cd,vod->scope,fp);
6343
 
 
6344
 
            prcode(fp,"::%O(",od);
6345
 
 
6346
 
            for (a = 0; a < od->cppsig->nrArgs; ++a)
6347
 
                prcode(fp,"%sa%d",(a == 0 ? "" : ","),a);
6348
 
 
6349
 
            prcode(fp,");\n"
6350
 
                );
6351
 
 
6352
 
            if (res == NULL)
6353
 
                prcode(fp,
 
6333
        generateUnambiguousClass(cd,vod->scope,fp);
 
6334
 
 
6335
        prcode(fp,"::%O(",od);
 
6336
 
 
6337
        for (a = 0; a < od->cppsig->nrArgs; ++a)
 
6338
            prcode(fp,"%sa%d",(a == 0 ? "" : ","),a);
 
6339
 
 
6340
        prcode(fp,");\n"
 
6341
            );
 
6342
 
 
6343
        if (res == NULL)
 
6344
            prcode(fp,
6354
6345
"        return;\n"
6355
6346
"    }\n"
6356
 
                    );
6357
 
        }
 
6347
                );
6358
6348
    }
6359
6349
 
6360
6350
    /*
6369
6359
"\n"
6370
6360
            );
6371
6361
 
6372
 
        generateVirtHandlerCall(mod, cd, vod, res, indent, fp);
 
6362
        generateVirtHandlerCall(mod, cd, vod, res, "    ", fp);
6373
6363
    }
6374
6364
    else
6375
6365
    {
6440
6430
        }
6441
6431
    }
6442
6432
 
6443
 
    if (isNewThread(od))
6444
 
        prcode(fp,
6445
 
"\n"
6446
 
"        sipEndThread();\n"
6447
 
"    }\n"
6448
 
"\n"
6449
 
"    SIP_UNBLOCK_THREADS\n"
6450
 
            );
6451
 
 
6452
6433
    prcode(fp,
6453
6434
"}\n"
6454
6435
        );
6467
6448
    argDef *ad;
6468
6449
    int a, args_keep = FALSE, result_keep = FALSE;
6469
6450
 
 
6451
    if (isNewThread(od))
 
6452
        prcode(fp,
 
6453
"%ssipStartThread();\n"
 
6454
"\n"
 
6455
            , indent);
 
6456
 
6470
6457
    saved = *vhd->cppsig;
6471
6458
    fakeProtectedArgs(vhd->cppsig);
6472
6459
 
6554
6541
 
6555
6542
    prcode(fp,");\n"
6556
6543
        );
 
6544
 
 
6545
    if (isNewThread(od))
 
6546
        prcode(fp,
 
6547
"\n"
 
6548
"%sSIP_BLOCK_THREADS\n"
 
6549
"%ssipEndThread();\n"
 
6550
"%sSIP_UNBLOCK_THREADS\n"
 
6551
            , indent
 
6552
            , indent
 
6553
            , indent);
 
6554
 
6557
6555
}
6558
6556
 
6559
6557
 
6989
6987
                {
6990
6988
                    prcode(fp, "(sipSelfWasArg ? %S::%s(", classFQCName(vl->cd), mname);
6991
6989
 
6992
 
                    generateProtectedCallArgs(od, fp);
 
6990
                    generateProtectedCallArgs(od->cppsig, fp);
6993
6991
 
6994
6992
                    prcode(fp, ") : ");
6995
6993
                    ++parens;
7000
6998
 
7001
6999
            prcode(fp,"%s(",mname);
7002
7000
 
7003
 
            generateProtectedCallArgs(od, fp);
 
7001
            generateProtectedCallArgs(od->cppsig, fp);
7004
7002
 
7005
7003
            while (parens--)
7006
7004
                prcode(fp,")");
7048
7046
/*
7049
7047
 * Generate the arguments for a call to a protected method.
7050
7048
 */
7051
 
static void generateProtectedCallArgs(overDef *od, FILE *fp)
 
7049
static void generateProtectedCallArgs(signatureDef *sd, FILE *fp)
7052
7050
{
7053
7051
    int a;
7054
7052
 
7055
 
    for (a = 0; a < od->cppsig->nrArgs; ++a)
 
7053
    for (a = 0; a < sd->nrArgs; ++a)
7056
7054
    {
7057
 
        argDef *ad = &od->cppsig->args[a];
 
7055
        argDef *ad = &sd->args[a];
7058
7056
 
7059
7057
        if (a > 0)
7060
7058
            prcode(fp, ",");
9588
9586
        fprintf(fp, "\"\\1");
9589
9587
        prScopedPythonName(fp, cd->ecd, cd->pyname->text);
9590
9588
        fprintf(fp, ".%s", md->pyname->text);
9591
 
        prPythonSignature(pt, fp, &sig->pysig, FALSE, FALSE, FALSE, FALSE);
 
9589
        prPythonSignature(pt, fp, &sig->pysig, FALSE, FALSE, FALSE, FALSE,
 
9590
                TRUE);
9592
9591
        fprintf(fp, "\", ");
9593
9592
    }
9594
9593
    else
10294
10293
 
10295
10294
        prcode(fp,
10296
10295
"\n"
10297
 
"            Py_XDECREF(*sipUnused);\n"
 
10296
"            if (sipUnused)\n"
 
10297
"            {\n"
 
10298
"                Py_XDECREF(*sipUnused);\n"
 
10299
"            }\n"
 
10300
"\n"
10298
10301
"            sipAddException(sipError, sipParseErr);\n"
10299
10302
"\n"
10300
10303
"            if (sipError == sipErrorFail)\n"
10308
10311
            prcode(fp,
10309
10312
"            if (sipIsErr)\n"
10310
10313
"            {\n"
10311
 
"                Py_XDECREF(*sipUnused);\n"
 
10314
"                if (sipUnused)\n"
 
10315
"                {\n"
 
10316
"                    Py_XDECREF(*sipUnused);\n"
 
10317
"                }\n"
 
10318
"\n"
10312
10319
"                sipAddException(sipErrorFail, sipParseErr);\n"
10313
10320
"                return NULL;\n"
10314
10321
"            }\n"
11381
11388
        prcode(fp,
11382
11389
"                return %s;\n"
11383
11390
"\n"
11384
 
            , ((isVoidReturnSlot(od->common) || isIntReturnSlot(od->common) || isLongReturnSlot(od->common)) ? "-1" : "NULL"));
 
11391
            , ((isVoidReturnSlot(od->common) || isIntReturnSlot(od->common) || isSSizeReturnSlot(od->common) || isLongReturnSlot(od->common)) ? "-1" : "NULL"));
11385
11392
    }
11386
11393
 
11387
11394
    /* Call any pre-hook. */
11669
11676
        );
11670
11677
 
11671
11678
    /* Handle the error flag if it was used. */
11672
 
    error_value = ((isVoidReturnSlot(od->common) || isIntReturnSlot(od->common) || isLongReturnSlot(od->common)) ? "-1" : "0");
 
11679
    error_value = ((isVoidReturnSlot(od->common) || isIntReturnSlot(od->common) || isSSizeReturnSlot(od->common) || isLongReturnSlot(od->common)) ? "-1" : "0");
11673
11680
 
11674
11681
    if (error_flag)
11675
11682
    {
11706
11713
"            Py_INCREF(sipSelf);\n"
11707
11714
"            return sipSelf;\n"
11708
11715
            );
11709
 
    else if (isIntReturnSlot(od->common) || isLongReturnSlot(od->common))
 
11716
    else if (isIntReturnSlot(od->common) || isSSizeReturnSlot(od->common) || isLongReturnSlot(od->common))
11710
11717
        prcode(fp,
11711
11718
"            return sipRes;\n"
11712
11719
            );
13553
13560
 
13554
13561
        fprintf(fp, "%s", md->pyname->text);
13555
13562
        need_sec = prPythonSignature(pt, fp, &od->pysig, FALSE, TRUE, TRUE,
13556
 
                TRUE);
 
13563
                TRUE, FALSE);
13557
13564
        ++currentLineNr;
13558
13565
 
13559
13566
        if (need_sec)
13566
13573
                fprintf(fp, ".");
13567
13574
 
13568
13575
            fprintf(fp, "%s", md->pyname->text);
13569
 
            prPythonSignature(pt, fp, &od->pysig, TRUE, TRUE, TRUE, TRUE);
 
13576
            prPythonSignature(pt, fp, &od->pysig, TRUE, TRUE, TRUE, TRUE,
 
13577
                    FALSE);
13570
13578
            ++currentLineNr;
13571
13579
        }
13572
13580
    }
13638
13646
 
13639
13647
        prScopedPythonName(fp, cd->ecd, cd->pyname->text);
13640
13648
        need_sec = prPythonSignature(pt, fp, &ct->pysig, FALSE, TRUE, TRUE,
13641
 
                TRUE);
 
13649
                TRUE, FALSE);
13642
13650
        ++currentLineNr;
13643
13651
 
13644
13652
        if (need_sec)
13646
13654
            fprintf(fp, "%s", sep);
13647
13655
 
13648
13656
            prScopedPythonName(fp, cd->ecd, cd->pyname->text);
13649
 
            prPythonSignature(pt, fp, &ct->pysig, TRUE, TRUE, TRUE, TRUE);
 
13657
            prPythonSignature(pt, fp, &ct->pysig, TRUE, TRUE, TRUE, TRUE,
 
13658
                    FALSE);
13650
13659
            ++currentLineNr;
13651
13660
        }
13652
13661
    }