- struct type_class_variant *variant_class;
- struct type_class *type_class;
- int ret;
-
- variant_class = g_new(struct type_class_variant, 1);
- type_class = &variant_class->p;
- variant_class->fields_by_tag = g_hash_table_new(g_direct_hash,
- g_direct_equal);
- variant_class->fields = g_array_sized_new(FALSE, TRUE,
- sizeof(struct type_class_field),
- DEFAULT_NR_STRUCT_FIELDS);
- type_class->name = g_quark_from_string(name);
- type_class->alignment = 1;
- type_class->copy = variant_copy;
- type_class->class_free = _variant_type_class_free;
- type_class->type_new = _variant_type_new;
- type_class->type_free = _variant_type_free;
- type_class->ref = 1;
-
- if (type_class->name) {
- ret = register_type(type_class);
- if (ret)
- goto error_register;
+ struct declaration_variant *variant_declaration;
+ struct declaration *declaration;
+
+ variant_declaration = g_new(struct declaration_variant, 1);
+ declaration = &variant_declaration->p;
+ variant_declaration->untagged_variant = untagged_variant;
+ variant_declaration->tag_name = g_array_new(FALSE, TRUE, sizeof(GQuark));
+ append_scope_path(tag, variant_declaration->tag_name);
+ declaration->id = CTF_TYPE_VARIANT;
+ declaration->alignment = 1;
+ declaration->copy = variant_copy;
+ declaration->declaration_free = _variant_declaration_free;
+ declaration->definition_new = _variant_definition_new;
+ declaration->definition_free = _variant_definition_free;
+ declaration->ref = 1;
+ return variant_declaration;
+}
+
+/*
+ * tag_instance is assumed to be an enumeration.
+ * Returns 0 if OK, < 0 if error.
+ */
+static
+int check_enum_tag(struct definition_variant *variant,
+ struct definition *enum_tag)
+{
+ struct definition_enum *_enum =
+ container_of(enum_tag, struct definition_enum, p);
+ struct declaration_enum *enum_declaration = _enum->declaration;
+ int missing_field = 0;
+ unsigned long i;
+
+ /*
+ * Strictly speaking, each enumerator must map to a field of the
+ * variant. However, we are even stricter here by requiring that each
+ * variant choice map to an enumerator too. We then validate that the
+ * number of enumerators equals the number of variant choices.
+ */
+ if (variant->declaration->untagged_variant->fields->len != enum_get_nr_enumerators(enum_declaration))
+ return -EPERM;
+
+ for (i = 0; i < variant->declaration->untagged_variant->fields->len; i++) {
+ struct declaration_field *field_declaration =
+ &g_array_index(variant->declaration->untagged_variant->fields,
+ struct declaration_field, i);
+ if (!enum_quark_to_range_set(enum_declaration, field_declaration->name)) {
+ missing_field = 1;
+ break;
+ }