pygi-cache.c 37.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* -*- Mode: C; c-basic-offset: 4 -*-
 * vim: tabstop=4 shiftwidth=4 expandtab
 *
 * Copyright (C) 2011 John (J5) Palmieri <johnp@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */
21 22

#include "pygi-info.h"
23 24
#include "pygi-cache.h"
#include "pygi-argument.h"
25
#include "pygi-type.h"
26
#include <girepository.h>
27

28
PyGIArgCache * _arg_cache_in_new_from_type_info (GITypeInfo *type_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
29 30 31 32 33 34 35
                                                 GIArgInfo *arg_info,
                                                 PyGIFunctionCache *function_cache,
                                                 GITypeTag type_tag,
                                                 GITransfer transfer,
                                                 GIDirection direction,
                                                 gint c_arg_index,
                                                 gint py_arg_index);
36
/* cleanup */
John (J5) Palmieri's avatar
John (J5) Palmieri committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
void
_pygi_arg_cache_free(PyGIArgCache *cache)
{
    if (cache == NULL)
        return;

    if (cache->arg_info != NULL)
        g_base_info_unref(cache->arg_info);
    if (cache->destroy_notify)
        cache->destroy_notify(cache);
    else
        g_slice_free(PyGIArgCache, cache);
}

static void
52
_interface_cache_free_func (PyGIInterfaceCache *cache)
53 54 55 56
{
    if (cache != NULL) {
        Py_XDECREF(cache->py_type);
        g_slice_free(PyGIInterfaceCache, cache);
57 58
        if (cache->type_name != NULL)
            g_free(cache->type_name);
59 60 61
    }
}

John (J5) Palmieri's avatar
John (J5) Palmieri committed
62 63
static void
_hash_cache_free_func(PyGIHashCache *cache)
64
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
65 66 67
    if (cache != NULL) {
        _pygi_arg_cache_free(cache->key_cache);
        _pygi_arg_cache_free(cache->value_cache);
68
        g_slice_free(PyGIHashCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
69
    }
70 71
}

72
static void
John (J5) Palmieri's avatar
John (J5) Palmieri committed
73
_sequence_cache_free_func(PyGISequenceCache *cache)
74
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
75 76
    if (cache != NULL) {
        _pygi_arg_cache_free(cache->item_cache);
77
        g_slice_free(PyGISequenceCache, cache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
78
    }
79 80
}

81
static void
John (J5) Palmieri's avatar
John (J5) Palmieri committed
82
_callback_cache_free_func(PyGICallbackCache *cache)
83 84 85 86 87 88
{
    if (cache != NULL)
        g_slice_free(PyGICallbackCache, cache);
}

void
John (J5) Palmieri's avatar
John (J5) Palmieri committed
89
_pygi_function_cache_free(PyGIFunctionCache *cache)
90 91 92
{
    int i;

93 94 95
    if (cache == NULL)
        return;

96 97 98
    g_slist_free(cache->out_args);
    for (i = 0; i < cache->n_args; i++) {
        PyGIArgCache *tmp = cache->args_cache[i];
99
        _pygi_arg_cache_free(tmp);
100
    }
101 102 103
    if (cache->return_cache != NULL)
        _pygi_arg_cache_free(cache->return_cache);

104
    g_slice_free1(cache->n_args * sizeof(PyGIArgCache *), cache->args_cache);
105 106 107 108 109
    g_slice_free(PyGIFunctionCache, cache);
}

/* cache generation */
static inline PyGIFunctionCache *
110
_function_cache_new_from_function_info(GIFunctionInfo *function_info)
111 112 113 114 115 116 117 118
{
    PyGIFunctionCache *fc;
    GIFunctionInfoFlags flags;

    fc = g_slice_new0(PyGIFunctionCache);
    flags = g_function_info_get_flags(function_info);
    fc->is_method = flags & GI_FUNCTION_IS_METHOD;
    fc->is_constructor = flags & GI_FUNCTION_IS_CONSTRUCTOR;
119

120
    fc->n_args = g_callable_info_get_n_args ( (GICallableInfo *) function_info) + (fc->is_method ? 1: 0);
121 122
    if (fc->n_args > 0)
        fc->args_cache = g_slice_alloc0(fc->n_args * sizeof(PyGIArgCache *));
123 124 125 126

    return fc;
}

