35
43
/* An entry in the sorted array of methods. */
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. */
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. */
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);
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"
623
for (mld = pt->allimports; mld != NULL; mld = mld->next)
644
for (mld = pt->module->allimports; mld != NULL; mld = mld->next)
625
646
"extern const sipExportedModuleDef *sipModuleAPI_%s_%s;\n"
626
647
,mname,mld->module->name);
649
if (optQ_OBJECT4(pt))
652
"typedef const QMetaObject *(*sip_qt_metaobject_func)(sipWrapper *,sipWrapperType *,const QMetaObject *);\n"
653
"extern sip_qt_metaobject_func sip_%s_qt_metaobject;\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"
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
709
741
generateUsedIncludes(pt->used, FALSE, fp);
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.
748
if (pt->qobjclass >= 0)
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"
711
760
/* Define the names. */
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"
1435
for (mld = pt->allimports; mld != NULL; mld = mld->next)
1490
for (mld = pt->module->allimports; mld != NULL; mld = mld->next)
1437
1492
"const sipExportedModuleDef *sipModuleAPI_%s_%s;\n"
1438
1493
,mname,mld->module->name);
1495
if (optQ_OBJECT4(pt))
1498
"sip_qt_metaobject_func sip_%s_qt_metaobject;\n"
1499
"sip_qt_metacall_func sip_%s_qt_metacall;\n"
1440
1503
/* Generate the Python module initialisation function. */
1556
1619
, xd->pyname, xd->exceptionnr);
1622
/* Generate any Qt metatype registration calls. */
1623
if (optRegisterTypes(pt))
1624
for (cd = pt->classes; cd != NULL; cd = cd->next)
1626
if (cd->iff->module != pt->module)
1629
generateRegisterMetaType(cd, fp);
1559
1632
/* Generate the post-initialisation code. */
1560
1633
generateCppCodeBlock(pt->postinitcode,fp);
1636
* This has to be done after the post-initialisation code in case this
1637
* module is exporting the symbol.
1639
if (optQ_OBJECT4(pt))
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"
3222
3306
if (vd->getcode == NULL || vd->setcode == NULL)
3232
if ((atype == string_type || atype == sstring_type || atype == ustring_type) && vd->type.nrderefs == 0)
3240
sprintf(name,"sipVal%s",ptr);
3242
generateNamedValueType(&mod,name,fp);
3311
generateNamedValueType(&vd->type, "sipVal", fp);
3285
3354
int pyobj = FALSE;
3291
prcode(fp,"sipVal");
3293
generateNamedValueType(&vd->type,"sipVal",fp);
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) ? "&" : ""));
3297
3359
generateVarMember(vd, fp);
3669
3741
case enum_type:
3671
" sipVal = (%E)PyInt_AsLong(sipPy);\n"
3742
prcode(fp, "(%E)PyInt_AsLong(sipPy);\n"
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)";
3679
fmt = "\tsipVal = (signed char *)PyString_AsString(sipPy);\n";
3750
rhs = "(signed char *)PyString_AsString(sipPy)";
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)";
3686
fmt = "\tsipVal = (unsigned char *)PyString_AsString(sipPy);\n";
3757
rhs = "(unsigned char *)PyString_AsString(sipPy)";
3689
3760
case string_type:
3690
3761
if (ad->nrderefs == 0)
3691
fmt = "\tsipValPtr = PyString_AsString(sipPy);\n";
3693
fmt = "\tsipVal = PyString_AsString(sipPy);\n";
3762
rhs = "sipString_AsChar(sipPy)";
3764
rhs = "PyString_AsString(sipPy)";
3768
if (ad->nrderefs == 0)
3769
rhs = "sipUnicode_AsWChar(sipPy)";
3771
rhs = "sipUnicode_AsWString(sipPy)";
3696
3774
case float_type:
3697
3775
case cfloat_type:
3698
fmt = "\tsipVal = (float)PyFloat_AsDouble(sipPy);\n";
3776
rhs = "(float)PyFloat_AsDouble(sipPy)";
3701
3779
case double_type:
3702
3780
case cdouble_type:
3703
fmt = "\tsipVal = PyFloat_AsDouble(sipPy);\n";
3781
rhs = "PyFloat_AsDouble(sipPy)";
3706
3784
case bool_type:
3707
3785
case cbool_type:
3708
fmt = "\tsipVal = (bool)PyInt_AsLong(sipPy);\n";
3786
rhs = "(bool)PyInt_AsLong(sipPy)";
3711
3789
case ushort_type:
3712
fmt = "\tsipVal = (unsigned short)sipLong_AsUnsignedLong(sipPy);\n";
3790
rhs = "(unsigned short)sipLong_AsUnsignedLong(sipPy)";
3715
3793
case short_type:
3716
fmt = "\tsipVal = (short)PyInt_AsLong(sipPy);\n";
3794
rhs = "(short)PyInt_AsLong(sipPy)";
3719
3797
case uint_type:
3720
fmt = "\tsipVal = (unsigned)sipLong_AsUnsignedLong(sipPy);\n";
3798
rhs = "(unsigned)sipLong_AsUnsignedLong(sipPy)";
3724
3802
case cint_type:
3725
fmt = "\tsipVal = (int)PyInt_AsLong(sipPy);\n";
3803
rhs = "(int)PyInt_AsLong(sipPy)";
3728
3806
case ulong_type:
3729
fmt = "\tsipVal = sipLong_AsUnsignedLong(sipPy);\n";
3807
rhs = "sipLong_AsUnsignedLong(sipPy)";
3732
3810
case long_type:
3733
fmt = "\tsipVal = PyLong_AsLong(sipPy);\n";
3811
rhs = "PyLong_AsLong(sipPy)";
3736
3814
case ulonglong_type:
3737
fmt = "\tsipVal = PyLong_AsUnsignedLongLong(sipPy);\n";
3815
rhs = "PyLong_AsUnsignedLongLong(sipPy)";
3740
3818
case longlong_type:
3741
fmt = "\tsipVal = PyLong_AsLongLong(sipPy);\n";
3819
rhs = "PyLong_AsLongLong(sipPy)";
3744
3822
case struct_type:
3746
" sipVal = (struct %S *)sipConvertToVoidPtr(sipPy);\n"
3823
prcode(fp, "(struct %S *)sipConvertToVoidPtr(sipPy);\n"
3750
3827
case void_type:
3751
fmt = "\tsipVal = sipConvertToVoidPtr(sipPy);\n";
3828
rhs = "sipConvertToVoidPtr(sipPy)";
3754
3831
case pyobject_type:
4330
4410
if (!generating_c)
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));
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"
4338
" ", classFQCName(cd)
4418
" ", classFQCName(cd)
4339
4419
, argName("sipSelf", cd->readbufcode)
4340
4420
, argName("sipSegment", cd->readbufcode)
4341
4421
, argName("sipPtrPtr", cd->readbufcode));
4366
4446
if (!generating_c)
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));
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"
4374
" ", classFQCName(cd)
4454
" ", classFQCName(cd)
4375
4455
, argName("sipSelf", cd->writebufcode)
4376
4456
, argName("sipSegment", cd->writebufcode)
4377
4457
, argName("sipPtrPtr", cd->writebufcode));
4402
4482
if (!generating_c)
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));
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"
4410
" ", classFQCName(cd)
4490
" ", classFQCName(cd)
4411
4491
, argName("sipSelf", cd->segcountcode)
4412
4492
, argName("sipLenPtr", cd->segcountcode));
4414
4494
generateClassFromVoid(cd, "sipCpp", "sipCppV", fp);
4416
4496
prcode(fp, ";\n"
4497
" SIP_SSIZE_T sipRes;\n"
4437
4517
if (!generating_c)
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));
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"
4445
" ", classFQCName(cd)
4525
" ", classFQCName(cd)
4446
4526
, argName("sipSelf", cd->charbufcode)
4447
4527
, argName("sipSegment", cd->charbufcode)
4448
4528
, argName("sipPtrPtr", cd->charbufcode));
4728
/* The metacall method if required. */
4729
if (isQObjectSubClass(cd) && optQ_OBJECT4(pt))
4733
"const QMetaObject *sip%C::metaObject() const\n"
4735
" return sip_%s_qt_metaobject(sipPySelf,sipClass_%C,%S::metaObject());\n"
4738
"int sip%C::qt_metacall(QMetaObject::Call _c,int _id,void **_a)\n"
4740
" sip%C::metaObject();\n"
4742
" _id = %S::qt_metacall(_c,_id,_a);\n"
4746
" SIP_BLOCK_THREADS\n"
4747
" _id = sip_%s_qt_metacall(sipPySelf,sipClass_%C,_c,_id,_a);\n"
4748
" SIP_UNBLOCK_THREADS\n"
4754
, pt->module->name, classFQCName(cd), classFQCName(cd)
4758
, pt->module->name, classFQCName(cd));
4648
4761
/* Generate the virtual catchers. */
4784
4897
* Generate the catcher for a virtual function.
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)
4790
4902
overDef *od = &vod->o;
4791
4903
virtHandlerDef *vhd = od->virthandler;
5136
5248
* Do what we can to provide type information to the compiler.
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)
5144
5256
else if (ad->atype == double_type || ad->atype == cdouble_type)
5146
5258
else if (ad->atype == uint_type)
5148
5260
else if (ad->atype == long_type || ad->atype == longlong_type)
5150
5262
else if (ad->atype == ulong_type || ad->atype == ulonglong_type)
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'");
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.
5458
5575
if ((res->atype == class_type || res->atype == mapped_type) && res->nrderefs == 0)
5459
5576
if (isReference(res))
5491
5608
prcode(fp, " ");
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.
5617
if (res->atype == wstring_type && res->nrderefs == 1)
5618
prcode(fp, "static ");
5493
5620
generateBaseType(&res_noconstref,fp);
5495
5622
prcode(fp," %ssipRes",(isref ? "*" : ""));
6472
6619
" %s~sip%C()%X;\n"
6473
6620
,(cd->vmembers != NULL ? "virtual " : ""),classFQCName(cd),cd->dtorexceptions);
6622
/* The metacall methods if required. */
6623
if (isQObjectSubClass(cd) && optQ_OBJECT4(pt))
6626
" const QMetaObject *metaObject() const;\n"
6627
" int qt_metacall(QMetaObject::Call,int,void **);\n"
6475
6630
/* The exposure of protected enums. */
6477
6632
generateProtectedEnums(pt,cd,fp);
7103
7264
static void generateTypeDefinition(sipSpec *pt, classDef *cd, FILE *fp)
7105
7266
char *mname = pt->module->name;
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;
7190
7352
" ", mname, classFQCName(cd));
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");
7358
prcode(fp, "%s%x", sep, ((cd->userflags << TYPE_FLAGS_SHIFT) & TYPE_FLAGS_MASK));
7362
if (isAbstractClass(cd))
7364
prcode(fp, "%sSIP_TYPE_ABSTRACT", sep);
7368
if (cd->subbase != NULL)
7370
prcode(fp, "%sSIP_TYPE_SCC", sep);
7201
7379
if (cd->real != NULL)
7848
* Generate the code to register a class as a Qt metatype.
7850
static void generateRegisterMetaType(classDef *cd, FILE *fp)
7852
int pub_def_ctor, pub_copy_ctor;
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.
7860
if (isAbstractClass(cd))
7863
if (!isPublicDtor(cd))
7866
if (classFQCName(cd)->next == NULL)
7868
if (strcmp(classBaseName(cd), "QChar") == 0)
7871
if (strcmp(classBaseName(cd), "QString") == 0)
7874
if (strcmp(classBaseName(cd), "QByteArray") == 0)
7878
pub_def_ctor = pub_copy_ctor = FALSE;
7880
for (ct = cd->ctors; ct != NULL; ct = ct->next)
7882
if (ct->cppsig == NULL || !isPublicCtor(ct))
7885
if (ct->cppsig->nrArgs == 0)
7886
pub_def_ctor = TRUE;
7887
else if (ct->cppsig->nrArgs == 1)
7889
argDef *ad = &ct->cppsig->args[0];
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;
7897
if (pub_def_ctor && pub_copy_ctor)
7899
" qRegisterMetaType<%S>(\"%S\");\n"
7900
, classFQCName(cd), classFQCName(cd));
7670
7905
* Generate the initialisation function or cast operators for the type.
7672
7907
static void generateTypeInit(sipSpec *pt, classDef *cd, FILE *fp)
7675
int need_self, need_owner, reg_type, pub_def_ctor, pub_copy_ctor;
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.
7682
reg_type = (optRegisterTypes(pt) && !isAbstractClass(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;
7690
7913
* See if we need to name the self and owner arguments so that we can
7741
7947
,classFQCName(cd),(need_self ? "sipSelf" : ""),(need_owner ? "sipOwner" : ""));
7743
if (reg_type && pub_def_ctor && pub_copy_ctor)
7745
" static bool sipRegistered = false;\n"
7747
" if (!sipRegistered)\n"
7749
" qRegisterMetaType<%S>(\"%S\");\n"
7750
" sipRegistered = true;\n"
7753
, classFQCName(cd), classFQCName(cd));
7755
7949
if (hasShadow(cd))
7757
7951
" sip%C *sipCpp = 0;\n"
8026
* We have to fiddle the type to generate the correct
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]);
9762
9992
argDef *ad = &sd->args[a];
9764
if (isInArg(ad) && hasConvertToCode(ad))
9997
if (ad->atype == wstring_type && ad->nrderefs == 1)
9999
if (generating_c || !isConstArg(ad))
10005
" sipFree(const_cast<wchar_t *>(a%d));\n"
10008
else if (hasConvertToCode(ad))
9766
10010
const char *fstr, *sstr;