~ubuntu-branches/ubuntu/trusty/gobject-introspection/trusty

« back to all changes in this revision

Viewing changes to girepository/giroffsets.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2011-03-22 00:32:36 UTC
  • mfrom: (1.4.1 upstream) (3.3.33 multiarch)
  • Revision ID: james.westby@ubuntu.com-20110322003236-4spdgfk1vai6xay1
Tags: 0.10.4-2
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * Boston, MA 02111-1307, USA.
19
19
 */
20
20
 
21
 
#include "girffi-private.h"
 
21
#include "girffi.h"
22
22
#include "girnode.h"
 
23
#include <string.h>
23
24
 
24
25
/* The C standard specifies that an enumeration can be any char or any signed
25
 
 * or unsigned integer type capable of resresenting all the values of the
 
26
 * or unsigned integer type capable of representing all the values of the
26
27
 * enumeration. We use test enumerations to figure out what choices the
27
 
 * compiler makes.
 
28
 * compiler makes. (Ignoring > 32 bit enumerations)
28
29
 */
29
30
 
30
31
typedef enum {
51
52
  ENUM_6 = ((guint)G_MAXINT) + 1 /* compiler could use uint32 */
52
53
} Enum6;
53
54
 
54
 
/* GIrNodeValue has guint32 values, so if it matters to the ABI whether
55
 
 * constant values are signed, we are in trouble. And we don't handle
56
 
 * enums with > 32 bit values. */
57
 
 
58
 
#if 0
59
55
typedef enum {
60
56
  ENUM_7 = -1 /* compiler could use int8, int16, int32 */
61
57
} Enum7;
62
58
 
63
 
/* etc... */
64
 
#endif
 
59
typedef enum {
 
60
  ENUM_8 = -129 /* compiler could use int16, int32 */
 
61
} Enum8;
 
62
 
 
63
typedef enum {
 
64
  ENUM_9 = G_MINSHORT - 1 /* compiler could use int32 */
 
65
} Enum9;
65
66
 
66
67
static void
67
68
compute_enum_storage_type (GIrNodeEnum *enum_node)
68
69
{
69
70
  GList *l;
70
 
  gint32 max_value = 0;
 
71
  gint64 max_value = 0;
 
72
  gint64 min_value = 0;
71
73
  int width;
 
74
  gboolean signed_type;
72
75
 
73
76
  if (enum_node->storage_type != GI_TYPE_TAG_VOID) /* already done */
74
77
    return;
78
81
      GIrNodeValue *value = l->data;
79
82
      if (value->value > max_value)
80
83
        max_value = value->value;
81
 
    }
82
 
 
83
 
  if (max_value < 128)
84
 
    width = sizeof (Enum1);
85
 
  else if (max_value < 256)
86
 
    width = sizeof (Enum2);
87
 
  else if (max_value < G_MAXSHORT)
88
 
    width = sizeof (Enum3);
89
 
  else if (max_value < G_MAXUSHORT)
90
 
    width = sizeof (Enum4);
91
 
  else if (max_value < G_MAXINT)
92
 
    width = sizeof (Enum5);
 
84
      if (value->value < min_value)
 
85
        min_value = value->value;
 
86
    }
 
87
 
 
88
  if (min_value < 0)
 
89
    {
 
90
      signed_type = TRUE;
 
91
 
 
92
      if (min_value > -128 && max_value <= 127)
 
93
        width = sizeof(Enum7);
 
94
      else if (min_value >= G_MINSHORT && max_value <= G_MAXSHORT)
 
95
        width = sizeof(Enum8);
 
96
      else
 
97
        width = sizeof(Enum9);
 
98
    }
93
99
  else
94
 
    width = sizeof (Enum6);
 
