2012-02-03 19:42:56 +01:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
|
|
|
* GObject introspection: Compute structure offsets
|
2008-11-11 06:10:36 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* 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 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., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2010-11-09 23:06:29 +01:00
|
|
|
#include "girffi.h"
|
2008-11-11 06:10:36 +01:00
|
|
|
#include "girnode.h"
|
2010-07-26 22:26:46 +02:00
|
|
|
#include <string.h>
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
/* The C standard specifies that an enumeration can be any char or any signed
|
2010-09-14 20:17:35 +02:00
|
|
|
* or unsigned integer type capable of representing all the values of the
|
2008-11-11 06:10:36 +01:00
|
|
|
* enumeration. We use test enumerations to figure out what choices the
|
2010-09-14 20:17:35 +02:00
|
|
|
* compiler makes. (Ignoring > 32 bit enumerations)
|
2008-11-11 06:10:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_1 = 1 /* compiler could use int8, uint8, int16, uint16, int32, uint32 */
|
|
|
|
} Enum1;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_2 = 128 /* compiler could use uint8, int16, uint16, int32, uint32 */
|
|
|
|
} Enum2;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_3 = 257 /* compiler could use int16, uint16, int32, uint32 */
|
|
|
|
} Enum3;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_4 = G_MAXSHORT + 1 /* compiler could use uint16, int32, uint32 */
|
|
|
|
} Enum4;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_5 = G_MAXUSHORT + 1 /* compiler could use int32, uint32 */
|
|
|
|
} Enum5;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_6 = ((guint)G_MAXINT) + 1 /* compiler could use uint32 */
|
|
|
|
} Enum6;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_7 = -1 /* compiler could use int8, int16, int32 */
|
|
|
|
} Enum7;
|
|
|
|
|
2010-09-14 20:17:35 +02:00
|
|
|
typedef enum {
|
|
|
|
ENUM_8 = -129 /* compiler could use int16, int32 */
|
|
|
|
} Enum8;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ENUM_9 = G_MINSHORT - 1 /* compiler could use int32 */
|
|
|
|
} Enum9;
|
2008-11-11 06:10:36 +01:00
|
|
|
|
2008-11-18 13:29:10 +01:00
|
|
|
static void
|
|
|
|
compute_enum_storage_type (GIrNodeEnum *enum_node)
|
2008-11-11 06:10:36 +01:00
|
|
|
{
|
|
|
|
GList *l;
|
2010-09-14 20:17:35 +02:00
|
|
|
gint64 max_value = 0;
|
|
|
|
gint64 min_value = 0;
|
2008-11-11 06:10:36 +01:00
|
|
|
int width;
|
2010-09-14 20:17:35 +02:00
|
|
|
gboolean signed_type;
|
2008-11-18 13:29:10 +01:00
|
|
|
|
|
|
|
if (enum_node->storage_type != GI_TYPE_TAG_VOID) /* already done */
|
|
|
|
return;
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
for (l = enum_node->values; l; l = l->next)
|
|
|
|
{
|
|
|
|
GIrNodeValue *value = l->data;
|
|
|
|
if (value->value > max_value)
|
2010-03-18 22:46:56 +01:00
|
|
|
max_value = value->value;
|
2010-09-14 20:17:35 +02:00
|
|
|
if (value->value < min_value)
|
|
|
|
min_value = value->value;
|
2008-11-11 06:10:36 +01:00
|
|
|
}
|
|
|
|
|
2010-09-14 20:17:35 +02:00
|
|
|
if (min_value < 0)
|
|
|
|
{
|
|
|
|
signed_type = TRUE;
|
|
|
|
|
|
|
|
if (min_value > -128 && max_value <= 127)
|
|
|
|
width = sizeof(Enum7);
|
|
|
|
else if (min_value >= G_MINSHORT && max_value <= G_MAXSHORT)
|
|
|
|
width = sizeof(Enum8);
|
|
|
|
else
|
|
|
|
width = sizeof(Enum9);
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
else
|
2010-09-14 20:17:35 +02:00
|
|
|
{
|
|
|
|
if (max_value <= 127)
|
|
|
|
{
|
|
|
|
width = sizeof (Enum1);
|
|
|
|
signed_type = (gint64)(Enum1)(-1) < 0;
|
|
|
|
}
|
|
|
|
else if (max_value <= 255)
|
|
|
|
{
|
|
|
|
width = sizeof (Enum2);
|
|
|
|
signed_type = (gint64)(Enum2)(-1) < 0;
|
|
|
|
}
|
|
|
|
else if (max_value <= G_MAXSHORT)
|
|
|
|
{
|
|
|
|
width = sizeof (Enum3);
|
|
|
|
signed_type = (gint64)(Enum3)(-1) < 0;
|
|
|
|
}
|
|
|
|
else if (max_value <= G_MAXUSHORT)
|
|
|
|
{
|
|
|
|
width = sizeof (Enum4);
|
|
|
|
signed_type = (gint64)(Enum4)(-1) < 0;
|
|
|
|
}
|
|
|
|
else if (max_value <= G_MAXINT)
|
|
|
|
{
|
|
|
|
width = sizeof (Enum5);
|
|
|
|
signed_type = (gint64)(Enum5)(-1) < 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = sizeof (Enum6);
|
|
|
|
signed_type = (gint64)(Enum6)(-1) < 0;
|
|
|
|
}
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
if (width == 1)
|
2010-09-14 20:17:35 +02:00
|
|
|
enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT8 : GI_TYPE_TAG_UINT8;
|
2008-11-11 06:10:36 +01:00
|
|
|
else if (width == 2)
|
2010-09-14 20:17:35 +02:00
|
|
|
enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT16 : GI_TYPE_TAG_UINT16;
|
2008-11-11 06:10:36 +01:00
|
|
|
else if (width == 4)
|
2010-09-14 20:17:35 +02:00
|
|
|
enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT32 : GI_TYPE_TAG_UINT32;
|
2008-11-11 06:10:36 +01:00
|
|
|
else if (width == 8)
|
2010-09-14 20:17:35 +02:00
|
|
|
enum_node->storage_type = signed_type ? GI_TYPE_TAG_INT64 : GI_TYPE_TAG_UINT64;
|
2008-11-11 06:10:36 +01:00
|
|
|
else
|
|
|
|
g_error ("Unexpected enum width %d", width);
|
2008-11-18 13:29:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
get_enum_size_alignment (GIrNodeEnum *enum_node,
|
|
|
|
gint *size,
|
|
|
|
gint *alignment)
|
|
|
|
{
|
|
|
|
ffi_type *type_ffi;
|
|
|
|
|
|
|
|
compute_enum_storage_type (enum_node);
|
|
|
|
|
|
|
|
switch (enum_node->storage_type)
|
|
|
|
{
|
|
|
|
case GI_TYPE_TAG_INT8:
|
|
|
|
case GI_TYPE_TAG_UINT8:
|
|
|
|
type_ffi = &ffi_type_uint8;
|
|
|
|
break;
|
|
|
|
case GI_TYPE_TAG_INT16:
|
|
|
|
case GI_TYPE_TAG_UINT16:
|
|
|
|
type_ffi = &ffi_type_uint16;
|
|
|
|
break;
|
|
|
|
case GI_TYPE_TAG_INT32:
|
|
|
|
case GI_TYPE_TAG_UINT32:
|
|
|
|
type_ffi = &ffi_type_uint32;
|
|
|
|
break;
|
|
|
|
case GI_TYPE_TAG_INT64:
|
|
|
|
case GI_TYPE_TAG_UINT64:
|
|
|
|
type_ffi = &ffi_type_uint64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_error ("Unexpected enum storage type %s",
|
|
|
|
g_type_tag_to_string (enum_node->storage_type));
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
*size = type_ffi->size;
|
|
|
|
*alignment = type_ffi->alignment;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-07-26 22:26:46 +02:00
|
|
|
get_interface_size_alignment (GIrTypelibBuild *build,
|
|
|
|
GIrNodeType *type,
|
2009-02-02 17:31:02 +01:00
|
|
|
gint *size,
|
|
|
|
gint *alignment,
|
|
|
|
const char *who)
|
2008-11-11 06:10:36 +01:00
|
|
|
{
|
|
|
|
GIrNode *iface;
|
|
|
|
|
2012-07-24 10:09:38 +02:00
|
|
|
iface = _g_ir_find_node (build, ((GIrNode*)type)->module, type->giinterface);
|
2010-07-26 22:26:46 +02:00
|
|
|
if (!iface)
|
2008-11-11 06:10:36 +01:00
|
|
|
{
|
2012-07-24 10:09:38 +02:00
|
|
|
_g_ir_module_fatal (build, 0, "Can't resolve type '%s' for %s", type->giinterface, who);
|
2008-11-11 06:10:36 +01:00
|
|
|
*size = -1;
|
|
|
|
*alignment = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-16 21:08:33 +01:00
|
|
|
_g_ir_node_compute_offsets (build, iface);
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
switch (iface->type)
|
|
|
|
{
|
|
|
|
case G_IR_NODE_BOXED:
|
|
|
|
{
|
|
|
|
GIrNodeBoxed *boxed = (GIrNodeBoxed *)iface;
|
|
|
|
*size = boxed->size;
|
|
|
|
*alignment = boxed->alignment;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_IR_NODE_STRUCT:
|
|
|
|
{
|
|
|
|
GIrNodeStruct *struct_ = (GIrNodeStruct *)iface;
|
|
|
|
*size = struct_->size;
|
|
|
|
*alignment = struct_->alignment;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-11 22:10:12 +01:00
|
|
|
case G_IR_NODE_OBJECT:
|
|
|
|
case G_IR_NODE_INTERFACE:
|
|
|
|
{
|
|
|
|
GIrNodeInterface *interface = (GIrNodeInterface *)iface;
|
|
|
|
*size = interface->size;
|
|
|
|
*alignment = interface->alignment;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
case G_IR_NODE_UNION:
|
|
|
|
{
|
|
|
|
GIrNodeUnion *union_ = (GIrNodeUnion *)iface;
|
|
|
|
*size = union_->size;
|
|
|
|
*alignment = union_->alignment;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_IR_NODE_ENUM:
|
|
|
|
case G_IR_NODE_FLAGS:
|
|
|
|
{
|
|
|
|
return get_enum_size_alignment ((GIrNodeEnum *)iface,
|
|
|
|
size, alignment);
|
|
|
|
}
|
|
|
|
case G_IR_NODE_CALLBACK:
|
|
|
|
{
|
|
|
|
*size = ffi_type_pointer.size;
|
|
|
|
*alignment = ffi_type_pointer.alignment;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2009-02-02 17:31:02 +01:00
|
|
|
g_warning ("%s has is not a pointer and is of type %s",
|
|
|
|
who,
|
2010-11-16 21:08:33 +01:00
|
|
|
_g_ir_node_type_to_string (iface->type));
|
2008-11-11 06:10:36 +01:00
|
|
|
*size = -1;
|
|
|
|
*alignment = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-11 14:45:13 +01:00
|
|
|
return *alignment > 0;
|
2008-11-11 06:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-07-26 22:26:46 +02:00
|
|
|
get_type_size_alignment (GIrTypelibBuild *build,
|
|
|
|
GIrNodeType *type,
|
2009-02-02 17:31:02 +01:00
|
|
|
gint *size,
|
|
|
|
gint *alignment,
|
|
|
|
const char *who)
|
2008-11-11 06:10:36 +01:00
|
|
|
{
|
|
|
|
ffi_type *type_ffi;
|
|
|
|
|
2010-06-02 19:36:59 +02:00
|
|
|
if (type->is_pointer)
|
|
|
|
{
|
|
|
|
type_ffi = &ffi_type_pointer;
|
|
|
|
}
|
|
|
|
else if (type->tag == GI_TYPE_TAG_ARRAY)
|
2009-02-02 17:31:02 +01:00
|
|
|
{
|
|
|
|
gint elt_size, elt_alignment;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
2009-02-02 17:31:02 +01:00
|
|
|
if (!type->has_size
|
2010-07-26 22:26:46 +02:00
|
|
|
|| !get_type_size_alignment(build, type->parameter_type1,
|
2009-02-02 17:31:02 +01:00
|
|
|
&elt_size, &elt_alignment, who))
|
|
|
|
{
|
|
|
|
*size = -1;
|
|
|
|
*alignment = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-03-24 19:00:06 +01:00
|
|
|
|
2009-02-02 17:31:02 +01:00
|
|
|
*size = type->size * elt_size;
|
|
|
|
*alignment = elt_alignment;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
2009-02-02 17:31:02 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (type->tag == GI_TYPE_TAG_INTERFACE)
|
|
|
|
{
|
2010-07-26 22:26:46 +02:00
|
|
|
return get_interface_size_alignment (build, type, size, alignment, who);
|
2008-11-11 06:10:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-09 23:06:29 +01:00
|
|
|
type_ffi = gi_type_tag_get_ffi_type (type->tag, type->is_pointer);
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
if (type_ffi == &ffi_type_void)
|
|
|
|
{
|
2009-02-02 17:31:02 +01:00
|
|
|
g_warning ("%s has void type", who);
|
2008-11-11 06:10:36 +01:00
|
|
|
*size = -1;
|
|
|
|
*alignment = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (type_ffi == &ffi_type_pointer)
|
|
|
|
{
|
2009-02-02 17:31:02 +01:00
|
|
|
g_warning ("%s has is not a pointer and is of type %s",
|
|
|
|
who,
|
2008-11-11 06:10:36 +01:00
|
|
|
g_type_tag_to_string (type->tag));
|
|
|
|
*size = -1;
|
|
|
|
*alignment = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (type_ffi);
|
|
|
|
*size = type_ffi->size;
|
|
|
|
*alignment = type_ffi->alignment;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-02-02 17:31:02 +01:00
|
|
|
static gboolean
|
2010-07-26 22:26:46 +02:00
|
|
|
get_field_size_alignment (GIrTypelibBuild *build,
|
|
|
|
GIrNodeField *field,
|
2009-02-02 17:31:02 +01:00
|
|
|
GIrNode *parent_node,
|
|
|
|
gint *size,
|
|
|
|
gint *alignment)
|
|
|
|
{
|
2010-07-26 22:26:46 +02:00
|
|
|
GIrModule *module = build->module;
|
2009-02-02 17:31:02 +01:00
|
|
|
gchar *who;
|
|
|
|
gboolean success;
|
2010-03-24 19:00:06 +01:00
|
|
|
|
2009-02-02 17:31:02 +01:00
|
|
|
who = g_strdup_printf ("field %s.%s.%s", module->name, parent_node->name, ((GIrNode *)field)->name);
|
|
|
|
|
2009-11-09 19:17:23 +01:00
|
|
|
if (field->callback)
|
|
|
|
{
|
|
|
|
*size = ffi_type_pointer.size;
|
|
|
|
*alignment = ffi_type_pointer.alignment;
|
|
|
|
success = TRUE;
|
|
|
|
}
|
|
|
|
else
|
2010-07-26 22:26:46 +02:00
|
|
|
success = get_type_size_alignment (build, field->type, size, alignment, who);
|
2009-02-02 17:31:02 +01:00
|
|
|
g_free (who);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2011-06-21 13:44:56 +02:00
|
|
|
#define GI_ALIGN(n, align) (((n) + (align) - 1) & ~((align) - 1))
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
static gboolean
|
2010-07-26 22:26:46 +02:00
|
|
|
compute_struct_field_offsets (GIrTypelibBuild *build,
|
|
|
|
GIrNode *node,
|
2008-11-11 16:25:51 +01:00
|
|
|
GList *members,
|
2008-11-11 06:10:36 +01:00
|
|
|
gint *size_out,
|
|
|
|
gint *alignment_out)
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
int alignment = 1;
|
|
|
|
GList *l;
|
|
|
|
gboolean have_error = FALSE;
|
|
|
|
|
2008-11-11 14:45:13 +01:00
|
|
|
*alignment_out = -2; /* mark to detect recursion */
|
|
|
|
|
2008-11-11 06:10:36 +01:00
|
|
|
for (l = members; l; l = l->next)
|
|
|
|
{
|
|
|
|
GIrNode *member = (GIrNode *)l->data;
|
|
|
|
|
|
|
|
if (member->type == G_IR_NODE_FIELD)
|
|
|
|
{
|
|
|
|
GIrNodeField *field = (GIrNodeField *)member;
|
|
|
|
|
|
|
|
if (!have_error)
|
|
|
|
{
|
|
|
|
int member_size;
|
|
|
|
int member_alignment;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
if (get_field_size_alignment (build, field, node,
|
2008-11-11 06:10:36 +01:00
|
|
|
&member_size, &member_alignment))
|
|
|
|
{
|
2011-06-21 13:44:56 +02:00
|
|
|
size = GI_ALIGN (size, member_alignment);
|
2008-11-11 06:10:36 +01:00
|
|
|
alignment = MAX (alignment, member_alignment);
|
|
|
|
field->offset = size;
|
|
|
|
size += member_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
have_error = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_error)
|
|
|
|
field->offset = -1;
|
|
|
|
}
|
|
|
|
else if (member->type == G_IR_NODE_CALLBACK)
|
|
|
|
{
|
2011-06-21 13:44:56 +02:00
|
|
|
size = GI_ALIGN (size, ffi_type_pointer.alignment);
|
2009-02-02 17:31:06 +01:00
|
|
|
alignment = MAX (alignment, ffi_type_pointer.alignment);
|
|
|
|
size += ffi_type_pointer.size;
|
2008-11-11 06:10:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Structs are tail-padded out to a multiple of their alignment */
|
2011-06-21 13:44:56 +02:00
|
|
|
size = GI_ALIGN (size, alignment);
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
if (!have_error)
|
|
|
|
{
|
|
|
|
*size_out = size;
|
|
|
|
*alignment_out = alignment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*size_out = -1;
|
|
|
|
*alignment_out = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !have_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2010-07-26 22:26:46 +02:00
|
|
|
compute_union_field_offsets (GIrTypelibBuild *build,
|
|
|
|
GIrNode *node,
|
2008-11-11 16:25:51 +01:00
|
|
|
GList *members,
|
2008-11-11 06:10:36 +01:00
|
|
|
gint *size_out,
|
|
|
|
gint *alignment_out)
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
int alignment = 1;
|
|
|
|
GList *l;
|
|
|
|
gboolean have_error = FALSE;
|
|
|
|
|
2008-11-11 14:45:13 +01:00
|
|
|
*alignment_out = -2; /* mark to detect recursion */
|
|
|
|
|
2008-11-11 06:10:36 +01:00
|
|
|
for (l = members; l; l = l->next)
|
|
|
|
{
|
|
|
|
GIrNode *member = (GIrNode *)l->data;
|
|
|
|
|
|
|
|
if (member->type == G_IR_NODE_FIELD)
|
|
|
|
{
|
|
|
|
GIrNodeField *field = (GIrNodeField *)member;
|
|
|
|
|
|
|
|
if (!have_error)
|
|
|
|
{
|
|
|
|
int member_size;
|
|
|
|
int member_alignment;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
if (get_field_size_alignment (build,field, node,
|
2008-11-11 06:10:36 +01:00
|
|
|
&member_size, &member_alignment))
|
|
|
|
{
|
|
|
|
size = MAX (size, member_size);
|
|
|
|
alignment = MAX (alignment, member_alignment);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
have_error = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unions are tail-padded out to a multiple of their alignment */
|
2011-06-21 13:44:56 +02:00
|
|
|
size = GI_ALIGN (size, alignment);
|
2008-11-11 06:10:36 +01:00
|
|
|
|
|
|
|
if (!have_error)
|
|
|
|
{
|
|
|
|
*size_out = size;
|
|
|
|
*alignment_out = alignment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*size_out = -1;
|
|
|
|
*alignment_out = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !have_error;
|
|
|
|
}
|
|
|
|
|
2008-11-11 14:45:13 +01:00
|
|
|
static gboolean
|
2010-07-26 22:26:46 +02:00
|
|
|
check_needs_computation (GIrTypelibBuild *build,
|
|
|
|
GIrNode *node,
|
2008-11-11 14:45:13 +01:00
|
|
|
gint alignment)
|
|
|
|
{
|
2010-07-26 22:26:46 +02:00
|
|
|
GIrModule *module = build->module;
|
2008-11-11 14:45:13 +01:00
|
|
|
/*
|
|
|
|
* 0: Not yet computed
|
|
|
|
* >0: Previously succeeded
|
|
|
|
* -1: Previously failed
|
|
|
|
* -2: In progress
|
|
|
|
*/
|
|
|
|
if (alignment == -2)
|
|
|
|
{
|
|
|
|
g_warning ("Recursion encountered when computing the size of %s.%s",
|
|
|
|
module->name, node->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return alignment == 0;
|
|
|
|
}
|
|
|
|
|
2008-11-11 06:10:36 +01:00
|
|
|
/**
|
2010-11-16 21:08:33 +01:00
|
|
|
* _g_ir_node_compute_offsets:
|
2010-07-26 22:26:46 +02:00
|
|
|
* @build: Current typelib build
|
2008-11-11 06:10:36 +01:00
|
|
|
* @node: a #GIrNode
|
|
|
|
*
|
|
|
|
* If a node is a a structure or union, makes sure that the field
|
|
|
|
* offsets have been computed, and also computes the overall size and
|
|
|
|
* alignment for the type.
|
|
|
|
*/
|
|
|
|
void
|
2010-11-16 21:08:33 +01:00
|
|
|
_g_ir_node_compute_offsets (GIrTypelibBuild *build,
|
|
|
|
GIrNode *node)
|
2008-11-11 06:10:36 +01:00
|
|
|
{
|
2010-07-26 22:26:46 +02:00
|
|
|
gboolean appended_stack;
|
|
|
|
|
|
|
|
if (build->stack)
|
|
|
|
appended_stack = node != (GIrNode*)build->stack->data;
|
|
|
|
else
|
|
|
|
appended_stack = TRUE;
|
|
|
|
if (appended_stack)
|
|
|
|
build->stack = g_list_prepend (build->stack, node);
|
|
|
|
|
2008-11-11 06:10:36 +01:00
|
|
|
switch (node->type)
|
|
|
|
{
|
|
|
|
case G_IR_NODE_BOXED:
|
|
|
|
{
|
|
|
|
GIrNodeBoxed *boxed = (GIrNodeBoxed *)node;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
if (!check_needs_computation (build, node, boxed->alignment))
|
2008-11-11 06:10:36 +01:00
|
|
|
return;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
compute_struct_field_offsets (build, node, boxed->members,
|
2008-11-11 06:10:36 +01:00
|
|
|
&boxed->size, &boxed->alignment);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case G_IR_NODE_STRUCT:
|
|
|
|
{
|
|
|
|
GIrNodeStruct *struct_ = (GIrNodeStruct *)node;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
if (!check_needs_computation (build, node, struct_->alignment))
|
2008-11-11 06:10:36 +01:00
|
|
|
return;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
compute_struct_field_offsets (build, node, struct_->members,
|
2008-11-11 06:10:36 +01:00
|
|
|
&struct_->size, &struct_->alignment);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-11 22:10:12 +01:00
|
|
|
case G_IR_NODE_OBJECT:
|
|
|
|
case G_IR_NODE_INTERFACE:
|
|
|
|
{
|
|
|
|
GIrNodeInterface *iface = (GIrNodeInterface *)node;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
if (!check_needs_computation (build, node, iface->alignment))
|
2008-11-11 22:10:12 +01:00
|
|
|
return;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
compute_struct_field_offsets (build, node, iface->members,
|
2008-11-11 22:10:12 +01:00
|
|
|
&iface->size, &iface->alignment);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
case G_IR_NODE_UNION:
|
|
|
|
{
|
|
|
|
GIrNodeUnion *union_ = (GIrNodeUnion *)node;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
if (!check_needs_computation (build, node, union_->alignment))
|
2008-11-11 06:10:36 +01:00
|
|
|
return;
|
|
|
|
|
2010-07-26 22:26:46 +02:00
|
|
|
compute_union_field_offsets (build, (GIrNode*)union_, union_->members,
|
2008-11-11 06:10:36 +01:00
|
|
|
&union_->size, &union_->alignment);
|
|
|
|
break;
|
|
|
|
}
|
2008-11-18 13:29:10 +01:00
|
|
|
case G_IR_NODE_ENUM:
|
|
|
|
case G_IR_NODE_FLAGS:
|
|
|
|
{
|
|
|
|
GIrNodeEnum *enum_ = (GIrNodeEnum *)node;
|
|
|
|
|
|
|
|
if (enum_->storage_type != GI_TYPE_TAG_VOID) /* already done */
|
|
|
|
return;
|
|
|
|
|
|
|
|
compute_enum_storage_type (enum_);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2008-11-11 06:10:36 +01:00
|
|
|
default:
|
2010-07-26 22:26:46 +02:00
|
|
|
break;
|
2008-11-11 06:10:36 +01:00
|
|
|
}
|
2010-07-26 22:26:46 +02:00
|
|
|
|
|
|
|
if (appended_stack)
|
|
|
|
build->stack = g_list_delete_link (build->stack, build->stack);
|
2008-11-11 06:10:36 +01:00
|
|
|
}
|