Separate declaration scope from type scope
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 2 Mar 2011 16:50:08 +0000 (11:50 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 2 Mar 2011 16:50:08 +0000 (11:50 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/babeltrace/types.h
types/array.c
types/sequence.c
types/struct.c
types/types.c
types/variant.c

index 718d391a0dc9a90aa405da80cc263e8f2d421fd6..cee6c5a3c4ef2995e17d00d9459488225b5e5632 100644 (file)
@@ -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 */
index e6a6c218c9c6c4d025ba8dc115f10a95cf4f5465..35415ec8f446401b80b10eb0a8d7dbed1b7d1bab 100644 (file)
@@ -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;
index e769b81e4e5d1776ead3c748a8acf11d7c8cf5a9..c379f98364c139e30891e167ee9c2757c21e6224 100644 (file)
@@ -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;
index baedbddb5f4ba6a08b58201cc29da9673a3e7b27..5e7e2b4ef3ef740cd94fcfd396fadb05ea14a555 100644 (file)
@@ -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;
index e001dd0263e05b047db5b7e50f7089f4a739a2d5..4b614f39978e49c6a41d9869e6f60fd251d9bc76 100644 (file)
 
 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);
 }
index 3c2244be88bc8c934e123d63ba5785d351a9ad24..98f687452696e2e923522b6c911dcf980f2cdfef 100644 (file)
@@ -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;
This page took 0.029322 seconds and 4 git commands to generate.