100
    {
 
101
      if (max_value <= 127)
 
102
        {
 
103
          width = sizeof (Enum1);
 
104
          signed_type = (gint64)(Enum1)(-1) < 0;
 
105
        }
 
106
      else if (max_value <= 255)
 
107
        {
 
108
          width = sizeof (Enum2);
 
109
          signed_type = (gint64)(Enum2)(-1) < 0;
 
110
        }
 
111
      else if (max_value <= G_MAXSHORT)
 
112
        {
 
113
          width = sizeof (Enum3);
 
114
          signed_type = (gint64)(Enum3)(-1) < 0;
 
115
        }
 
116
      else if (max_value <= G_MAXUSHORT)
 
117
        {
 
118
          width = sizeof (Enum4);
 
119
          signed_type = (gint64)(Enum4)(-1) < 0;
 
120
        }
 
121
      else if (max_value <= G_MAXINT)
 
122
        {
 
123
          width = sizeof (Enum5);
 
124
          signed_type = (gint64)(Enum5)(-1) < 0;
 
125
        }
 
126
      else
 
127
        {
 
128
          width = sizeof (Enum6);
 
129
          signed_type = (gint64)(Enum6)(-1) < 0;
 
130
        }
 
131
    }
95
132
 
96
133
  if (width == 1)
97
 
    enum_node->storage_type = GI_TYPE_TAG_UINT8;
 
134
    enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT8 : GI_TYPE_TAG_UINT8;
98
135
  else if (width == 2)
99
 
    enum_node->storage_type = GI_TYPE_TAG_UINT16;
 
136
    enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT16 : GI_TYPE_TAG_UINT16;
100
137
  else if (width == 4)
101
 
    enum_node->storage_type = GI_TYPE_TAG_UINT32;
 
138
    enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT32 : GI_TYPE_TAG_UINT32;
102
139
  else if (width == 8)
103
 
    enum_node->storage_type = GI_TYPE_TAG_UINT64;
 
140
    enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT64 : GI_TYPE_TAG_UINT64;
104
141
  else
105
142
    g_error ("Unexpected enum width %d", width);
106
143
}
144
181
}
145
182
 
146
183
static gboolean
147
 
get_interface_size_alignment (GIrNodeType *type,
148
 
                              GIrModule   *module,
149
 
                              GList       *modules,
 
184
get_interface_size_alignment (GIrTypelibBuild   *build,
 
185
                              GIrNodeType *type,
150
186
                              gint        *size,
151
187
                              gint        *alignment,
152
188
                              const char  *who)
153
189
{
154
190
  GIrNode *iface;
155
 
  GIrModule *iface_module;
156
191
 
157
 
  if (!g_ir_find_node (module, modules, type->interface, &iface, &iface_module))
 
192
  iface = _g_ir_find_node (build, ((GIrNode*)type)->module, type->interface);
 
193
  if (!iface)
158
194
    {
159
 
      g_ir_module_fatal (module, 0, "Can't resolve type '%s' for %s", type->interface, who);
 
195
      _g_ir_module_fatal (build, 0, "Can't resolve type '%s' for %s", type->interface, who);
160
196
      *size = -1;
161
197
      *alignment = -1;
162
198
      return FALSE;
163
199
    }
164
200
 
165
 
  g_ir_node_compute_offsets (iface, iface_module,
166
 
                             iface_module == module ? modules : NULL);
 
201
  _g_ir_node_compute_offsets (build, iface);
167
202
 
168
203
  switch (iface->type)
169
204
    {
212
247
      {
213
248
        g_warning ("%s has is not a pointer and is of type %s",
214
249
                   who,
215
 
                   g_ir_node_type_to_string (iface->type));
 
250
                   _g_ir_node_type_to_string (iface->type));
216
251
        *size = -1;
217
252
        *alignment = -1;
218
253
        break;
223
258
}
224
259
 
225
260
static gboolean
226
 
get_type_size_alignment (GIrNodeType *type,
227
 
                         GIrModule   *module,
228
 
                         GList       *modules,
 
261
get_type_size_alignment (GIrTypelibBuild   *build,
 
262
                         GIrNodeType *type,
229
263
                         gint        *size,
230
264
                         gint        *alignment,
231
265
                         const char  *who)
241
275
      gint elt_size, elt_alignment;
242
276
 
243
277
      if (!type->has_size
244
 
          || !get_type_size_alignment(type->parameter_type1, module, modules,
 
278
          || !get_type_size_alignment(build, type->parameter_type1,
245
279
                                      &elt_size, &elt_alignment, who))
246
280
        {
247
281
          *size = -1;
258
292
    {
259
293
      if (type->tag == GI_TYPE_TAG_INTERFACE)
260
294
        {
261
 
          return get_interface_size_alignment (type, module, modules, size, alignment, who);
 
295
          return get_interface_size_alignment (build, type, size, alignment, who);
262
296
        }
263
297
      else
264
298
        {
265
 
          type_ffi = g_ir_ffi_get_ffi_type (type->tag, type->is_pointer);
 
299
          type_ffi = gi_type_tag_get_ffi_type (type->tag, type->is_pointer);
266
300
 
267
301
          if (type_ffi == &ffi_type_void)
268
302
            {
291
325
}
292
326
 
293
327
static gboolean
294
 
get_field_size_alignment (GIrNodeField *field,
 
328
get_field_size_alignment (GIrTypelibBuild    *build,
 
329
                          GIrNodeField *field,
295
330
                          GIrNode      *parent_node,
296
 
                          GIrModule    *module,
297
 
                          GList        *modules,
298
331
                          gint         *size,
299
332
                          gint         *alignment)
300
333
{
 
334
  GIrModule *module = build->module;
301
335
  gchar *who;
302
336
  gboolean success;
303
337
 
310
344
      success = TRUE;
311
345
    }
312
346
  else
313
 
    success = get_type_size_alignment (field->type, module, modules, size, alignment, who);
 
347
    success = get_type_size_alignment (build, field->type, size, alignment, who);
314
348
  g_free (who);
315
349
 
316
350
  return success;
319
353
#define ALIGN(n, align) (((n) + (align) - 1) & ~((align) - 1))
320
354
 
321
355
static gboolean
322
 
compute_struct_field_offsets (GIrNode     *node,
 
356
compute_struct_field_offsets (GIrTypelibBuild   *build,
 
357
                              GIrNode     *node,
323
358
                              GList       *members,
324
 
                              GIrModule   *module,
325
 
                              GList       *modules,
326
359
                              gint        *size_out,
327
360
                              gint        *alignment_out)
328
361
{
346
379
              int member_size;
347
380
              int member_alignment;
348
381
 
349
 
              if (get_field_size_alignment (field, node,
350
 
                                            module, modules,
 
382
              if (get_field_size_alignment (build, field, node,
351
383
                                            &member_size, &member_alignment))
352
384
                {
353
385
                  size = ALIGN (size, member_alignment);
388
420
}
389
421
 
390
422
static gboolean
391
 
compute_union_field_offsets (GIrNode     *node,
 
423
compute_union_field_offsets (GIrTypelibBuild *build,
 
424
                             GIrNode     *node,
392
425
                             GList       *members,
393
 
                             GIrModule   *module,
394
 
                             GList       *modules,
395
426
                             gint        *size_out,
396
427
                             gint        *alignment_out)
397
428
{
415
446
              int member_size;
416
447
              int member_alignment;
417
448
 
418
 
              if (get_field_size_alignment (field, node,
419
 
                                            module, modules,
 
449
              if (get_field_size_alignment (build,field, node,
420
450
                                            &member_size, &member_alignment))
421
451
                {
422
452
                  size = MAX (size, member_size);
446
476
}
447
477
 
448
478
static gboolean
449
 
check_needs_computation (GIrNode   *node,
450
 
                         GIrModule *module,
 
479
check_needs_computation (GIrTypelibBuild *build,
 
480
                         GIrNode   *node,
451
481
                         gint       alignment)
452
482
{
 
483
  GIrModule *module = build->module;
453
484
  /*
454
485
   *  0: Not yet computed
455
486
   * >0: Previously succeeded
466
497
}
467
498
 
468
499
/**
469
 
 * g_ir_node_compute_offsets:
 
500
 * _g_ir_node_compute_offsets:
 
501
 * @build: Current typelib build
470
502
 * @node: a #GIrNode
471
 
 * @module: Current module being processed
472
 
 * @modules: all currently loaded modules
473
503
 *
474
504
 * If a node is a a structure or union, makes sure that the field
475
505
 * offsets have been computed, and also computes the overall size and
476
506
 * alignment for the type.
477
507
 */
478
508
void
479
 
g_ir_node_compute_offsets (GIrNode   *node,
480
 
                           GIrModule *module,
481
 
                           GList     *modules)
 
509
_g_ir_node_compute_offsets (GIrTypelibBuild *build,
 
510
                            GIrNode         *node)
482
511
{
 
512
  gboolean appended_stack;
 
513
 
 
514
  if (build->stack)
 
515
    appended_stack = node != (GIrNode*)build->stack->data; 
 
516
  else
 
517
    appended_stack = TRUE;
 
518
  if (appended_stack)
 
519
    build->stack = g_list_prepend (build->stack, node);
 
520
 
483
521
  switch (node->type)
484
522
    {
485
523
    case G_IR_NODE_BOXED:
486
524
      {
487
525
        GIrNodeBoxed *boxed = (GIrNodeBoxed *)node;
488
526
 
489
 
        if (!check_needs_computation (node, module, boxed->alignment))
 
527
        if (!check_needs_computation (build, node, boxed->alignment))
490
528
          return;
491
529
 
492
 
        compute_struct_field_offsets (node, boxed->members,
493
 
                                      module, modules,
 
530
        compute_struct_field_offsets (build, node, boxed->members,
494
531
                                      &boxed->size, &boxed->alignment);
495
532
        break;
496
533
      }
498
535
      {
499
536
        GIrNodeStruct *struct_ = (GIrNodeStruct *)node;
500
537
 
501
 
        if (!check_needs_computation (node, module, struct_->alignment))
 
538
        if (!check_needs_computation (build, node, struct_->alignment))
502
539
          return;
503
540
 
504
 
        compute_struct_field_offsets (node, struct_->members,
505
 
                                      module, modules,
 
541
        compute_struct_field_offsets (build, node, struct_->members,
506
542
                                      &struct_->size, &struct_->alignment);
507
543
        break;
508
544
      }
511
547
      {
512
548
        GIrNodeInterface *iface = (GIrNodeInterface *)node;
513
549
 
514
 
        if (!check_needs_computation (node, module, iface->alignment))
 
550
        if (!check_needs_computation (build, node, iface->alignment))
515
551
          return;
516
552
 
517
 
        compute_struct_field_offsets (node, iface->members,
518
 
                                      module, modules,
 
553
        compute_struct_field_offsets (build, node, iface->members,
519
554
                                      &iface->size, &iface->alignment);
520
555
        break;
521
556
      }
523
558
      {
524
559
        GIrNodeUnion *union_ = (GIrNodeUnion *)node;
525
560
 
526
 
        if (!check_needs_computation (node, module, union_->alignment))
 
561
        if (!check_needs_computation (build, node, union_->alignment))
527
562
          return;
528
563
 
529
 
        compute_union_field_offsets (node, union_->members,
530
 
                                     module, modules,
 
564
        compute_union_field_offsets (build, (GIrNode*)union_, union_->members,
531
565
                                     &union_->size, &union_->alignment);
532
566
        break;
533
567
      }
544
578
        break;
545
579
      }
546
580
    default:
547
 
      /* Nothing to do */
548
 
      return;
 
581
      break;
549
582
    }
 
583
  
 
584
  if (appended_stack)
 
585
    build->stack = g_list_delete_link (build->stack, build->stack);
550
586
}