integer.c \
sequence.c \
string.c \
- struct.c
+ struct.c \
+ variant.c
};
struct field {
- struct declaration *type;
+ struct declaration *declaration;
};
struct type_struct {
struct declaration p;
struct type_variant *type;
struct declaration_scope *scope;
- struct declaration *tag;
+ struct declaration *enum_tag;
GArray *fields; /* Array of struct field */
struct field *current_field; /* Last field read */
};
sequence.c \
string.c \
struct.c \
+ variant.c \
types.c
fdest->array_begin(dest, array_type);
for (i = 0; i < array_type->len; i++) {
- struct type *elem_type = array->current_element.type;
- elem_type->p.type->copy(dest, fdest, src, fsrc, elem_type);
+ struct declaration *elem = array->current_element.declaration;
+ elem->type->copy(dest, fdest, src, fsrc, elem);
}
fsrc->array_end(src, array_type);
fdest->array_end(dest, array_type);
{
struct type_array *array_type;
struct type *type;
- int ret;
array_type = g_new(struct type_array, 1);
type = &array_type->p;
type->declaration_new = _array_declaration_new;
type->declaration_free = _array_declaration_free;
type->ref = 1;
-
- if (type->name) {
- ret = register_type(type);
- if (ret)
- goto error_register;
- }
return array_type;
-
-error_register:
- type_unref(array_type->elem);
- g_free(array_type);
- return NULL;
}
static
array = g_new(struct declaration_array, 1);
type_ref(&array_type->p);
- array->p.type = array_type;
+ array->p.type = type;
+ array->type = array_type;
array->p.ref = 1;
array->scope = new_declaration_scope(parent_scope);
array->current_element.declaration =
- array_type->elem.p->declaration_new(&array_type->elem.p,
- parent_scope);
+ array_type->elem->declaration_new(array_type->elem,
+ parent_scope);
return &array->p;
}
static
void _array_declaration_free(struct declaration *declaration)
{
- struct type_array *array =
- container_of(declaration, struct type_array, p);
+ struct declaration_array *array =
+ container_of(declaration, struct declaration_array, p);
struct declaration *elem_declaration =
array->current_element.declaration;
- elem_type->p.type->declaration_free(elem_type);
+ elem_declaration->type->declaration_free(elem_declaration);
free_declaration_scope(array->scope);
type_unref(array->p.type);
g_free(array);
assert(array);
/* unref previous array */
if (_enum->value)
- g_array_unref(_enum->value, TRUE);
+ g_array_unref(_enum->value);
_enum->value = array;
/*
* Arbitrarily choose the first one.
void _enum_type_free(struct type *type)
{
struct type_enum *enum_type =
- container_of(type, struct enum_type, p);
+ container_of(type, struct type_enum, p);
struct enum_range_to_quark *iter, *tmp;
g_hash_table_destroy(enum_type->table.value_to_quark_set);
g_free(iter);
}
g_hash_table_destroy(enum_type->table.quark_to_range_set);
- type_unref(enum_type->integer_type);
+ type_unref(&enum_type->integer_type->p);
g_free(enum_type);
}
_enum_type_new(const char *name, struct type_integer *integer_type)
{
struct type_enum *enum_type;
- int ret;
enum_type = g_new(struct type_enum, 1);
enum_type->table.quark_to_range_set = g_hash_table_new_full(g_int_hash,
g_int_equal,
NULL, enum_range_set_free);
- type_ref(integer_type);
+ type_ref(&integer_type->p);
enum_type->integer_type = integer_type;
enum_type->p.name = g_quark_from_string(name);
enum_type->p.alignment = 1;
enum_type->p.declaration_new = _enum_declaration_new;
enum_type->p.declaration_free = _enum_declaration_free;
enum_type->p.ref = 1;
- if (enum_type->p.name) {
- ret = register_type(&enum_type->p);
- if (ret)
- goto register_error;
- }
return enum_type;
-
-register_error:
- g_hash_table_destroy(enum_type->table.value_to_quark_set);
- g_hash_table_destroy(enum_type->table.quark_to_range_set);
- type_unref(enum_type->integer_type);
- g_free(enum_type);
- return NULL;
}
static
struct type_enum *enum_type =
container_of(type, struct type_enum, p);
struct declaration_enum *_enum;
- struct declaration_integer_parent *declaration_integer_parent;
+ struct declaration *declaration_integer_parent;
_enum = g_new(struct declaration_enum, 1);
type_ref(&enum_type->p);
- _enum->p.type = enum_type;
+ _enum->p.type = type;
+ _enum->type = enum_type;
_enum->p.ref = 1;
_enum->value = NULL;
declaration_integer_parent =
struct declaration_enum *_enum =
container_of(declaration, struct declaration_enum, p);
- declaration_unref(_enum->integer);
+ declaration_unref(&_enum->integer->p);
type_unref(_enum->p.type);
if (_enum->value)
- g_array_unref(_enum->value, TRUE);
+ g_array_unref(_enum->value);
g_free(_enum);
}
{
struct type_float *float_type;
struct type *type;
- int ret;
float_type = g_new(struct type_float, 1);
type = &float_type->p;
float_type->sign = integer_type_new(NULL, 1,
byte_order, false, 1);
- if (!float_type->mantissa)
- goto error_sign;
float_type->mantissa = integer_type_new(NULL, mantissa_len - 1,
byte_order, false, 1);
- if (!float_type->mantissa)
- goto error_mantissa;
float_type->exp = integer_type_new(NULL, exp_len,
byte_order, true, 1);
- if (!float_type->exp)
- goto error_exp;
-
- if (float_type->p.name) {
- ret = register_type(&float_type->p);
- if (ret)
- goto error_register;
- }
return float_type;
-
-error_register:
- type_unref(&float_type->exp->p);
-error_exp:
- type_unref(&float_type->mantissa->p);
-error_mantissa:
- type_unref(&float_type->sign->p);
-error_sign:
- g_free(float_type);
- return NULL;
}
static
struct declaration_float *_float;
_float = g_new(struct declaration_float, 1);
- type_ref(&_float_type->p);
- _float->p.type= _float_type;
+ type_ref(&float_type->p);
+ _float->p.type= type;
+ _float->type= float_type;
_float->p.ref = 1;
_float->value = 0.0;
return &_float->p;
struct declaration *declaration)
{
struct declaration_integer *integer =
- container_of(type, struct declaration_integer, p);
+ container_of(declaration, struct declaration_integer, p);
struct type_integer *integer_type = integer->type;
if (!integer_type->signedness) {
int signedness, size_t alignment)
{
struct type_integer *integer_type;
- int ret;
integer_type = g_new(struct type_integer, 1);
integer_type->p.name = g_quark_from_string(name);
integer_type->len = len;
integer_type->byte_order = byte_order;
integer_type->signedness = signedness;
- if (integer_type->p.name) {
- ret = register_type(&integer_type->p);
- if (ret) {
- g_free(integer_type);
- return NULL;
- }
- }
return integer_type;
}
integer = g_new(struct declaration_integer, 1);
type_ref(&integer_type->p);
- integer->p.type = integer_type;
+ integer->p.type = type;
+ integer->type = integer_type;
integer->p.ref = 1;
integer->value._unsigned = 0;
return &integer->p;
&sequence->len->p);
for (i = 0; i < sequence->len->value._unsigned; i++) {
- struct declaration *elem_type =
+ struct declaration *elem =
sequence->current_element.declaration;
- elem_type->p.type->copy(dest, fdest, src, fsrc, elem_type);
+ elem->type->copy(dest, fdest, src, fsrc, elem);
}
fsrc->sequence_end(src, sequence_type);
fdest->sequence_end(dest, sequence_type);
{
struct type_sequence *sequence_type;
struct type *type;
- int ret;
sequence_type = g_new(struct type_sequence, 1);
type = &sequence_type->p;
type->declaration_new = _sequence_declaration_new;
type->declaration_free = _sequence_declaration_free;
type->ref = 1;
-
- if (type->name) {
- ret = register_type(type);
- if (ret)
- goto error_register;
- }
return sequence_type;
-
-error_register:
- type_unref(&len_type->p);
- type_unref(elem_type);
- g_free(sequence_type);
- return NULL;
}
static
sequence = g_new(struct declaration_sequence, 1);
type_ref(&sequence_type->p);
- sequence->p.type = sequence_type;
+ sequence->p.type = type;
+ sequence->type = sequence_type;
sequence->p.ref = 1;
sequence->scope = new_declaration_scope(parent_scope);
- len_parent =
- sequence_type->len_type.p->type_new(&sequence_type->len_type.p,
- parent_scope);
+ len_parent = sequence_type->len_type->p.declaration_new(&sequence_type->len_type->p,
+ parent_scope);
sequence->len =
container_of(len_parent, struct declaration_integer, p);
sequence->current_element.declaration =
- sequence_type->elem.p->type_new(&sequence_type->elem.p,
- parent_scope);
+ sequence_type->elem->declaration_new(sequence_type->elem,
+ parent_scope);
return &sequence->p;
}
static
void _sequence_declaration_free(struct declaration *declaration)
{
- struct type_sequence *sequence =
- container_of(declaration, struct type_sequence, p);
- struct declaration *len_declaration = sequence->len;
+ struct declaration_sequence *sequence =
+ container_of(declaration, struct declaration_sequence, p);
+ struct declaration *len_declaration = &sequence->len->p;
struct declaration *elem_declaration =
sequence->current_element.declaration;
- len_declaration->p.type->declaration_free(len_declaration);
- elem_declaration->p.type->declaration_free(elem_declaration);
+ len_declaration->type->declaration_free(len_declaration);
+ elem_declaration->type->declaration_free(elem_declaration);
free_declaration_scope(sequence->scope);
type_unref(sequence->p.type);
g_free(sequence);
struct type_string *string_type_new(const char *name)
{
struct type_string *string_type;
- int ret;
string_type = g_new(struct type_string, 1);
string_type->p.name = g_quark_from_string(name);
string_type->p.copy = string_copy;
string_type->p.type_free = _string_type_free;
string_type->p.declaration_new = _string_declaration_new;
- string_type->p.declaration_free = _strin_declaration_free;
+ string_type->p.declaration_free = _string_declaration_free;
string_type->p.ref = 1;
- if (string_type->p.name) {
- ret = register_type(&string_type->p);
- if (ret) {
- g_free(string_type);
- return NULL;
- }
- }
return string_type;
}
string = g_new(struct declaration_string, 1);
type_ref(&string_type->p);
- string->p.type = string_type;
+ string->p.type = type;
+ string->type = string_type;
string->p.ref = 1;
string->value = NULL;
return &string->p;
for (i = 0; i < _struct->fields->len; i++) {
struct field *field = &g_array_index(_struct->fields,
struct field, i);
- struct type *field_type = field->type->p.type;
+ struct type *field_type = field->declaration->type;
- field_type->copy(dest, fdest, src, fsrc, &field->type->p);
+ field_type->copy(dest, fdest, src, fsrc, field->declaration);
}
fsrc->struct_end(src, struct_type);
g_hash_table_destroy(struct_type->fields_by_name);
for (i = 0; i < struct_type->fields->len; i++) {
- struct field *type_field =
+ struct type_field *type_field =
&g_array_index(struct_type->fields,
struct type_field, i);
- type_unref(field->type);
+ type_unref(type_field->type);
}
g_array_free(struct_type->fields, true);
g_free(struct_type);
{
struct type_struct *struct_type;
struct type *type;
- int ret;
struct_type = g_new(struct type_struct, 1);
type = &struct_type->p;
type->declaration_new = _struct_declaration_new;
type->declaration_free = _struct_declaration_free;
type->ref = 1;
-
- if (type->name) {
- ret = register_type(type);
- if (ret)
- goto error_register;
- }
return struct_type;
-
-error_register:
- g_hash_table_destroy(struct_type->fields_by_name);
- g_array_free(struct_type->fields, true);
- g_free(struct_type);
- return NULL;
}
static
_struct = g_new(struct declaration_struct, 1);
type_ref(&struct_type->p);
- _struct->p.type = struct_type;
+ _struct->p.type = type;
+ _struct->type = struct_type;
_struct->p.ref = 1;
_struct->scope = new_declaration_scope(parent_scope);
_struct->fields = g_array_sized_new(FALSE, TRUE,
static void free_declaration(struct declaration *declaration)
{
- declaration->p.declaration_free(declaration);
+ declaration->type->declaration_free(declaration);
}
int register_type(struct type *type, struct declaration_scope *scope)
{
+ if (!type->name)
+ return -EPERM;
+
/* Only lookup in local scope */
if (lookup_type_scope(type->name, scope))
return -EEXIST;
#include <babeltrace/compiler.h>
#include <babeltrace/format.h>
-
+#include <errno.h>
static
struct declaration *_variant_declaration_new(struct type *type,
struct type_variant *variant_type = variant->type;
struct field *field;
struct type *field_type;
- unsigned long i;
fsrc->variant_begin(src, variant_type);
fdest->variant_begin(dest, variant_type);
field = variant_get_current_field(variant);
- field_type = field->type->p.type;
- field_type->copy(dest, fdest, src, fsrc, &field->type->p);
+ field_type = field->declaration->type;
+ field_type->copy(dest, fdest, src, fsrc, field->declaration);
fsrc->variant_end(src, variant_type);
fdest->variant_end(dest, variant_type);
g_hash_table_destroy(variant_type->fields_by_tag);
for (i = 0; i < variant_type->fields->len; i++) {
- struct field *type_field =
+ struct type_field *type_field =
&g_array_index(variant_type->fields,
struct type_field, i);
- type_unref(field->type);
+ type_unref(type_field->type);
}
g_array_free(variant_type->fields, true);
g_free(variant_type);
{
struct type_variant *variant_type;
struct type *type;
- int ret;
variant_type = g_new(struct type_variant, 1);
type = &variant_type->p;
type->declaration_new = _variant_declaration_new;
type->declaration_free = _variant_declaration_free;
type->ref = 1;
-
- if (type->name) {
- ret = register_type(type);
- if (ret)
- goto error_register;
- }
return variant_type;
-
-error_register:
- g_hash_table_destroy(variant_type->fields_by_tag);
- g_array_free(variant_type->fields, true);
- g_free(variant_type);
- return NULL;
}
static
variant = g_new(struct declaration_variant, 1);
type_ref(&variant_type->p);
- variant->p.type = variant_type;
+ variant->p.type = type;
+ variant->type = variant_type;
variant->p.ref = 1;
variant->scope = new_declaration_scope(parent_scope);
variant->fields = g_array_sized_new(FALSE, TRUE,
}
static
-void variant_declaration_free(struct declaration *declaration)
+void _variant_declaration_free(struct declaration *declaration)
{
struct declaration_variant *variant =
container_of(declaration, struct declaration_variant, p);
type_ref(tag_type);
field->type = tag_type;
/* Keep index in hash rather than pointer, because array can relocate */
- g_hash_table_insert(variant_type->fields_by_name,
+ g_hash_table_insert(variant_type->fields_by_tag,
(gpointer) (unsigned long) field->name,
(gpointer) index);
/*
{
struct declaration_enum *_enum =
container_of(variant->enum_tag, struct declaration_enum, p);
- struct variant_type *variant_type = variant->type;
+ struct type_variant *variant_type = variant->type;
unsigned long index;
GArray *tag_array;
GQuark tag;