127 128 129 130
static inline PyGIInterfaceCache *
_interface_cache_new_from_interface_info(GIInterfaceInfo *iface_info)
{
    PyGIInterfaceCache *ic;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
131

132
    ic = g_slice_new0(PyGIInterfaceCache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
133
    ((PyGIArgCache *)ic)->destroy_notify = (GDestroyNotify)_interface_cache_free_func;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    ic->g_type = g_registered_type_info_get_g_type ( (GIRegisteredTypeInfo *)iface_info);
    if (ic->g_type != G_TYPE_NONE) {
        ic->py_type = _pygi_type_get_from_g_type (ic->g_type);
    } else {
        /* FIXME: avoid passing GI infos to noncache API */
        ic->py_type = _pygi_type_import_by_gi_info ( (GIBaseInfo *) iface_info);
    }

    if (ic->py_type == NULL)
        return NULL;

    ic->type_name == _pygi_g_base_info_get_fullname(iface_info);
    return ic;
}

149
static inline PyGISequenceCache *
150 151 152 153
_sequence_cache_new_from_type_info(GITypeInfo *type_info)
{
    PyGISequenceCache *sc;
    GITypeInfo *item_type_info;
154
    GITypeTag item_type_tag;
155 156

    sc = g_slice_new0(PyGISequenceCache);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
157
    ((PyGIArgCache *)sc)->destroy_notify = (GDestroyNotify)_sequence_cache_free_func;
158 159 160 161

    sc->fixed_size = -1;
    sc->len_arg_index = -1;
    sc->is_zero_terminated = g_type_info_is_zero_terminated(type_info);
162
    if (!sc->is_zero_terminated)
163 164
        sc->fixed_size = g_type_info_get_array_fixed_size(type_info);
    if (sc->fixed_size < 0)
165 166
        sc->len_arg_index = g_type_info_get_array_length (type_info);

167
    item_type_info = g_type_info_get_param_type (type_info, 0);
168
    item_type_tag = g_type_info_get_tag (item_type_info);
169

170 171
    /* FIXME: support out also */
    sc->item_cache = _arg_cache_in_new_from_type_info(item_type_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
172
                                                      NULL,
173 174 175 176 177 178
                                                      NULL,
                                                      item_type_tag,
                                                      GI_TRANSFER_EVERYTHING,
                                                      GI_DIRECTION_IN,
                                                      0, 0);

179 180 181 182
    if (sc->item_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)sc);
        return NULL;
    }
John (J5) Palmieri's avatar
John (J5) Palmieri committed
183

184 185
    sc->item_cache->type_tag = item_type_tag;
    sc->item_size = _pygi_g_type_tag_size(item_type_tag);
186
    g_base_info_unref( (GIBaseInfo *) item_type_info);
187

188
    return sc;
189
}
John (J5) Palmieri's avatar
John (J5) Palmieri committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static inline PyGIHashCache *
_hash_cache_new_from_type_info(GITypeInfo *type_info)
{
    PyGIHashCache *hc;
    GITypeInfo *key_type_info;
    GITypeTag key_type_tag;
    GITypeInfo *value_type_info;
    GITypeTag value_type_tag;

    hc = g_slice_new0(PyGIHashCache);
    ((PyGIArgCache *)hc)->destroy_notify = (GDestroyNotify)_hash_cache_free_func;
    key_type_info = g_type_info_get_param_type (type_info, 0);
    key_type_tag = g_type_info_get_tag (key_type_info);
    value_type_info = g_type_info_get_param_type (type_info, 1);
    value_type_tag = g_type_info_get_tag (value_type_info);

    hc->key_cache = _arg_cache_in_new_from_type_info(key_type_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
207
                                                     NULL,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
208 209 210 211 212 213 214 215 216 217 218 219
                                                     NULL,
                                                     key_type_tag,
                                                     GI_TRANSFER_EVERYTHING,
                                                     GI_DIRECTION_IN,
                                                     0, 0);

    if (hc->key_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)hc);
        return NULL;
    }

    hc->value_cache = _arg_cache_in_new_from_type_info(value_type_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
220
                                                       NULL,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
                                                       NULL,
                                                       value_type_tag,
                                                       GI_TRANSFER_EVERYTHING,
                                                       GI_DIRECTION_IN,
                                                       0, 0);

    if (hc->value_cache == NULL) {
        _pygi_arg_cache_free((PyGIArgCache *)hc);
        return NULL;
    }

    hc->key_cache->type_tag = key_type_tag;
    hc->value_cache->type_tag = value_type_tag;

    g_base_info_unref( (GIBaseInfo *)key_type_info);
    g_base_info_unref( (GIBaseInfo *)value_type_info);

    return hc;
}
240

