#include <glib.h>
static
-struct type_enum *_enum_type_new(struct type_class *type_class,
- struct declaration_scope *parent_scope);
+struct definition *_enum_definition_new(struct declaration *declaration,
+ struct definition_scope *parent_scope,
+ GQuark field_name, int index);
static
-void _enum_type_free(struct type *type);
+void _enum_definition_free(struct definition *definition);
static
void enum_range_set_free(void *ptr)
* Returns a GArray or NULL.
* Caller must release the GArray with g_array_unref().
*/
-GArray *enum_uint_to_quark_set(const struct type_class_enum *enum_class,
+GArray *enum_uint_to_quark_set(const struct declaration_enum *enum_declaration,
uint64_t v)
{
struct enum_range_to_quark *iter;
GArray *qs, *ranges = NULL;
/* Single values lookup */
- qs = g_hash_table_lookup(enum_class->table.value_to_quark_set, &v);
+ qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
/* Range lookup */
- cds_list_for_each_entry(iter, &enum_class->table.range_to_quark, node) {
+ cds_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) {
if (iter->range.start._unsigned > v || iter->range.end._unsigned < v)
continue;
if (!ranges) {
* Returns a GArray or NULL.
* Caller must release the GArray with g_array_unref().
*/
-GArray *enum_int_to_quark_set(const struct type_class_enum *enum_class, uint64_t v)
+GArray *enum_int_to_quark_set(const struct declaration_enum *enum_declaration,
+ uint64_t v)
{
struct enum_range_to_quark *iter;
GArray *qs, *ranges = NULL;
/* Single values lookup */
- qs = g_hash_table_lookup(enum_class->table.value_to_quark_set, &v);
+ qs = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
/* Range lookup */
- cds_list_for_each_entry(iter, &enum_class->table.range_to_quark, node) {
+ cds_list_for_each_entry(iter, &enum_declaration->table.range_to_quark, node) {
if (iter->range.start._signed > v || iter->range.end._signed < v)
continue;
if (!ranges) {
}
static
-void enum_signed_insert_value_to_quark_set(struct type_class_enum *enum_class,
+void enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
int64_t v, GQuark q)
{
int64_t *valuep;
GArray *array;
- array = g_hash_table_lookup(enum_class->table.value_to_quark_set, &v);
+ array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
if (!array) {
array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
g_array_set_size(array, 1);
g_array_index(array, GQuark, array->len - 1) = q;
valuep = g_new(int64_t, 1);
*valuep = v;
- g_hash_table_insert(enum_class->table.value_to_quark_set, valuep, array);
+ g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array);
} else {
g_array_set_size(array, array->len + 1);
g_array_index(array, GQuark, array->len - 1) = q;
}
static
-void enum_unsigned_insert_value_to_quark_set(struct type_class_enum *enum_class,
+void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
uint64_t v, GQuark q)
{
uint64_t *valuep;
GArray *array;
- array = g_hash_table_lookup(enum_class->table.value_to_quark_set, &v);
+ array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set, &v);
if (!array) {
array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
g_array_set_size(array, 1);
g_array_index(array, GQuark, array->len - 1) = q;
valuep = g_new(uint64_t, 1);
*valuep = v;
- g_hash_table_insert(enum_class->table.value_to_quark_set, valuep, array);
+ g_hash_table_insert(enum_declaration->table.value_to_quark_set, valuep, array);
} else {
g_array_set_size(array, array->len + 1);
g_array_index(array, GQuark, array->len - 1) = q;
}
static
-void enum_signed_insert_value_to_quark_set(struct type_class_enum *enum_class,
+void enum_signed_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
int64_t v, GQuark q)
{
GArray *array;
- array = g_hash_table_lookup(enum_class->table.value_to_quark_set,
+ array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
(gconstpointer) v);
if (!array) {
array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
g_array_set_size(array, 1);
g_array_index(array, GQuark, array->len - 1) = q;
- g_hash_table_insert(enum_class->table.value_to_quark_set,
+ g_hash_table_insert(enum_declaration->table.value_to_quark_set,
(gpointer) v, array);
} else {
g_array_set_size(array, array->len + 1);
}
static
-void enum_unsigned_insert_value_to_quark_set(struct type_class_enum *enum_class,
+void enum_unsigned_insert_value_to_quark_set(struct declaration_enum *enum_declaration,
uint64_t v, GQuark q)
{
GArray *array;
- array = g_hash_table_lookup(enum_class->table.value_to_quark_set,
+ array = g_hash_table_lookup(enum_declaration->table.value_to_quark_set,
(gconstpointer) v);
if (!array) {
array = g_array_sized_new(FALSE, TRUE, sizeof(GQuark), 1);
g_array_set_size(array, 1);
g_array_index(array, GQuark, array->len - 1) = q;
- g_hash_table_insert(enum_class->table.value_to_quark_set,
+ g_hash_table_insert(enum_declaration->table.value_to_quark_set,
(gpointer) v, array);
} else {
g_array_set_size(array, array->len + 1);
}
#endif /* __WORDSIZE != 32 */
-GArray *enum_quark_to_range_set(const struct type_class_enum *enum_class,
+GArray *enum_quark_to_range_set(const struct declaration_enum *enum_declaration,
GQuark q)
{
- return g_hash_table_lookup(enum_class->table.quark_to_range_set,
+ return g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
(gconstpointer) (unsigned long) q);
}
static
-void enum_signed_insert_range_to_quark(struct type_class_enum *enum_class,
+void enum_signed_insert_range_to_quark(struct declaration_enum *enum_declaration,
int64_t start, int64_t end, GQuark q)
{
struct enum_range_to_quark *rtoq;
rtoq = g_new(struct enum_range_to_quark, 1);
- cds_list_add(&rtoq->node, &enum_class->table.range_to_quark);
+ cds_list_add(&rtoq->node, &enum_declaration->table.range_to_quark);
rtoq->range.start._signed = start;
rtoq->range.end._signed = end;
rtoq->quark = q;
}
static
-void enum_unsigned_insert_range_to_quark(struct type_class_enum *enum_class,
+void enum_unsigned_insert_range_to_quark(struct declaration_enum *enum_declaration,
uint64_t start, uint64_t end, GQuark q)
{
struct enum_range_to_quark *rtoq;
rtoq = g_new(struct enum_range_to_quark, 1);
- cds_list_add(&rtoq->node, &enum_class->table.range_to_quark);
+ cds_list_add(&rtoq->node, &enum_declaration->table.range_to_quark);
rtoq->range.start._unsigned = start;
rtoq->range.end._unsigned = end;
rtoq->quark = q;
}
-void enum_signed_insert(struct type_class_enum *enum_class,
+void enum_signed_insert(struct declaration_enum *enum_declaration,
int64_t start, int64_t end, GQuark q)
{
GArray *array;
struct enum_range *range;
if (start == end) {
- enum_signed_insert_value_to_quark_set(enum_class, start, q);
+ enum_signed_insert_value_to_quark_set(enum_declaration, start, q);
} else {
if (start > end) {
uint64_t tmp;
start = end;
end = tmp;
}
- enum_signed_insert_range_to_quark(enum_class, start, end, q);
+ enum_signed_insert_range_to_quark(enum_declaration, start, end, q);
}
- array = g_hash_table_lookup(enum_class->table.quark_to_range_set,
+ array = g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
(gconstpointer) (unsigned long) q);
if (!array) {
array = g_array_sized_new(FALSE, TRUE,
sizeof(struct enum_range), 1);
- g_hash_table_insert(enum_class->table.quark_to_range_set,
+ g_hash_table_insert(enum_declaration->table.quark_to_range_set,
(gpointer) (unsigned long) q,
array);
}
range->end._signed = end;
}
-void enum_unsigned_insert(struct type_class_enum *enum_class,
+void enum_unsigned_insert(struct declaration_enum *enum_declaration,
uint64_t start, uint64_t end, GQuark q)
{
GArray *array;
if (start == end) {
- enum_unsigned_insert_value_to_quark_set(enum_class, start, q);
+ enum_unsigned_insert_value_to_quark_set(enum_declaration, start, q);
} else {
if (start > end) {
uint64_t tmp;
start = end;
end = tmp;
}
- enum_unsigned_insert_range_to_quark(enum_class, start, end, q);
+ enum_unsigned_insert_range_to_quark(enum_declaration, start, end, q);
}
- array = g_hash_table_lookup(enum_class->table.quark_to_range_set,
+ array = g_hash_table_lookup(enum_declaration->table.quark_to_range_set,
(gconstpointer) (unsigned long) q);
if (!array) {
array = g_array_sized_new(FALSE, TRUE,
sizeof(struct enum_range), 1);
- g_hash_table_insert(enum_class->table.quark_to_range_set,
+ g_hash_table_insert(enum_declaration->table.quark_to_range_set,
(gpointer) (unsigned long) q,
array);
}
range->end._unsigned = end;
}
-size_t enum_get_nr_enumerators(struct type_class_enum *enum_class)
+size_t enum_get_nr_enumerators(struct declaration_enum *enum_declaration)
{
- return g_hash_table_size(enum_class->table.quark_to_range_set);
+ return g_hash_table_size(enum_declaration->table.quark_to_range_set);
}
void enum_copy(struct stream_pos *dest, const struct format *fdest,
struct stream_pos *src, const struct format *fsrc,
- struct type *type)
+ struct definition *definition)
{
- struct type_enum *_enum = container_of(type, struct type_enum, p);
- struct type_class_enum *enum_class = _enum->_class;
+ struct definition_enum *_enum =
+ container_of(definition, struct definition_enum, p);
+ struct declaration_enum *enum_declaration= _enum->declaration;
GArray *array;
GQuark v;
- array = fsrc->enum_read(src, enum_class);
+ array = fsrc->enum_read(src, enum_declaration);
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.
- * TODO: use direct underlying type read/write intead. Not doing it for
+ * TODO: use direct underlying declaration read/write intead. Not doing it for
* now to test enum read and write code.
*/
v = g_array_index(array, GQuark, 0);
- return fdest->enum_write(dest, enum_class, v);
+ return fdest->enum_write(dest, enum_declaration, v);
}
static
-void _enum_type_class_free(struct type_class *type_class)
+void _enum_declaration_free(struct declaration *declaration)
{
- struct type_class_enum *enum_class =
- container_of(type_class, struct enum_type_class, p);
+ struct declaration_enum *enum_declaration =
+ container_of(declaration, struct declaration_enum, p);
struct enum_range_to_quark *iter, *tmp;
- g_hash_table_destroy(enum_class->table.value_to_quark_set);
- cds_list_for_each_entry_safe(iter, tmp, &enum_class->table.range_to_quark, node) {
+ g_hash_table_destroy(enum_declaration->table.value_to_quark_set);
+ cds_list_for_each_entry_safe(iter, tmp, &enum_declaration->table.range_to_quark, node) {
cds_list_del(&iter->node);
g_free(iter);
}
- g_hash_table_destroy(enum_class->table.quark_to_range_set);
- g_free(enum_class);
+ g_hash_table_destroy(enum_declaration->table.quark_to_range_set);
+ declaration_unref(&enum_declaration->integer_declaration->p);
+ g_free(enum_declaration);
}
-struct type_class_enum *
-_enum_type_class_new(const char *name, size_t len, int byte_order,
- int signedness, size_t alignment)
+struct declaration_enum *
+ _enum_declaration_new(const char *name, struct declaration_integer *integer_declaration)
{
- struct type_class_enum *enum_class;
- struct type_class_integer *int_class;
- int ret;
+ struct declaration_enum *enum_declaration;
- enum_class = g_new(struct type_class_enum, 1);
- enum_class->table.value_to_quark_set = g_hash_table_new_full(enum_val_hash,
+ enum_declaration = g_new(struct declaration_enum, 1);
+
+ enum_declaration->table.value_to_quark_set = g_hash_table_new_full(enum_val_hash,
enum_val_equal,
enum_val_free,
enum_range_set_free);
- CDS_INIT_LIST_HEAD(&enum_class->table.range_to_quark);
- enum_class->table.quark_to_range_set = g_hash_table_new_full(g_int_hash,
+ CDS_INIT_LIST_HEAD(&enum_declaration->table.range_to_quark);
+ enum_declaration->table.quark_to_range_set = g_hash_table_new_full(g_int_hash,
g_int_equal,
NULL, enum_range_set_free);
- int_class = &enum_class->p;
- int_class->p.name = g_quark_from_string(name);
- int_class->p.alignment = alignment;
- int_class->p.copy = enum_copy;
- int_class->p.class_free = _enum_type_class_free;
- int_class->p.type_new = _enum_type_new;
- int_class->p.type_free = _enum_type_free;
- int_class->p.ref = 1;
- int_class->len = len;
- int_class->byte_order = byte_order;
- int_class->signedness = signedness;
- if (int_class->p.name) {
- ret = register_type(&int_class->p);
- if (ret)
- goto register_error;
- }
- return enum_class;
-
-register_error:
- {
- struct enum_range_to_quark *iter, *tmp;
-
- cds_list_for_each_entry_safe(iter, tmp, &enum_class->table.range_to_quark, node) {
- cds_list_del(&iter->node);
- g_free(iter);
- }
- }
- g_hash_table_destroy(enum_class->table.value_to_quark_set);
- g_hash_table_destroy(enum_class->table.quark_to_range_set);
- g_free(enum_class);
- return NULL;
+ declaration_ref(&integer_declaration->p);
+ enum_declaration->integer_declaration = integer_declaration;
+ enum_declaration->p.id = CTF_TYPE_ENUM;
+ enum_declaration->p.name = g_quark_from_string(name);
+ enum_declaration->p.alignment = 1;
+ enum_declaration->p.copy = enum_copy;
+ enum_declaration->p.declaration_free = _enum_declaration_free;
+ enum_declaration->p.definition_new = _enum_definition_new;
+ enum_declaration->p.definition_free = _enum_definition_free;
+ enum_declaration->p.ref = 1;
+ return enum_declaration;
}
static
-struct type_enum *_enum_type_new(struct type_class *type_class,
- struct declaration_scope *parent_scope)
+struct definition *
+ _enum_definition_new(struct declaration *declaration,
+ struct definition_scope *parent_scope,
+ GQuark field_name, int index)
{
- struct type_class_enum *enum_class =
- container_of(type_class, struct type_class_enum, p);
- struct type_enum *_enum;
-
- _enum = g_new(struct type_enum, 1);
- type_class_ref(&enum_class->p);
- _enum->p._class = enum_class;
+ struct declaration_enum *enum_declaration =
+ container_of(declaration, struct declaration_enum, p);
+ struct definition_enum *_enum;
+ struct definition *definition_integer_parent;
+
+ _enum = g_new(struct definition_enum, 1);
+ declaration_ref(&enum_declaration->p);
+ _enum->p.declaration = declaration;
+ _enum->declaration = enum_declaration;
_enum->p.ref = 1;
+ _enum->p.index = index;
_enum->value = NULL;
+ definition_integer_parent =
+ enum_declaration->integer_declaration->p.definition_new(&enum_declaration->integer_declaration->p,
+ parent_scope, field_name, 0);
+ _enum->integer = container_of(definition_integer_parent,
+ struct definition_integer, p);
return &_enum->p;
}
static
-void _enum_type_free(struct type *type)
+void _enum_definition_free(struct definition *definition)
{
- struct type_enum *_enum = container_of(type, struct type_enum, p);
+ struct definition_enum *_enum =
+ container_of(definition, struct definition_enum, p);
- type_class_unref(_enum->p._class);
+ definition_unref(&_enum->integer->p);
+ declaration_unref(_enum->p.declaration);
if (_enum->value)
- g_array_unref(_enum->value, TRUE);
+ g_array_unref(_enum->value);
g_free(_enum);
}