+static
+struct declaration_variant *
+ lookup_variant_declaration_scope(GQuark variant_name,
+ struct declaration_scope *scope)
+{
+ return g_hash_table_lookup(scope->variant_declarations,
+ (gconstpointer) (unsigned long) variant_name);
+}
+
+struct declaration_variant *
+ lookup_variant_declaration(GQuark variant_name,
+ struct declaration_scope *scope)
+{
+ struct declaration_variant *declaration;
+
+ while (scope) {
+ declaration = lookup_variant_declaration_scope(variant_name, scope);
+ if (declaration)
+ return declaration;
+ scope = scope->parent_scope;
+ }
+ return NULL;
+}
+
+int register_variant_declaration(GQuark variant_name,
+ struct declaration_variant *variant_declaration,
+ struct declaration_scope *scope)
+{
+ if (!variant_name)
+ return -EPERM;
+
+ /* Only lookup in local scope */
+ if (lookup_variant_declaration_scope(variant_name, scope))
+ return -EEXIST;
+
+ g_hash_table_insert(scope->variant_declarations,
+ (gpointer) (unsigned long) variant_name,
+ variant_declaration);
+ declaration_ref(&variant_declaration->p);
+ return 0;
+}
+
+static
+struct declaration_enum *
+ lookup_enum_declaration_scope(GQuark enum_name,
+ struct declaration_scope *scope)
+{
+ return g_hash_table_lookup(scope->enum_declarations,
+ (gconstpointer) (unsigned long) enum_name);
+}
+
+struct declaration_enum *
+ lookup_enum_declaration(GQuark enum_name,
+ struct declaration_scope *scope)
+{
+ struct declaration_enum *declaration;
+
+ while (scope) {
+ declaration = lookup_enum_declaration_scope(enum_name, scope);
+ if (declaration)
+ return declaration;
+ scope = scope->parent_scope;
+ }
+ return NULL;
+}
+
+int register_enum_declaration(GQuark enum_name,
+ struct declaration_enum *enum_declaration,
+ struct declaration_scope *scope)
+{
+ if (!enum_name)
+ return -EPERM;
+
+ /* Only lookup in local scope */
+ if (lookup_enum_declaration_scope(enum_name, scope))
+ return -EEXIST;
+
+ g_hash_table_insert(scope->enum_declarations,
+ (gpointer) (unsigned long) enum_name,
+ enum_declaration);
+ declaration_ref(&enum_declaration->p);
+ return 0;
+}
+
+static struct definition_scope *
+ _new_definition_scope(struct definition_scope *parent_scope,
+ int scope_path_len)
+{
+ struct definition_scope *scope = g_new(struct definition_scope, 1);
+
+ scope->definitions = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) definition_unref);
+ scope->parent_scope = parent_scope;
+ scope->scope_path = g_array_sized_new(FALSE, TRUE, sizeof(GQuark),
+ scope_path_len);
+ g_array_set_size(scope->scope_path, scope_path_len);
+ return scope;
+}
+
+struct definition_scope *
+ new_definition_scope(struct definition_scope *parent_scope,
+ GQuark field_name)
+{
+ struct definition_scope *scope;
+ int scope_path_len = 1;
+
+ if (parent_scope)
+ scope_path_len += parent_scope->scope_path->len;
+ scope = _new_definition_scope(parent_scope, scope_path_len);
+ if (parent_scope)
+ memcpy(scope->scope_path, parent_scope->scope_path,
+ sizeof(GQuark) * (scope_path_len - 1));
+ g_array_index(scope->scope_path, GQuark, scope_path_len - 1) =
+ field_name;
+ return scope;
+}
+
+/*
+ * Same as new_definition_scope, but reset the scope path.
+ */
+struct definition_scope *
+ new_dynamic_definition_scope(struct definition_scope *parent_scope,
+ GQuark field_name)
+{
+ struct definition_scope *scope;
+
+ scope = _new_definition_scope(parent_scope, 1);
+ g_array_index(scope->scope_path, GQuark, 0) = field_name;
+ return scope;
+}
+
+void free_definition_scope(struct definition_scope *scope)