John (J5) Palmieri's avatar
John (J5) Palmieri committed
241 242 243 244 245 246 247 248 249 250 251 252 253
static inline PyGICallbackCache *
_callback_cache_new_from_arg_info(GIArgInfo *arg_info)
{
   PyGICallbackCache *cc;

   cc = g_slice_new0(PyGICallbackCache);
   cc->user_data_index = g_arg_info_get_closure(arg_info);
   cc->destroy_notify_index = g_arg_info_get_destroy(arg_info);
   cc->scope = g_arg_info_get_scope(arg_info);

   return cc;
}

254 255 256 257 258
static inline PyGIArgCache *
_arg_cache_new(void)
{
    return g_slice_new0(PyGIArgCache);
}
259 260
/* process in args */

261 262
static inline PyGIArgCache *
_arg_cache_new_for_in_void(void)
263
{
264
     PyGIArgCache *arg_cache = _arg_cache_new();
265
     arg_cache->in_marshaller = _pygi_marshal_in_void;
266

267
     return arg_cache;
268 269
}

270 271
static inline PyGIArgCache *
_arg_cache_new_for_in_boolean(void)
272
{
273
    PyGIArgCache *arg_cache = _arg_cache_new();
274
    arg_cache->in_marshaller = _pygi_marshal_in_boolean;
275
    return arg_cache;
276 277
}

278 279
static inline PyGIArgCache *
_arg_cache_new_for_in_int8(void)
280
{
281
    PyGIArgCache *arg_cache = _arg_cache_new();
282
    arg_cache->in_marshaller = _pygi_marshal_in_int8;
283
    return arg_cache;
284 285
}

286 287
static inline PyGIArgCache *
_arg_cache_new_for_in_uint8(void)
288
{
289
    PyGIArgCache *arg_cache = _arg_cache_new();
290
    arg_cache->in_marshaller = _pygi_marshal_in_uint8;
291
    return arg_cache;
292 293
}

294 295
static inline PyGIArgCache *
_arg_cache_new_for_in_int16(void)
296
{
297
    PyGIArgCache *arg_cache = _arg_cache_new();
298
    arg_cache->in_marshaller = _pygi_marshal_in_int16;
299
    return arg_cache;
300 301
}

302 303
static inline PyGIArgCache *
_arg_cache_new_for_in_uint16(void)
304
{
305
    PyGIArgCache *arg_cache = _arg_cache_new();
306
    arg_cache->in_marshaller = _pygi_marshal_in_uint16;
307
    return arg_cache;
308 309
}

310 311
static inline PyGIArgCache *
_arg_cache_new_for_in_int32(void)
312
{
313
    PyGIArgCache *arg_cache = _arg_cache_new();
314
    arg_cache->in_marshaller = _pygi_marshal_in_int32;
315
    return arg_cache;
316 317
}

318 319
static inline PyGIArgCache *
_arg_cache_new_for_in_uint32(void)
320
{
321
    PyGIArgCache *arg_cache = _arg_cache_new();
322
    arg_cache->in_marshaller = _pygi_marshal_in_uint32;
323
    return arg_cache;
324 325
}

326 327
static inline PyGIArgCache *
_arg_cache_new_for_in_int64(void)
328
{
329
    PyGIArgCache *arg_cache = _arg_cache_new();
330
    arg_cache->in_marshaller = _pygi_marshal_in_int64;
331
    return arg_cache;
332 333
}

334 335
static inline PyGIArgCache *
_arg_cache_new_for_in_uint64(void)
336
{
337
    PyGIArgCache *arg_cache = _arg_cache_new();
338
    arg_cache->in_marshaller = _pygi_marshal_in_uint64;
339
    return arg_cache;
340 341
}

342 343
static inline PyGIArgCache *
_arg_cache_new_for_in_float(void)
344
{
345
    PyGIArgCache *arg_cache = _arg_cache_new();
346
    arg_cache->in_marshaller = _pygi_marshal_in_float;
347
    return arg_cache;
348 349
}

350 351
static inline PyGIArgCache *
_arg_cache_new_for_in_double(void)
352
{
353
    PyGIArgCache *arg_cache = _arg_cache_new();
354
    arg_cache->in_marshaller = _pygi_marshal_in_double;
355
    return arg_cache;
356 357
}

358 359
static inline PyGIArgCache *
_arg_cache_new_for_in_unichar(void)
360
{
361
    PyGIArgCache *arg_cache = _arg_cache_new();
362
    arg_cache->in_marshaller = _pygi_marshal_in_unichar;
363
    return arg_cache;
364 365
}

