+static
+struct declaration *
+ lookup_type_declaration_scope(GQuark type_name, struct type_scope *scope)
+{
+ return g_hash_table_lookup(scope->type_declarations,
+ (gconstpointer) (unsigned long) type_name);
+}
+
+struct declaration *lookup_type_declaration(GQuark type_name, struct type_scope *scope)
+{
+ struct declaration *declaration;
+
+ while (scope) {
+ declaration = lookup_type_declaration_scope(type_name, scope);
+ if (declaration)
+ return declaration;
+ scope = scope->parent_scope;
+ }
+ return NULL;
+}
+
+int register_type_declaration(GQuark name, struct declaration *declaration,
+ struct type_scope *scope)
+{
+ if (!name)
+ return -EPERM;
+
+ /* Only lookup in local scope */
+ if (lookup_type_declaration_scope(name, scope))
+ return -EEXIST;
+
+ g_hash_table_insert(scope->type_declarations,
+ (gpointer) (unsigned long) name,
+ declaration);
+ declaration_ref(declaration);
+ return 0;
+}
+
+static
+struct declaration *
+ lookup_field_declaration_scope(GQuark field_name, struct declaration_scope *scope)
+{
+ return g_hash_table_lookup(scope->declarations,
+ (gconstpointer) (unsigned long) field_name);
+}
+
+struct declaration *
+ lookup_field_declaration(GQuark field_name, struct declaration_scope *scope)
+{
+ struct declaration *declaration;
+
+ while (scope) {
+ declaration = lookup_field_declaration_scope(field_name, scope);
+ if (declaration)
+ return declaration;
+ scope = scope->parent_scope;
+ }
+ return NULL;
+}
+
+int register_field_declaration(GQuark field_name, struct declaration *declaration,
+ struct declaration_scope *scope)
+{
+ if (!field_name)
+ return -EPERM;
+
+ /* Only lookup in local scope */
+ if (lookup_field_declaration_scope(field_name, scope))
+ return -EEXIST;
+
+ g_hash_table_insert(scope->declarations,
+ (gpointer) (unsigned long) field_name,
+ declaration);
+ declaration_ref(declaration);
+ return 0;
+}
+
+void type_ref(struct type *type)
+{
+ type->ref++;
+}
+
+void type_unref(struct type *type)
+{
+ if (!--type->ref)
+ type->type_free(type);
+}
+
+void declaration_ref(struct declaration *declaration)
+{
+ declaration->ref++;
+}
+
+void declaration_unref(struct declaration *declaration)
+{
+ if (!--declaration->ref)
+ 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->type_declarations = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) declaration_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;
+}