X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=types%2Ftypes.c;h=54116540762c01e99c96ee0d19b4ea2bd9e47d78;hp=b766f8d8d4cb415e23011c82eaf217ded71b110a;hb=05628561ca57ff5d269571a72a12cb86854c5f70;hpb=e19c3d69b39d2fa422ab54b5ec7192799f536680 diff --git a/types/types.c b/types/types.c index b766f8d8..54116540 100644 --- a/types/types.c +++ b/types/types.c @@ -23,18 +23,19 @@ #include static -struct type *lookup_type_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->types, - (gconstpointer) (unsigned long) qname); + (gconstpointer) (unsigned long) type_name); } -struct type *lookup_type(GQuark qname, struct declaration_scope *scope) +struct type *lookup_type(GQuark type_name, struct type_scope *scope) { struct type *type; while (scope) { - type = lookup_type_scope(qname, scope); + type = lookup_type_scope(type_name, scope); if (type) return type; scope = scope->parent_scope; @@ -42,25 +43,58 @@ struct type *lookup_type(GQuark qname, struct declaration_scope *scope) return NULL; } -static void free_type(struct type *type) +int register_type(GQuark name, struct type *type, struct type_scope *scope) { - type->type_free(type); + 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 +struct declaration * + lookup_declaration_scope(GQuark field_name, struct declaration_scope *scope) +{ + return g_hash_table_lookup(scope->declarations, + (gconstpointer) (unsigned long) field_name); } -static void free_declaration(struct declaration *declaration) +struct declaration * + lookup_declaration(GQuark field_name, struct declaration_scope *scope) { - declaration->p.declaration_free(declaration); + struct declaration *declaration; + + while (scope) { + declaration = lookup_declaration_scope(field_name, scope); + if (declaration) + return declaration; + scope = scope->parent_scope; + } + return NULL; } -int register_type(struct type *type, struct declaration_scope *scope) +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_scope(type->name, scope)) + if (lookup_declaration_scope(field_name, scope)) return -EEXIST; - g_hash_table_insert(scope->types, - (gpointer) (unsigned long) type->name, - type); + g_hash_table_insert(scope->declarations, + (gpointer) (unsigned long) field_name, + declaration); + declaration_ref(declaration); return 0; } @@ -72,7 +106,7 @@ void type_ref(struct type *type) void type_unref(struct type *type) { if (!--type->ref) - free_type(type); + type->type_free(type); } void declaration_ref(struct declaration *declaration) @@ -83,7 +117,25 @@ void declaration_ref(struct declaration *declaration) void declaration_unref(struct declaration *declaration) { if (!--declaration->ref) - free_declaration(declaration); + declaration->type->declaration_free(declaration); +} + +struct type_scope * + new_type_scope(struct type_scope *parent_scope) +{ + 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 * @@ -91,15 +143,15 @@ struct declaration_scope * { struct declaration_scope *scope = g_new(struct declaration_scope, 1); - scope->types = g_hash_table_new_full(g_direct_hash, + scope->declarations = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, - (GDestroyNotify) type_unref); + (GDestroyNotify) declaration_unref); scope->parent_scope = parent_scope; return scope; } void free_declaration_scope(struct declaration_scope *scope) { - g_hash_table_destroy(scope->types); + g_hash_table_destroy(scope->declarations); g_free(scope); }