X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=types%2Ftypes.c;h=54116540762c01e99c96ee0d19b4ea2bd9e47d78;hp=961337800a1e45472a2abd175de21448726884d5;hb=05628561ca57ff5d269571a72a12cb86854c5f70;hpb=c054553dac076f91196b372fa19efaf2adc4e4f9 diff --git a/types/types.c b/types/types.c index 96133780..54116540 100644 --- a/types/types.c +++ b/types/types.c @@ -23,86 +23,135 @@ #include static -struct type_class *lookup_type_class_scope(GQuark qname, - struct declaration_scope *scope) +struct type * + lookup_type_scope(GQuark type_name, struct type_scope *scope) { - return g_hash_table_lookup(scope->type_classes, - (gconstpointer) (unsigned long) qname); + return g_hash_table_lookup(scope->types, + (gconstpointer) (unsigned long) type_name); } -struct type_class *lookup_type_class(GQuark qname, - struct declaration_scope *scope) +struct type *lookup_type(GQuark type_name, struct type_scope *scope) { - struct type_class *tc; + struct type *type; while (scope) { - tc = lookup_type_class_scope(qname, scope); - if (tc) - return tc; + type = lookup_type_scope(type_name, scope); + if (type) + return type; scope = scope->parent_scope; } return NULL; } -static void free_type_class(struct type_class *type_class) +int register_type(GQuark name, struct type *type, struct type_scope *scope) { - type_class->class_free(type_class); + if (!name) + return -EPERM; + + /* Only lookup in local scope */ + if (lookup_type_scope(name, scope)) + return -EEXIST; + + g_hash_table_insert(scope->types, + (gpointer) (unsigned long) name, + type); + type_ref(type); + return 0; } -static void free_type(struct type *type) +static +struct declaration * + lookup_declaration_scope(GQuark field_name, struct declaration_scope *scope) { - type->p.type_free(type); + return g_hash_table_lookup(scope->declarations, + (gconstpointer) (unsigned long) field_name); } -int register_type_class(struct type_class *type_class, - struct declaration_scope *scope) +struct declaration * + lookup_declaration(GQuark field_name, struct declaration_scope *scope) { + struct declaration *declaration; + + while (scope) { + declaration = lookup_declaration_scope(field_name, scope); + if (declaration) + return declaration; + scope = scope->parent_scope; + } + return NULL; +} + +int register_declaration(GQuark field_name, struct declaration *declaration, + struct declaration_scope *scope) +{ + if (!field_name) + return -EPERM; + /* Only lookup in local scope */ - if (lookup_type_class_scope(type_class->name, scope)) + if (lookup_declaration_scope(field_name, scope)) return -EEXIST; - g_hash_table_insert(scope->type_classes, - (gpointer) (unsigned long) type_class->name, - type_class); + g_hash_table_insert(scope->declarations, + (gpointer) (unsigned long) field_name, + declaration); + declaration_ref(declaration); return 0; } -void type_class_ref(struct type_class *type_class) +void type_ref(struct type *type) +{ + type->ref++; +} + +void type_unref(struct type *type) { - type_class->ref++; + if (!--type->ref) + type->type_free(type); } -void type_class_unref(struct type_class *type_class) +void declaration_ref(struct declaration *declaration) { - if (!--type_class->ref) - free_type_class(type_class); + declaration->ref++; } -void type_ref(struct type *type) +void declaration_unref(struct declaration *declaration) { - type->ref++; + if (!--declaration->ref) + declaration->type->declaration_free(declaration); } -void type_unref(struct type *type) +struct type_scope * + new_type_scope(struct type_scope *parent_scope) { - if (!--type->ref) - free_type(type); + 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) + new_declaration_scope(struct declaration_scope *parent_scope) { struct declaration_scope *scope = g_new(struct declaration_scope, 1); - scope->type_classes = g_hash_table_new_full(g_direct_hash, + scope->declarations = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, - (GDestroyNotify) type_class_unref); + (GDestroyNotify) declaration_unref); scope->parent_scope = parent_scope; return scope; } void free_declaration_scope(struct declaration_scope *scope) { - g_hash_table_destroy(scope->type_classes); + g_hash_table_destroy(scope->declarations); g_free(scope); }