+static
+struct definition *
+ lookup_type_definition_scope(GQuark type_name, struct type_scope *scope)
+{
+ return g_hash_table_lookup(scope->type_definitions,
+ (gconstpointer) (unsigned long) type_name);
+}
+
+struct definition *lookup_type_definition(GQuark type_name, struct type_scope *scope)
+{
+ struct definition *definition;
+
+ while (scope) {
+ definition = lookup_type_definition_scope(type_name, scope);
+ if (definition)
+ return definition;
+ scope = scope->parent_scope;
+ }
+ return NULL;
+}
+
+int register_type_definition(GQuark name, struct definition *definition,
+ struct type_scope *scope)
+{
+ if (!name)
+ return -EPERM;
+
+ /* Only lookup in local scope */
+ if (lookup_type_definition_scope(name, scope))
+ return -EEXIST;
+
+ g_hash_table_insert(scope->type_definitions,
+ (gpointer) (unsigned long) name,
+ definition);
+ definition_ref(definition);
+ return 0;
+}
+
+static
+struct definition *
+ lookup_field_definition_scope(GQuark field_name, struct definition_scope *scope)
+{
+ return g_hash_table_lookup(scope->definitions,
+ (gconstpointer) (unsigned long) field_name);
+}
+
+struct definition *
+ lookup_field_definition(GQuark field_name, struct definition_scope *scope)
+{
+ struct definition *definition;
+
+ while (scope) {
+ definition = lookup_field_definition_scope(field_name, scope);
+ if (definition)
+ return definition;
+ scope = scope->parent_scope;
+ }
+ return NULL;
+}
+
+int register_field_definition(GQuark field_name, struct definition *definition,
+ struct definition_scope *scope)
+{
+ if (!field_name)
+ return -EPERM;
+
+ /* Only lookup in local scope */
+ if (lookup_field_definition_scope(field_name, scope))
+ return -EEXIST;
+
+ g_hash_table_insert(scope->definitions,
+ (gpointer) (unsigned long) field_name,
+ definition);
+ definition_ref(definition);
+ return 0;
+}
+
+void type_ref(struct type *type)
+{
+ type->ref++;
+}
+
+void type_unref(struct type *type)
+{
+ if (!--type->ref)
+ type->type_free(type);
+}
+
+void definition_ref(struct definition *definition)
+{
+ definition->ref++;
+}
+
+void definition_unref(struct definition *definition)
+{
+ if (!--definition->ref)
+ definition->type->definition_free(definition);
+}
+
+struct type_scope *
+ new_type_scope(struct type_scope *parent_scope)
+{
+ struct type_scope *scope = g_new(struct type_scope, 1);
+
+ scope->type_definitions = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) definition_unref);
+ scope->struct_types = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) type_unref);
+ scope->variant_types = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) type_unref);
+ scope->enum_types = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) type_unref);
+ scope->parent_scope = parent_scope;
+ return scope;
+}