366 367
static inline PyGIArgCache *
_arg_cache_new_for_in_gtype(void)
368
{
369
    PyGIArgCache *arg_cache = _arg_cache_new();
370
    arg_cache->in_marshaller = _pygi_marshal_in_gtype;
371
    return arg_cache;
372 373
}

374 375
static inline PyGIArgCache *
_arg_cache_new_for_in_utf8(GITransfer transfer)
376
{
377
    PyGIArgCache *arg_cache = _arg_cache_new();
378
    arg_cache->in_marshaller = _pygi_marshal_in_utf8;
379 380 381
    if (arg_cache->transfer == GI_TRANSFER_NOTHING)
        arg_cache->cleanup = g_free;

382
    return arg_cache;
383 384
}

385 386
static inline PyGIArgCache *
_arg_cache_new_for_in_filename(GITransfer transfer)
387
{
388
    PyGIArgCache *arg_cache = _arg_cache_new();
389
    arg_cache->in_marshaller = _pygi_marshal_in_filename;
390 391 392
    if (arg_cache->transfer == GI_TRANSFER_NOTHING)
        arg_cache->cleanup = g_free;

393
    return arg_cache;
394 395
}

396
static inline PyGIArgCache *
397 398
_arg_cache_new_for_in_array(PyGIFunctionCache *function_cache,
                            GITypeInfo *type_info,
399
                            GITransfer transfer)
400
{
401 402 403 404 405 406
    PyGISequenceCache *seq_cache = _sequence_cache_new_from_type_info(type_info);
    PyGIArgCache *arg_cache = (PyGIArgCache *)seq_cache;

    seq_cache->len_arg_index = g_type_info_get_array_length(type_info);
    if (seq_cache->len_arg_index >= 0) {
        PyGIArgCache *aux_cache = _arg_cache_new();
John (J5) Palmieri's avatar
John (J5) Palmieri committed
407
        aux_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
408 409 410 411 412 413 414 415 416
        if (function_cache->args_cache[seq_cache->len_arg_index] != NULL) {
            PyGIArgCache *invalid_cache = function_cache->args_cache[seq_cache->len_arg_index];
            arg_cache->c_arg_index = invalid_cache->c_arg_index;
            _pygi_arg_cache_free(invalid_cache);
        }

        function_cache->args_cache[seq_cache->len_arg_index] = aux_cache;
    }

417
    arg_cache->in_marshaller = _pygi_marshal_in_array;
418 419

    /* arg_cache->cleanup = _pygi_cleanup_array; */
420
    return arg_cache;
421 422
}

423 424 425
static inline PyGIArgCache *
_arg_cache_new_for_in_glist(GITypeInfo *type_info,
                                      GITransfer transfer)
426
{
427
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
428
    arg_cache->in_marshaller = _pygi_marshal_in_glist;
429 430
    /* arg_cache->cleanup = */

431
    return arg_cache;
432 433
}

434 435 436
static inline PyGIArgCache *
_arg_cache_new_for_in_gslist(GITypeInfo *type_info,
                                       GITransfer transfer)
437
{
438
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
439
    arg_cache->in_marshaller = _pygi_marshal_in_gslist;
440 441
    /* arg_cache->cleanup = */

442
    return arg_cache;
443 444
}

445 446
static inline PyGIArgCache *
_arg_cache_new_for_in_ghash(GITypeInfo *type_info)
447
{
John (J5) Palmieri's avatar
John (J5) Palmieri committed
448 449
    PyGIArgCache *arg_cache = (PyGIArgCache *)_hash_cache_new_from_type_info(type_info);
    arg_cache->in_marshaller = _pygi_marshal_in_ghash;
450
    return arg_cache;
451 452
}

453 454
static inline PyGIArgCache *
_arg_cache_new_for_in_gerror(void)
455
{
456
    PyGIArgCache *arg_cache = _arg_cache_new();
457
    arg_cache->in_marshaller = _pygi_marshal_in_gerror;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
458
    arg_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
459
    return arg_cache;
460 461
}

