mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2025-08-04 10:41:17 +00:00
qapi-types: Consolidate gen_struct() and gen_union()
These two methods are now close enough that we can finally merge them, relying on the fact that simple unions now provide a reasonable local_members. Change gen_struct() to gen_object() that handles all forms of QAPISchemaObjectType, and rename and shrink gen_union() to gen_variants() to handle the portion of gen_object() needed when variants are present. gen_struct_fields() now has a single caller, so it no longer needs an optional parameter; however, I did not choose to inline it into the caller. No difference to generated code. Backports commit 570cd8d1194cf68f7c9948971e52e47f20855a77 from qemu
This commit is contained in:
parent
b7886e2d59
commit
f637efea10
|
@ -49,7 +49,7 @@ def gen_struct_field(member):
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def gen_struct_fields(local_members, base=None):
|
def gen_struct_fields(local_members, base):
|
||||||
ret = ''
|
ret = ''
|
||||||
|
|
||||||
if base:
|
if base:
|
||||||
|
@ -67,7 +67,7 @@ def gen_struct_fields(local_members, base=None):
|
||||||
ret += gen_struct_field(memb)
|
ret += gen_struct_field(memb)
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def gen_struct(name, base, members):
|
def gen_object(name, base, members, variants):
|
||||||
ret = mcgen('''
|
ret = mcgen('''
|
||||||
struct %(c_name)s {
|
struct %(c_name)s {
|
||||||
''',
|
''',
|
||||||
|
@ -75,11 +75,14 @@ struct %(c_name)s {
|
||||||
|
|
||||||
ret += gen_struct_fields(members, base)
|
ret += gen_struct_fields(members, base)
|
||||||
|
|
||||||
|
if variants:
|
||||||
|
ret += gen_variants(variants)
|
||||||
|
|
||||||
# Make sure that all structs have at least one field; this avoids
|
# Make sure that all structs have at least one field; this avoids
|
||||||
# potential issues with attempting to malloc space for zero-length
|
# potential issues with attempting to malloc space for zero-length
|
||||||
# structs in C, and also incompatibility with C++ (where an empty
|
# structs in C, and also incompatibility with C++ (where an empty
|
||||||
# struct is size 1).
|
# struct is size 1).
|
||||||
if not (base and base.members) and not members:
|
if not (base and base.members) and not members and not variants:
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
char qapi_dummy_field_for_empty_struct;
|
char qapi_dummy_field_for_empty_struct;
|
||||||
''')
|
''')
|
||||||
|
@ -137,18 +140,7 @@ const int %(c_name)s_qtypes[QTYPE_MAX] = {
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def gen_union(name, base, variants):
|
def gen_variants(variants):
|
||||||
ret = mcgen('''
|
|
||||||
struct %(c_name)s {
|
|
||||||
''',
|
|
||||||
c_name=c_name(name))
|
|
||||||
if base:
|
|
||||||
ret += gen_struct_fields([], base)
|
|
||||||
else:
|
|
||||||
ret += gen_struct_field(variants.tag_member.name,
|
|
||||||
variants.tag_member.type,
|
|
||||||
False)
|
|
||||||
|
|
||||||
# FIXME: What purpose does data serve, besides preventing a union that
|
# FIXME: What purpose does data serve, besides preventing a union that
|
||||||
# has a branch named 'data'? We use it in qapi-visit.py to decide
|
# has a branch named 'data'? We use it in qapi-visit.py to decide
|
||||||
# whether to bypass the switch statement if visiting the discriminator
|
# whether to bypass the switch statement if visiting the discriminator
|
||||||
|
@ -157,11 +149,11 @@ struct %(c_name)s {
|
||||||
# should not be any data leaks even without a data pointer. Or, if
|
# should not be any data leaks even without a data pointer. Or, if
|
||||||
# 'data' is merely added to guarantee we don't have an empty union,
|
# 'data' is merely added to guarantee we don't have an empty union,
|
||||||
# shouldn't we enforce that at .json parse time?
|
# shouldn't we enforce that at .json parse time?
|
||||||
ret += mcgen('''
|
ret = mcgen('''
|
||||||
union { /* union tag is @%(c_name)s */
|
union { /* union tag is @%(c_name)s */
|
||||||
void *data;
|
void *data;
|
||||||
''',
|
''',
|
||||||
c_name=c_name(variants.tag_member.name))
|
c_name=c_name(variants.tag_member.name))
|
||||||
|
|
||||||
for var in variants.variants:
|
for var in variants.variants:
|
||||||
# Ugly special case for simple union TODO get rid of it
|
# Ugly special case for simple union TODO get rid of it
|
||||||
|
@ -174,7 +166,6 @@ struct %(c_name)s {
|
||||||
|
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
} u;
|
} u;
|
||||||
};
|
|
||||||
''')
|
''')
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
|
@ -265,14 +256,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
|
||||||
|
|
||||||
def visit_object_type(self, name, info, base, members, variants):
|
def visit_object_type(self, name, info, base, members, variants):
|
||||||
self._fwdecl += gen_fwd_object_or_array(name)
|
self._fwdecl += gen_fwd_object_or_array(name)
|
||||||
if variants:
|
self.decl += gen_object(name, base, members, variants)
|
||||||
if members:
|
|
||||||
# Members other than variants.tag_member not implemented
|
|
||||||
assert len(members) == 1
|
|
||||||
assert members[0] == variants.tag_member
|
|
||||||
self.decl += gen_union(name, base, variants)
|
|
||||||
else:
|
|
||||||
self.decl += gen_struct(name, base, members)
|
|
||||||
if base:
|
if base:
|
||||||
self.decl += gen_upcast(name, base)
|
self.decl += gen_upcast(name, base)
|
||||||
self._gen_type_cleanup(name)
|
self._gen_type_cleanup(name)
|
||||||
|
@ -280,7 +264,7 @@ class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
|
||||||
def visit_alternate_type(self, name, info, variants):
|
def visit_alternate_type(self, name, info, variants):
|
||||||
self._fwdecl += gen_fwd_object_or_array(name)
|
self._fwdecl += gen_fwd_object_or_array(name)
|
||||||
self._fwdefn += gen_alternate_qtypes(name, variants)
|
self._fwdefn += gen_alternate_qtypes(name, variants)
|
||||||
self.decl += gen_union(name, None, variants)
|
self.decl += gen_object(name, None, [variants.tag_member], variants)
|
||||||
self.decl += gen_alternate_qtypes_decl(name)
|
self.decl += gen_alternate_qtypes_decl(name)
|
||||||
self._gen_type_cleanup(name)
|
self._gen_type_cleanup(name)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue