18
18
* Boston, MA 02111-1307, USA.
21
#include "girffi-private.h"
22
22
#include "girnode.h"
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
28
* compiler makes. (Ignoring > 32 bit enumerations)
51
52
ENUM_6 = ((guint)G_MAXINT) + 1 /* compiler could use uint32 */
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. */
60
56
ENUM_7 = -1 /* compiler could use int8, int16, int32 */
60
ENUM_8 = -129 /* compiler could use int16, int32 */
64
ENUM_9 = G_MINSHORT - 1 /* compiler could use int32 */
67
68
compute_enum_storage_type (GIrNodeEnum *enum_node)
73
76
if (enum_node->storage_type != GI_TYPE_TAG_VOID) /* already done */
78
81
GIrNodeValue *value = l->data;
79
82
if (value->value > max_value)
80
83
max_value = value->value;
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;
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);
97
width = sizeof(Enum9);
94
width = sizeof (Enum6);
101
if (max_value <= 127)
103
width = sizeof (Enum1);
104
signed_type = (gint64)(Enum1)(-1) < 0;
106
else if (max_value <= 255)
108
width = sizeof (Enum2);
109
signed_type = (gint64)(Enum2)(-1) < 0;
111
else if (max_value <= G_MAXSHORT)
113
width = sizeof (Enum3);
114
signed_type = (gint64)(Enum3)(-1) < 0;
116
else if (max_value <= G_MAXUSHORT)
118
width = sizeof (Enum4);
119
signed_type = (gint64)(Enum4)(-1) < 0;
121
else if (max_value <= G_MAXINT)
123
width = sizeof (Enum5);
124
signed_type = (gint64)(Enum5)(-1) < 0;
128
width = sizeof (Enum6);
129
signed_type = (gint64)(Enum6)(-1) < 0;
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;
105
142
g_error ("Unexpected enum width %d", width);
147
get_interface_size_alignment (GIrNodeType *type,
184
get_interface_size_alignment (GIrTypelibBuild *build,
155
GIrModule *iface_module;
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);
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);
165
g_ir_node_compute_offsets (iface, iface_module,
166
iface_module == module ? modules : NULL);
201
_g_ir_node_compute_offsets (build, iface);
168
203
switch (iface->type)
241
275
gint elt_size, elt_alignment;
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))
259
293
if (type->tag == GI_TYPE_TAG_INTERFACE)
261
return get_interface_size_alignment (type, module, modules, size, alignment, who);
295
return get_interface_size_alignment (build, type, size, alignment, who);
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);
267
301
if (type_ffi == &ffi_type_void)
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);
319
353
#define ALIGN(n, align) (((n) + (align) - 1) & ~((align) - 1))
322
compute_struct_field_offsets (GIrNode *node,
356
compute_struct_field_offsets (GIrTypelibBuild *build,
327
360
gint *alignment_out)
347
380
int member_alignment;
349
if (get_field_size_alignment (field, node,
382
if (get_field_size_alignment (build, field, node,
351
383
&member_size, &member_alignment))
353
385
size = ALIGN (size, member_alignment);
416
447
int member_alignment;
418
if (get_field_size_alignment (field, node,
449
if (get_field_size_alignment (build,field, node,
420
450
&member_size, &member_alignment))
422
452
size = MAX (size, member_size);
449
check_needs_computation (GIrNode *node,
479
check_needs_computation (GIrTypelibBuild *build,
483
GIrModule *module = build->module;
454
485
* 0: Not yet computed
455
486
* >0: Previously succeeded
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
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.
479
g_ir_node_compute_offsets (GIrNode *node,
509
_g_ir_node_compute_offsets (GIrTypelibBuild *build,
512
gboolean appended_stack;
515
appended_stack = node != (GIrNode*)build->stack->data;
517
appended_stack = TRUE;
519
build->stack = g_list_prepend (build->stack, node);
483
521
switch (node->type)
485
523
case G_IR_NODE_BOXED:
487
525
GIrNodeBoxed *boxed = (GIrNodeBoxed *)node;
489
if (!check_needs_computation (node, module, boxed->alignment))
527
if (!check_needs_computation (build, node, boxed->alignment))
492
compute_struct_field_offsets (node, boxed->members,
530
compute_struct_field_offsets (build, node, boxed->members,
494
531
&boxed->size, &boxed->alignment);
499
536
GIrNodeStruct *struct_ = (GIrNodeStruct *)node;
501
if (!check_needs_computation (node, module, struct_->alignment))
538
if (!check_needs_computation (build, node, struct_->alignment))
504
compute_struct_field_offsets (node, struct_->members,
541
compute_struct_field_offsets (build, node, struct_->members,
506
542
&struct_->size, &struct_->alignment);
512
548
GIrNodeInterface *iface = (GIrNodeInterface *)node;
514
if (!check_needs_computation (node, module, iface->alignment))
550
if (!check_needs_computation (build, node, iface->alignment))
517
compute_struct_field_offsets (node, iface->members,
553
compute_struct_field_offsets (build, node, iface->members,
519
554
&iface->size, &iface->alignment);
524
559
GIrNodeUnion *union_ = (GIrNodeUnion *)node;
526
if (!check_needs_computation (node, module, union_->alignment))
561
if (!check_needs_computation (build, node, union_->alignment))
529
compute_union_field_offsets (node, union_->members,
564
compute_union_field_offsets (build, (GIrNode*)union_, union_->members,
531
565
&union_->size, &union_->alignment);