462 463 464 465 466 467 468 469 470 471
static inline PyGIArgCache *
_arg_cache_new_for_in_interface_union(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_inteface_union;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

472

John (J5) Palmieri's avatar
John (J5) Palmieri committed
473
static void
474 475 476 477
_g_slice_free_gvalue_func(GValue *value) {
    g_slice_free(GValue, value);
}

478
static inline PyGIArgCache *
479 480
_arg_cache_new_for_in_interface_struct(GIInterfaceInfo *iface_info,
                                       GITransfer transfer)
481
{
482
    PyGIInterfaceCache *iface_cache = _interface_cache_new_from_interface_info(iface_info);
483
    PyGIArgCache *arg_cache = (PyGIArgCache *)iface_cache;
484 485 486 487 488 489 490
    iface_cache->is_foreign = g_struct_info_is_foreign( (GIStructInfo*)iface_info);
    arg_cache->in_marshaller = _pygi_marshal_in_interface_struct;
    if (iface_cache->g_type == G_TYPE_VALUE)
        arg_cache->cleanup = _g_slice_free_gvalue_func;
    if (iface_cache->g_type == G_TYPE_CLOSURE)
        arg_cache->cleanup = g_closure_unref;

491 492 493 494
    return arg_cache;
}

static inline PyGIArgCache *
495 496
_arg_cache_new_for_in_interface_object(GIInterfaceInfo *iface_info,
                                       GITransfer transfer)
497
{
498
    PyGIArgCache *arg_cache = (PyGIArgCache *)_interface_cache_new_from_interface_info(iface_info);
499 500 501 502 503 504 505 506
    arg_cache->in_marshaller = _pygi_marshal_in_interface_object;
    if (transfer == GI_TRANSFER_EVERYTHING)
        arg_cache->cleanup = (GDestroyNotify)g_object_unref;

    return arg_cache;
}

static inline PyGIArgCache *
507 508
_arg_cache_new_for_in_interface_boxed(GIInterfaceInfo *iface_info,
                                      GITransfer transfer)
509
{
510 511
    PyGIArgCache *arg_cache = (PyGIArgCache *)_interface_cache_new_from_interface_info(iface_info);
    arg_cache->in_marshaller = _pygi_marshal_in_interface_boxed;
512 513 514 515
    return arg_cache;
}

static inline PyGIArgCache *
John (J5) Palmieri's avatar
John (J5) Palmieri committed
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
_arg_cache_new_for_in_interface_callback(PyGIFunctionCache *function_cache,
                                         GIArgInfo *arg_info)
{
    PyGICallbackCache *callback_cache = _callback_cache_new_from_arg_info(arg_info);
    PyGIArgCache *arg_cache = (PyGIArgCache *)callback_cache;
    if (callback_cache->user_data_index >= 0) {
        PyGIArgCache *user_data_arg_cache = _arg_cache_new();
        user_data_arg_cache->aux_type = PYGI_AUX_TYPE_HAS_PYARG;
        function_cache->args_cache[callback_cache->user_data_index] = user_data_arg_cache;
    }
    
    if (callback_cache->destroy_notify_index) {
        PyGIArgCache *destroy_arg_cache = _arg_cache_new();
        destroy_arg_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
        function_cache->args_cache[callback_cache->destroy_notify_index] = destroy_arg_cache;
    }
    arg_cache->in_marshaller = _pygi_marshal_in_interface_callback;
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
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_in_interface_enum(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_enum;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_in_interface_flags(void)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->in_marshaller = _pygi_marshal_in_flags;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

PyGIArgCache *
_arg_cache_in_new_from_interface_info (GIInterfaceInfo *iface_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
558
                                       GIArgInfo *arg_info,
559 560 561 562 563 564 565 566 567 568 569 570 571 572
                                       PyGIFunctionCache *function_cache,
                                       GIInfoType info_type,
                                       GITransfer transfer,
                                       GIDirection direction,
                                       gint c_arg_index,
                                       gint py_arg_index)
{
    PyGIArgCache *arg_cache = NULL;

    switch (info_type) {
        case GI_INFO_TYPE_UNION:
            arg_cache = _arg_cache_new_for_in_interface_union();
            break;
        case GI_INFO_TYPE_STRUCT:
573 574
            arg_cache = _arg_cache_new_for_in_interface_struct(iface_info,
                                                               transfer);
575 576 577
            break;
        case GI_INFO_TYPE_OBJECT:
        case GI_INFO_TYPE_INTERFACE:
578 579
            arg_cache = _arg_cache_new_for_in_interface_object(iface_info,
                                                               transfer);
580 581
            break;
        case GI_INFO_TYPE_BOXED:
582 583
            arg_cache = _arg_cache_new_for_in_interface_boxed(iface_info,
                                                              transfer);
584 585
            break;
        case GI_INFO_TYPE_CALLBACK:
John (J5) Palmieri's avatar
John (J5) Palmieri committed
586 587
            arg_cache = _arg_cache_new_for_in_interface_callback(function_cache,
                                                                 arg_info);
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
            break;
        case GI_INFO_TYPE_ENUM:
            arg_cache = _arg_cache_new_for_in_interface_enum();
            break;
        case GI_INFO_TYPE_FLAGS:
            arg_cache = _arg_cache_new_for_in_interface_flags();
            break;
        default:
            g_assert_not_reached();
    }

    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = GI_TYPE_TAG_INTERFACE;
        arg_cache->py_arg_index = py_arg_index;
        arg_cache->c_arg_index = c_arg_index;
    }
606 607

    return arg_cache;
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
/* process out args */

static inline PyGIArgCache *
_arg_cache_new_for_out_void(void)
{
     PyGIArgCache *arg_cache = _arg_cache_new();
     arg_cache->out_marshaller = _pygi_marshal_out_void;

     return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_boolean(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_boolean;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int8(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int8;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint8(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint8;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int16(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int16;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint16(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint16;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int32(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int32;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint32(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint32;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_int64(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_int64;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_uint64(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_uint64;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_float(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_float;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_double(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_double;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_unichar(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_unichar;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_gtype(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_gtype;
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_utf8(GITransfer transfer)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_utf8;

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_filename(GITransfer transfer)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_filename;

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_array(GITypeInfo *type_info,
                            GITransfer transfer)
{
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
    arg_cache->out_marshaller = _pygi_marshal_out_array;

    /* arg_cache->cleanup = _pygi_cleanup_array; */
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_glist(GITypeInfo *type_info,
                                      GITransfer transfer)
{
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
    arg_cache->out_marshaller = _pygi_marshal_out_glist;
    /* arg_cache->cleanup = */

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_gslist(GITypeInfo *type_info,
                                       GITransfer transfer)
{
    PyGIArgCache *arg_cache = (PyGIArgCache *)_sequence_cache_new_from_type_info(type_info);
    arg_cache->out_marshaller = _pygi_marshal_out_gslist;
    /* arg_cache->cleanup = */

    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_ghash(GITypeInfo *type_info)
{
    PyGIArgCache *arg_cache = NULL;
    /*arg_cache->out_marshaller = _pygi_marshal_out_ghash;*/
    PyErr_Format(PyExc_NotImplementedError,
                 "Caching for this type is not fully implemented yet");
    return arg_cache;
}

static inline PyGIArgCache *
_arg_cache_new_for_out_gerror(void)
{
    PyGIArgCache *arg_cache = _arg_cache_new();
    arg_cache->out_marshaller = _pygi_marshal_out_gerror;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
791
    arg_cache->aux_type = PYGI_AUX_TYPE_IGNORE;
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
    return arg_cache;
}


PyGIArgCache *
_arg_cache_out_new_from_type_info (GITypeInfo *type_info,
                                   PyGIFunctionCache *function_cache,
                                   GITypeTag type_tag,
                                   GITransfer transfer,
                                   GIDirection direction,
                                   gint c_arg_index)
{
    PyGIArgCache *arg_cache = NULL;

    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
           arg_cache = _arg_cache_new_for_out_void();
           break;
       case GI_TYPE_TAG_BOOLEAN:
           arg_cache = _arg_cache_new_for_out_boolean();
           break;
       case GI_TYPE_TAG_INT8:
           arg_cache = _arg_cache_new_for_out_int8();
           break;
       case GI_TYPE_TAG_UINT8:
           arg_cache = _arg_cache_new_for_out_uint8();
           break;
       case GI_TYPE_TAG_INT16:
           arg_cache = _arg_cache_new_for_out_uint16();
           break;
       case GI_TYPE_TAG_UINT16:
           arg_cache = _arg_cache_new_for_out_uint16();
           break;
       case GI_TYPE_TAG_INT32:
           arg_cache = _arg_cache_new_for_out_int32();
           break;
       case GI_TYPE_TAG_UINT32:
           arg_cache = _arg_cache_new_for_out_uint32();
           break;
       case GI_TYPE_TAG_INT64:
           arg_cache = _arg_cache_new_for_out_int64();
           break;
       case GI_TYPE_TAG_UINT64:
           arg_cache = _arg_cache_new_for_out_uint64();
           break;
       case GI_TYPE_TAG_FLOAT:
           arg_cache = _arg_cache_new_for_out_float();
           break;
       case GI_TYPE_TAG_DOUBLE:
           arg_cache = _arg_cache_new_for_out_double();
           break;
       case GI_TYPE_TAG_UNICHAR:
           arg_cache = _arg_cache_new_for_out_unichar();
           break;
       case GI_TYPE_TAG_GTYPE:
           arg_cache = _arg_cache_new_for_out_gtype();
           break;
       case GI_TYPE_TAG_UTF8:
           arg_cache = _arg_cache_new_for_out_utf8(transfer);
           break;
       case GI_TYPE_TAG_FILENAME:
           arg_cache = _arg_cache_new_for_out_filename(transfer);
           break;
       case GI_TYPE_TAG_ARRAY:
           arg_cache = _arg_cache_new_for_out_array(type_info,
                                                   transfer);
           break;
       case GI_TYPE_TAG_INTERFACE:
           /*
           {
               GIInterfaceInfo *interface_info = g_type_info_get_interface(type_info);
               GIInfoType info_type = g_base_info_get_type( (GIBaseInfo *) interface_info);
               arg_cache = _arg_cache_out_new_from_interface_info(interface_info,
                                                                 function_cache,
                                                                 info_type,
                                                                 transfer,
                                                                 direction,
                                                                 c_arg_index,
                                                                 py_arg_index);

               g_base_info_unref( (GIBaseInfo *) interface_info);
               return arg_cache;
           }
           */
       case GI_TYPE_TAG_GLIST:
           arg_cache = _arg_cache_new_for_out_glist(type_info,
                                                   transfer);
           break;
       case GI_TYPE_TAG_GSLIST:
           arg_cache = _arg_cache_new_for_out_gslist(type_info,
                                                    transfer);
           break;
       case GI_TYPE_TAG_GHASH:
           arg_cache = _arg_cache_new_for_out_ghash(type_info);
           break;
       case GI_TYPE_TAG_ERROR:
           arg_cache = _arg_cache_new_for_out_gerror();
           break;
    }

    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = type_tag;
        arg_cache->c_arg_index = c_arg_index;
    }

    return arg_cache;
}
901

902 903
PyGIArgCache *
_arg_cache_in_new_from_type_info (GITypeInfo *type_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
904
                                  GIArgInfo *arg_info,
905 906 907 908 909 910
                                  PyGIFunctionCache *function_cache,
                                  GITypeTag type_tag,
                                  GITransfer transfer,
                                  GIDirection direction,
                                  gint c_arg_index,
                                  gint py_arg_index)
911
{
912
    PyGIArgCache *arg_cache = NULL;
913 914 915

    switch (type_tag) {
       case GI_TYPE_TAG_VOID:
916
           arg_cache = _arg_cache_new_for_in_void();
917 918
           break;
       case GI_TYPE_TAG_BOOLEAN:
919
           arg_cache = _arg_cache_new_for_in_boolean();
920 921
           break;
       case GI_TYPE_TAG_INT8:
922
           arg_cache = _arg_cache_new_for_in_int8();
923 924
           break;
       case GI_TYPE_TAG_UINT8:
925
           arg_cache = _arg_cache_new_for_in_uint8();
926 927
           break;
       case GI_TYPE_TAG_INT16:
928
           arg_cache = _arg_cache_new_for_in_uint16();
929 930
           break;
       case GI_TYPE_TAG_UINT16:
931
           arg_cache = _arg_cache_new_for_in_uint16();
932 933
           break;
       case GI_TYPE_TAG_INT32:
934
           arg_cache = _arg_cache_new_for_in_int32();
935 936
           break;
       case GI_TYPE_TAG_UINT32:
937
           arg_cache = _arg_cache_new_for_in_uint32();
938 939
           break;
       case GI_TYPE_TAG_INT64:
940
           arg_cache = _arg_cache_new_for_in_int64();
941 942
           break;
       case GI_TYPE_TAG_UINT64:
943
           arg_cache = _arg_cache_new_for_in_uint64();
944 945
           break;
       case GI_TYPE_TAG_FLOAT:
946
           arg_cache = _arg_cache_new_for_in_float();
947 948
           break;
       case GI_TYPE_TAG_DOUBLE:
949
           arg_cache = _arg_cache_new_for_in_double();
950 951
           break;
       case GI_TYPE_TAG_UNICHAR:
952
           arg_cache = _arg_cache_new_for_in_unichar();
953 954
           break;
       case GI_TYPE_TAG_GTYPE:
955
           arg_cache = _arg_cache_new_for_in_gtype();
956 957
           break;
       case GI_TYPE_TAG_UTF8:
958
           arg_cache = _arg_cache_new_for_in_utf8(transfer);
959 960
           break;
       case GI_TYPE_TAG_FILENAME:
961
           arg_cache = _arg_cache_new_for_in_filename(transfer);
962 963
           break;
       case GI_TYPE_TAG_ARRAY:
964 965
           arg_cache = _arg_cache_new_for_in_array(function_cache,
                                                   type_info,
966
                                                   transfer);
967 968
           break;
       case GI_TYPE_TAG_INTERFACE:
969 970 971 972
           {
               GIInterfaceInfo *interface_info = g_type_info_get_interface(type_info);
               GIInfoType info_type = g_base_info_get_type( (GIBaseInfo *) interface_info);
               arg_cache = _arg_cache_in_new_from_interface_info(interface_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
973
                                                                 arg_info,
974 975 976 977 978 979 980 981 982 983
                                                                 function_cache,
                                                                 info_type,
                                                                 transfer,
                                                                 direction,
                                                                 c_arg_index,
                                                                 py_arg_index);

               g_base_info_unref( (GIBaseInfo *) interface_info);
               return arg_cache;
           }
984
       case GI_TYPE_TAG_GLIST:
985 986
           arg_cache = _arg_cache_new_for_in_glist(type_info,
                                                   transfer);
987 988
           break;
       case GI_TYPE_TAG_GSLIST:
989 990
           arg_cache = _arg_cache_new_for_in_gslist(type_info,
                                                    transfer);
991 992
           break;
       case GI_TYPE_TAG_GHASH:
993
           arg_cache = _arg_cache_new_for_in_ghash(type_info);
994 995
           break;
       case GI_TYPE_TAG_ERROR:
996
           arg_cache = _arg_cache_new_for_in_gerror();
997 998 999
           break;
    }

1000 1001 1002 1003 1004 1005 1006 1007 1008
    if (arg_cache != NULL) {
        arg_cache->direction = direction;
        arg_cache->transfer = transfer;
        arg_cache->type_tag = type_tag;
        arg_cache->py_arg_index = py_arg_index;
        arg_cache->c_arg_index = c_arg_index;
    }

    return arg_cache;
1009 1010
}

1011
static inline gboolean
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1012
_args_cache_generate(GIFunctionInfo *function_info,
1013 1014
                     PyGIFunctionCache *function_cache)
{
1015
    int arg_index = 0;
1016
    int i;
1017 1018 1019 1020 1021 1022
    GITypeInfo *return_info;
    GITypeTag return_type_tag;
    PyGIArgCache *return_cache;
    /* cache the return arg */
    return_info = g_callable_info_get_return_type( (GICallableInfo *)function_info);
    return_type_tag = g_type_info_get_tag(return_info);
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1023
    return_cache =
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
        _arg_cache_out_new_from_type_info(return_info,
                                          function_cache,
                                          return_type_tag,
                                          GI_TRANSFER_EVERYTHING,
                                          GI_DIRECTION_OUT,
                                          -1);

    function_cache->return_cache = return_cache;
    g_base_info_unref(return_info);

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
    /* first arg is the instance */
    if (function_cache->is_method) {
        GIInterfaceInfo *interface_info;
        PyGIArgCache *instance_cache;
        GIInfoType info_type;

        interface_info = g_base_info_get_container ( (GIBaseInfo *) function_info);
        info_type = g_base_info_get_type(interface_info);

        instance_cache =
            _arg_cache_in_new_from_interface_info(interface_info,
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1045
                                                  NULL,
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
                                                  function_cache,
                                                  info_type,
                                                  GI_TRANSFER_NOTHING,
                                                  GI_DIRECTION_IN,
                                                  arg_index,
                                                  0);

        g_base_info_unref( (GIBaseInfo *) interface_info);

        if (instance_cache == NULL)
            return FALSE;

        function_cache->args_cache[arg_index] = instance_cache;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1059

1060 1061
        arg_index++;
        function_cache->n_in_args++;
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1062
        function_cache->n_py_args++;
1063 1064
    }

1065

1066
    for (i=0; arg_index < function_cache->n_args; arg_index++, i++) {
1067
        PyGIArgCache *arg_cache = NULL;
1068
        GIArgInfo *arg_info;
1069
        GITypeInfo *type_info;
1070 1071 1072 1073
        GIDirection direction;
        GITransfer transfer;
        GITypeTag type_tag;
        gint py_arg_index;
1074

1075 1076 1077 1078 1079
        /* must be an aux arg filled in by its owner
         * fill in it's c_arg_index, add to the in count
         * and continue
         */
        if (function_cache->args_cache[arg_index] != NULL) {
John (J5) Palmieri's avatar
John (J5) Palmieri committed
1080 1081 1082 1083 1084 1085
            arg_cache = function_cache->args_cache[arg_index];
            if (arg_cache->aux_type == PYGI_AUX_TYPE_HAS_PYARG) {
                arg_cache->py_arg_index = function_cache->n_py_args;
                function_cache->n_py_args++;
            }   
            arg_cache->c_arg_index = function_cache->n_in_args;