diff options
author | Wenchao Xia <wenchaoqemu@gmail.com> | 2014-03-06 17:08:56 -0800 |
---|---|---|
committer | Luiz Capitulino <lcapitulino@redhat.com> | 2014-03-11 09:07:42 -0400 |
commit | bceae7697ff1711675c26f715b945737bc6849ae (patch) | |
tree | b528a35656db4d66e9c5be5be54edc7cbeb79344 /scripts/qapi.py | |
parent | b0b58195e4a3039b6a473124dc27ed707db50240 (diff) |
qapi script: support enum type as discriminator in union
By default, any union will automatically generate a enum type as
"[UnionName]Kind" in C code, and it is duplicated when the discriminator
is specified as a pre-defined enum type in schema. After this patch,
the pre-defined enum type will be really used as the switch case
condition in generated C code, if discriminator is an enum field.
Signed-off-by: Wenchao Xia <wenchaoqemu@gmail.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
Diffstat (limited to 'scripts/qapi.py')
-rw-r--r-- | scripts/qapi.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/scripts/qapi.py b/scripts/qapi.py index eebc8a7061..2b43ad2f1b 100644 --- a/scripts/qapi.py +++ b/scripts/qapi.py @@ -180,6 +180,25 @@ def find_base_fields(base): return None return base_struct_define['data'] +# Return the discriminator enum define if discriminator is specified as an +# enum type, otherwise return None. +def discriminator_find_enum_define(expr): + base = expr.get('base') + discriminator = expr.get('discriminator') + + if not (discriminator and base): + return None + + base_fields = find_base_fields(base) + if not base_fields: + return None + + discriminator_type = base_fields.get(discriminator) + if not discriminator_type: + return None + + return find_enum(discriminator_type) + def check_union(expr, expr_info): name = expr['union'] base = expr.get('base') @@ -254,11 +273,17 @@ def parse_schema(fp): add_enum(expr['enum'], expr['data']) elif expr.has_key('union'): add_union(expr) - add_enum('%sKind' % expr['union']) elif expr.has_key('type'): add_struct(expr) exprs.append(expr) + # Try again for hidden UnionKind enum + for expr_elem in schema.exprs: + expr = expr_elem['expr'] + if expr.has_key('union'): + if not discriminator_find_enum_define(expr): + add_enum('%sKind' % expr['union']) + try: check_exprs(schema) except QAPIExprError, e: |