+
+
+static
+struct definition *
+ _variant_definition_new(struct declaration *declaration,
+ struct definition_scope *parent_scope,
+ GQuark field_name, int index,
+ const char *root_name)
+{
+ struct declaration_variant *variant_declaration =
+ container_of(declaration, struct declaration_variant, p);
+ struct definition_variant *variant;
+ unsigned long i;
+ int ret;
+
+ variant = g_new(struct definition_variant, 1);
+ declaration_ref(&variant_declaration->p);
+ variant->p.declaration = declaration;
+ variant->declaration = variant_declaration;
+ variant->p.ref = 1;
+ /*
+ * Use INT_MAX order to ensure that all fields of the parent
+ * scope are seen as being prior to this scope.
+ */
+ variant->p.index = root_name ? INT_MAX : index;
+ variant->p.name = field_name;
+ variant->p.path = new_definition_path(parent_scope, field_name, root_name);
+ variant->p.scope = new_definition_scope(parent_scope, field_name, root_name);
+
+ ret = register_field_definition(field_name, &variant->p,
+ parent_scope);
+ assert(!ret);
+
+ variant->enum_tag = lookup_path_definition(variant->p.scope->scope_path,
+ variant_declaration->tag_name,
+ parent_scope);
+
+ if (!variant->enum_tag
+ || check_enum_tag(variant, variant->enum_tag) < 0)
+ goto error;
+ definition_ref(variant->enum_tag);
+ variant->fields = g_ptr_array_sized_new(variant_declaration->untagged_variant->fields->len);
+ g_ptr_array_set_size(variant->fields, variant_declaration->untagged_variant->fields->len);
+ for (i = 0; i < variant_declaration->untagged_variant->fields->len; i++) {
+ struct declaration_field *declaration_field =
+ &g_array_index(variant_declaration->untagged_variant->fields,
+ struct declaration_field, i);
+ struct definition **field =
+ (struct definition **) &g_ptr_array_index(variant->fields, i);
+
+ /*
+ * All child definition are at index 0, because they are
+ * various choices of the same field.
+ */
+ *field = declaration_field->declaration->definition_new(declaration_field->declaration,
+ variant->p.scope,
+ declaration_field->name, 0, NULL);
+ if (!*field)
+ goto error;
+ }
+ variant->current_field = NULL;
+ return &variant->p;
+error:
+ free_definition_scope(variant->p.scope);
+ declaration_unref(&variant_declaration->p);
+ g_free(variant);
+ return NULL;
+}
+
+static
+void _variant_definition_free(struct definition *definition)
+{
+ struct definition_variant *variant =
+ container_of(definition, struct definition_variant, p);
+ unsigned long i;
+
+ assert(variant->fields->len == variant->declaration->untagged_variant->fields->len);
+ for (i = 0; i < variant->fields->len; i++) {
+ struct definition *field = g_ptr_array_index(variant->fields, i);
+ definition_unref(field);
+ }
+ definition_unref(variant->enum_tag);
+ free_definition_scope(variant->p.scope);
+ declaration_unref(variant->p.declaration);
+ g_ptr_array_free(variant->fields, TRUE);
+ g_free(variant);
+}
+
+void untagged_variant_declaration_add_field(struct declaration_untagged_variant *untagged_variant_declaration,
+ const char *field_name,
+ struct declaration *field_declaration)
+{
+ struct declaration_field *field;
+ unsigned long index;
+
+ g_array_set_size(untagged_variant_declaration->fields, untagged_variant_declaration->fields->len + 1);
+ index = untagged_variant_declaration->fields->len - 1; /* last field (new) */
+ field = &g_array_index(untagged_variant_declaration->fields, struct declaration_field, index);
+ field->name = g_quark_from_string(field_name);
+ declaration_ref(field_declaration);
+ field->declaration = field_declaration;
+ /* Keep index in hash rather than pointer, because array can relocate */
+ g_hash_table_insert(untagged_variant_declaration->fields_by_tag,
+ (gpointer) (unsigned long) field->name,
+ (gpointer) index);
+ /*
+ * Alignment of variant is based on the alignment of its currently
+ * selected choice, so we leave variant alignment as-is (statically
+ * speaking).
+ */
+}
+
+struct declaration_field *
+untagged_variant_declaration_get_field_from_tag(struct declaration_untagged_variant *untagged_variant_declaration, GQuark tag)
+{
+ gpointer index;
+ gboolean found;
+
+ found = g_hash_table_lookup_extended(
+ untagged_variant_declaration->fields_by_tag,
+ (gconstpointer) (unsigned long) tag, NULL, &index);
+
+ if (!found) {
+ return NULL;
+ }
+
+ return &g_array_index(untagged_variant_declaration->fields, struct declaration_field, (unsigned long)index);
+}
+