/*
 * Generate the Python representation of a type.
 */
static void pyiType(sipSpec *pt, moduleDef *mod, argDef *ad, int out, int sec,
        ifaceFileList *defined, int pep484, FILE *fp)
{
    const char *type_name;
    typeHintDef *thd;

    /* 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, defined, pep484, fp);
        return;
    }

    /* For classes and mapped types we need the default implementation. */
    if (ad->atype == class_type || ad->atype == mapped_type)
    {
        classDef *cd = ad->u.cd;
        mappedTypeDef *mtd = ad->u.mtd;

        getDefaultImplementation(pt, ad->atype, &cd, &mtd);

        if (cd != NULL)
        {
            prClassRef(cd, mod, defined, pep484, fp);
        }
        else
        {
            /*
             * This should never happen as it should have been picked up when
             * generating code - but maybe we haven't been asked to generate
             * code.
             */
            fprintf(fp, anyObject(pep484));
        }

        return;
    }

    type_name = NULL;

    switch (ad->atype)
    {
    case enum_type:
        if (ad->u.ed->pyname != NULL)
            prEnumRef(ad->u.ed, mod, defined, pep484, fp);
        else
            type_name = "int";

        break;

    case capsule_type:
        type_name = scopedNameTail(ad->u.cap);
        break;

    case struct_type:
    case void_type:
        type_name = "sip.voidptr";
        break;

    case signal_type:
        type_name = "QT_SIGNAL";
        break;

    case slot_type:
        type_name = "QT_SLOT_QT_SIGNAL";
        break;

    case rxcon_type:
    case rxdis_type:
        if (sec)
        {
            type_name = (pep484 ? "typing.Callable[..., None]" : "Callable[..., None]");
        }
        else
        {
            /* The class should always be found. */
            if (pt->qobject_cd != NULL)
                prClassRef(pt->qobject_cd, mod, defined, pep484, fp);
            else
                type_name = anyObject(pep484);
        }

        break;

    case qobject_type:
        type_name = "QObject";
        break;

    case ustring_type:
    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:
        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 = anyObject(pep484);
        break;

    case pytuple_type:
        type_name = (pep484 ? "typing.Tuple" : "Tuple");
        break;

    case pylist_type:
        type_name = (pep484 ? "typing.List" : "List");
        break;

    case pydict_type:
        type_name = (pep484 ? "typing.Dict" : "Dict");
        break;

    case pycallable_type:
        type_name = (pep484 ? "typing.Callable[..., None]" : "Callable[..., None]");
        break;

    case pyslice_type:
        type_name = "slice";
        break;

    case pytype_type:
        type_name = "type";
        break;

    case pybuffer_type:
        type_name = "sip.Buffer";
        break;

    case ellipsis_type:
        type_name = "*";
        break;

    case slotcon_type:
    case anyslot_type:
        type_name = "QT_SLOT";
        break;

    default:
        type_name = anyObject(pep484);
    }

    if (type_name != NULL)
        fprintf(fp, "%s", type_name);
}
Ejemplo n.º 2
0
/*
 * 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 qobject_type:
            restPyClass(pt->qobject_cd, fp);
            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, "\"");
}