+/*
+ * 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;
+ }
+ }
+ if (missing_field)
+ return -EPERM;
+
+ /*
+ * Check the enumeration: it must map each value to one and only one
+ * enumerator tag.
+ * TODO: we should also check that each range map to one and only one
+ * tag. For the moment, we will simply check this dynamically in
+ * variant_declaration_get_current_field().
+ */
+ return 0;
+}
+
+
+