From 64893f33bdc4bfe20820928b28731277797e41fc Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 2 Mar 2011 11:50:08 -0500 Subject: [PATCH] Separate declaration scope from type scope Signed-off-by: Mathieu Desnoyers --- include/babeltrace/types.h | 39 ++++++++++++++++++++++++++------------ types/array.c | 5 ++++- types/sequence.c | 5 ++++- types/struct.c | 5 ++++- types/types.c | 36 +++++++++++++++++++++++++---------- types/variant.c | 5 ++++- 6 files changed, 69 insertions(+), 26 deletions(-) diff --git a/include/babeltrace/types.h b/include/babeltrace/types.h index 718d391a..cee6c5a3 100644 --- a/include/babeltrace/types.h +++ b/include/babeltrace/types.h @@ -87,10 +87,15 @@ char *get_pos_addr(struct stream_pos *pos) struct format; struct declaration; -/* Type declaration scope */ -struct declaration_scope { +/* type scope */ +struct type_scope { /* Hash table mapping type name GQuark to struct type */ GHashTable *types; + struct type_scope *parent_scope; +}; + +/* declaration scope */ +struct declaration_scope { /* Hash table mapping field name GQuark to struct declaration */ GHashTable *declarations; struct declaration_scope *parent_scope; @@ -238,6 +243,7 @@ struct field { struct type_struct { struct type p; GHashTable *fields_by_name; /* Tuples (field name, field index) */ + struct type_scope *scope; GArray *fields; /* Array of type_field */ }; @@ -251,6 +257,7 @@ struct declaration_struct { struct type_variant { struct type p; GHashTable *fields_by_tag; /* Tuples (field tag, field index) */ + struct type_scope *scope; GArray *fields; /* Array of type_field */ }; @@ -267,6 +274,7 @@ struct type_array { struct type p; size_t len; struct type *elem; + struct type_scope *scope; }; struct declaration_array { @@ -280,6 +288,7 @@ struct type_sequence { struct type p; struct type_integer *len_type; struct type *elem; + struct type_scope *scope; }; struct declaration_sequence { @@ -290,21 +299,23 @@ struct declaration_sequence { struct field current_element; /* struct field */ }; -struct type *lookup_type(GQuark type_name, struct declaration_scope *scope); -int register_type(struct type *type, struct declaration_scope *scope); +int register_type(GQuark type_name, struct type *type, + struct type_scope *scope); +struct type *lookup_type(GQuark type_name, struct type_scope *scope); +struct type_scope *new_type_scope(struct type_scope *parent_scope); +void free_type_scope(struct type_scope *scope); struct declaration * lookup_declaration(GQuark field_name, struct declaration_scope *scope); int register_declaration(GQuark field_name, struct declaration *declaration, struct declaration_scope *scope); - -void type_ref(struct type *type); -void type_unref(struct type *type); - struct declaration_scope * new_declaration_scope(struct declaration_scope *parent_scope); void free_declaration_scope(struct declaration_scope *scope); +void type_ref(struct type *type); +void type_unref(struct type *type); + void declaration_ref(struct declaration *declaration); void declaration_unref(struct declaration *declaration); @@ -355,7 +366,8 @@ size_t enum_get_nr_enumerators(struct type_enum *enum_type); struct type_enum *enum_type_new(const char *name, struct type_integer *integer_type); -struct type_struct *struct_type_new(const char *name); +struct type_struct *struct_type_new(const char *name, + struct type_scope *parent_scope); void struct_type_add_field(struct type_struct *struct_type, const char *field_name, struct type *field_type); /* @@ -378,7 +390,8 @@ struct_get_field_from_index(struct declaration_struct *struct_declaration, * from numeric values to a single tag. Overlapping tag value ranges are * therefore forbidden. */ -struct type_variant *variant_type_new(const char *name); +struct type_variant *variant_type_new(const char *name, + struct type_scope *parent_scope); void variant_type_add_field(struct type_variant *variant_type, const char *tag_name, struct type *tag_type); struct type_field * @@ -401,7 +414,8 @@ variant_get_current_field(struct declaration_variant *variant); * explicitly. "len" is the number of elements in the array. */ struct type_array *array_type_new(const char *name, - size_t len, struct type *elem_type); + size_t len, struct type *elem_type, + struct type_scope *parent_scope); /* * int_type and elem_type passed as parameter now belong to the sequence. No @@ -409,6 +423,7 @@ struct type_array *array_type_new(const char *name, */ struct type_sequence *sequence_type_new(const char *name, struct type_integer *len_type, - struct type *elem_type); + struct type *elem_type, + struct type_scope *parent_scope); #endif /* _BABELTRACE_TYPES_H */ diff --git a/types/array.c b/types/array.c index e6a6c218..35415ec8 100644 --- a/types/array.c +++ b/types/array.c @@ -51,12 +51,14 @@ void _array_type_free(struct type *type) struct type_array *array_type = container_of(type, struct type_array, p); + free_type_scope(array_type->scope); type_unref(array_type->elem); g_free(array_type); } struct type_array * - array_type_new(const char *name, size_t len, struct type *elem_type) + array_type_new(const char *name, size_t len, struct type *elem_type, + struct type_scope *parent_scope) { struct type_array *array_type; struct type *type; @@ -66,6 +68,7 @@ struct type_array * array_type->len = len; type_ref(elem_type); array_type->elem = elem_type; + array_type->scope = new_type_scope(parent_scope); type->name = g_quark_from_string(name); /* No need to align the array, the first element will align itself */ type->alignment = 1; diff --git a/types/sequence.c b/types/sequence.c index e769b81e..c379f983 100644 --- a/types/sequence.c +++ b/types/sequence.c @@ -59,6 +59,7 @@ void _sequence_type_free(struct type *type) struct type_sequence *sequence_type = container_of(type, struct type_sequence, p); + free_type_scope(sequence_type->scope); type_unref(&sequence_type->len_type->p); type_unref(sequence_type->elem); g_free(sequence_type); @@ -66,7 +67,8 @@ void _sequence_type_free(struct type *type) struct type_sequence * sequence_type_new(const char *name, struct type_integer *len_type, - struct type *elem_type) + struct type *elem_type, + struct type_scope *parent_scope) { struct type_sequence *sequence_type; struct type *type; @@ -78,6 +80,7 @@ struct type_sequence * sequence_type->len_type = len_type; type_ref(elem_type); sequence_type->elem = elem_type; + sequence_type->scope = new_type_scope(parent_scope); type->name = g_quark_from_string(name); type->alignment = max(len_type->p.alignment, elem_type->alignment); type->copy = sequence_copy; diff --git a/types/struct.c b/types/struct.c index baedbddb..5e7e2b4e 100644 --- a/types/struct.c +++ b/types/struct.c @@ -60,6 +60,7 @@ void _struct_type_free(struct type *type) container_of(type, struct type_struct, p); unsigned long i; + free_type_scope(struct_type->scope); g_hash_table_destroy(struct_type->fields_by_name); for (i = 0; i < struct_type->fields->len; i++) { @@ -72,7 +73,8 @@ void _struct_type_free(struct type *type) g_free(struct_type); } -struct type_struct *struct_type_new(const char *name) +struct type_struct *struct_type_new(const char *name, + struct type_scope *parent_scope) { struct type_struct *struct_type; struct type *type; @@ -84,6 +86,7 @@ struct type_struct *struct_type_new(const char *name) struct_type->fields = g_array_sized_new(FALSE, TRUE, sizeof(struct type_field), DEFAULT_NR_STRUCT_FIELDS); + struct_type->scope = new_type_scope(parent_scope); type->name = g_quark_from_string(name); type->alignment = 1; type->copy = struct_copy; diff --git a/types/types.c b/types/types.c index e001dd02..4b614f39 100644 --- a/types/types.c +++ b/types/types.c @@ -24,13 +24,13 @@ static struct type * - lookup_type_scope(GQuark type_name, struct declaration_scope *scope) + lookup_type_scope(GQuark type_name, struct type_scope *scope) { return g_hash_table_lookup(scope->types, (gconstpointer) (unsigned long) type_name); } -struct type *lookup_type(GQuark type_name, struct declaration_scope *scope) +struct type *lookup_type(GQuark type_name, struct type_scope *scope) { struct type *type; @@ -43,17 +43,19 @@ struct type *lookup_type(GQuark type_name, struct declaration_scope *scope) return NULL; } -int register_type(struct type *type, struct declaration_scope *scope) +int register_type(GQuark name, struct type *type, struct type_scope *scope) { - if (!type->name) + g_assert(name == type->name); + + if (!name) return -EPERM; /* Only lookup in local scope */ - if (lookup_type_scope(type->name, scope)) + if (lookup_type_scope(name, scope)) return -EEXIST; g_hash_table_insert(scope->types, - (gpointer) (unsigned long) type->name, + (gpointer) (unsigned long) name, type); type_ref(type); return 0; @@ -120,14 +122,29 @@ void declaration_unref(struct declaration *declaration) declaration->type->declaration_free(declaration); } -struct declaration_scope * - new_declaration_scope(struct declaration_scope *parent_scope) +struct type_scope * + new_type_scope(struct type_scope *parent_scope) { - struct declaration_scope *scope = g_new(struct declaration_scope, 1); + struct type_scope *scope = g_new(struct type_scope, 1); scope->types = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) type_unref); + scope->parent_scope = parent_scope; + return scope; +} + +void free_type_scope(struct type_scope *scope) +{ + g_hash_table_destroy(scope->types); + g_free(scope); +} + +struct declaration_scope * + new_declaration_scope(struct declaration_scope *parent_scope) +{ + struct declaration_scope *scope = g_new(struct declaration_scope, 1); + scope->declarations = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) declaration_unref); @@ -138,6 +155,5 @@ struct declaration_scope * void free_declaration_scope(struct declaration_scope *scope) { g_hash_table_destroy(scope->declarations); - g_hash_table_destroy(scope->types); g_free(scope); } diff --git a/types/variant.c b/types/variant.c index 3c2244be..98f68745 100644 --- a/types/variant.c +++ b/types/variant.c @@ -54,6 +54,7 @@ void _variant_type_free(struct type *type) container_of(type, struct type_variant, p); unsigned long i; + free_type_scope(variant_type->scope); g_hash_table_destroy(variant_type->fields_by_tag); for (i = 0; i < variant_type->fields->len; i++) { @@ -66,7 +67,8 @@ void _variant_type_free(struct type *type) g_free(variant_type); } -struct type_variant *variant_type_new(const char *name) +struct type_variant *variant_type_new(const char *name, + struct type_scope *parent_scope) { struct type_variant *variant_type; struct type *type; @@ -78,6 +80,7 @@ struct type_variant *variant_type_new(const char *name) variant_type->fields = g_array_sized_new(FALSE, TRUE, sizeof(struct type_field), DEFAULT_NR_STRUCT_FIELDS); + variant_type->scope = new_type_scope(parent_scope); type->name = g_quark_from_string(name); type->alignment = 1; type->copy = variant_copy; -- 2.34.1