mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2024-10-20 23:28:15 +02:00
d777876e6b
We already have several places that want to visit all the members of an implicit object within a larger context (simple union variant, event with anonymous data, command with anonymous arguments struct); and will be adding another one soon (the ability to declare an anonymous base for a flat union). Having a C struct declared for these implicit types, along with a visit_type_FOO_members() helper function, will make for fewer special cases in our generator. We do not, however, need qapi_free_FOO() or visit_type_FOO() functions for implicit types, because they should not be used directly outside of the generated code. This is done by adding a conditional in visit_object_type() for both qapi-types.py and qapi-visit.py based on the object name. The comparison of "name.startswith('q_')" is a bit hacky (it's basically duplicating what .is_implicit() already uses), but beats changing the signature of the visit_object_type() callback to pass a new 'implicit' flag. The hack should be temporary: we are considering adding a future patch that consolidates the narrow visit_object_type(..., base, local_members, variants) and visit_object_type_flat(..., all_members, variants) [where different sets of information are already broken out, and the QAPISchemaObjectType is no longer available] into a broader visit_object_type(obj_type) [where the visitor can query the needed fields from obj_type directly]. Also, now that we WANT to output C code for implicits, we no longer need the visit_needed() filter, leaving 'q_empty' as the only object still needing a special case. Remember, 'q_empty' is the only built-in generated object, which means that without a special case it would be emitted in multiple files (the main qapi-types.h and in qga-qapi-types.h) causing compilation failure due to redefinition. But since it has no members, it's easier to just avoid an attempt to visit that particular type; since gen_object() is called recursively, we also prime the objects_seen set to cover any recursion into the empty type. The patch relies on the changed naming of implicit types in the previous patch. It is a bit unfortunate that the generated struct names and visit_type_FOO_members() don't match normal naming conventions, but it's not too bad, since they will only be used in generated code. The generated code grows substantially in size: the implicit '-wrapper' types must be emitted in qapi-types.h before any union can include an unboxed member of that type. Arguably, the '-args' types could be emitted in a private header for just qapi-visit.c and qmp-marshal.c, rather than polluting qapi-types.h; but adding complexity to the generator to split the output location according to role doesn't seem worth the maintenance costs. Backports commit 7ce106a96feee4d46bfcdb47127b0935804c9357 from qemu
348 lines
9.2 KiB
Python
348 lines
9.2 KiB
Python
#
|
|
# QAPI types generator
|
|
#
|
|
# Copyright IBM, Corp. 2011
|
|
# Copyright (c) 2013-2016 Red Hat Inc.
|
|
#
|
|
# Authors:
|
|
# Anthony Liguori <aliguori@us.ibm.com>
|
|
# Markus Armbruster <armbru@redhat.com>
|
|
#
|
|
# This work is licensed under the terms of the GNU GPL, version 2.
|
|
# See the COPYING file in the top-level directory.
|
|
|
|
from qapi import *
|
|
|
|
# variants must be emitted before their container; track what has already
|
|
# been output
|
|
objects_seen = set()
|
|
|
|
|
|
def gen_fwd_object_or_array(name):
|
|
return mcgen('''
|
|
|
|
typedef struct %(c_name)s %(c_name)s;
|
|
''',
|
|
c_name=c_name(name))
|
|
|
|
def gen_array(name, element_type):
|
|
return mcgen('''
|
|
struct %(c_name)s {
|
|
union {
|
|
%(c_type)s value;
|
|
uint64_t padding;
|
|
};
|
|
struct %(c_name)s *next;
|
|
};
|
|
''',
|
|
c_name=c_name(name), c_type=element_type.c_type())
|
|
|
|
|
|
def gen_struct_members(members):
|
|
ret = ''
|
|
|
|
for memb in members:
|
|
if memb.optional:
|
|
ret += mcgen('''
|
|
bool has_%(c_name)s;
|
|
''',
|
|
c_name=c_name(memb.name))
|
|
ret += mcgen('''
|
|
%(c_type)s %(c_name)s;
|
|
''',
|
|
c_type=memb.type.c_type(), c_name=c_name(memb.name))
|
|
return ret
|
|
|
|
|
|
def gen_object(name, base, members, variants):
|
|
if name in objects_seen:
|
|
return ''
|
|
objects_seen.add(name)
|
|
|
|
ret = ''
|
|
if variants:
|
|
for v in variants.variants:
|
|
if isinstance(v.type, QAPISchemaObjectType):
|
|
ret += gen_object(v.type.name, v.type.base,
|
|
v.type.local_members, v.type.variants)
|
|
|
|
ret += mcgen('''
|
|
|
|
struct %(c_name)s {
|
|
''',
|
|
c_name=c_name(name))
|
|
|
|
if base:
|
|
ret += mcgen('''
|
|
/* Members inherited from %(c_name)s: */
|
|
''',
|
|
c_name=base.c_name())
|
|
ret += gen_struct_members(base.members)
|
|
ret += mcgen('''
|
|
/* Own members: */
|
|
''')
|
|
ret += gen_struct_members(members)
|
|
|
|
if variants:
|
|
ret += gen_variants(variants)
|
|
|
|
# Make sure that all structs have at least one member; this avoids
|
|
# potential issues with attempting to malloc space for zero-length
|
|
# structs in C, and also incompatibility with C++ (where an empty
|
|
# struct is size 1).
|
|
if not (base and base.members) and not members and not variants:
|
|
ret += mcgen('''
|
|
char qapi_dummy_for_empty_struct;
|
|
''')
|
|
|
|
ret += mcgen('''
|
|
};
|
|
''')
|
|
|
|
return ret
|
|
|
|
|
|
def gen_upcast(name, base):
|
|
# C makes const-correctness ugly. We have to cast away const to let
|
|
# this function work for both const and non-const obj.
|
|
return mcgen('''
|
|
|
|
static inline %(base)s *qapi_%(c_name)s_base(const %(c_name)s *obj)
|
|
{
|
|
return (%(base)s *)obj;
|
|
}
|
|
''',
|
|
c_name=c_name(name), base=base.c_name())
|
|
|
|
|
|
def gen_alternate_qtypes_decl(name):
|
|
return mcgen('''
|
|
|
|
extern const int %(c_name)s_qtypes[];
|
|
''',
|
|
c_name=c_name(name))
|
|
|
|
|
|
def gen_alternate_qtypes(name, variants):
|
|
ret = mcgen('''
|
|
|
|
const int %(c_name)s_qtypes[QTYPE__MAX] = {
|
|
''',
|
|
c_name=c_name(name))
|
|
|
|
for var in variants.variants:
|
|
qtype = var.type.alternate_qtype()
|
|
assert qtype
|
|
|
|
ret += mcgen('''
|
|
[%(qtype)s] = %(enum_const)s,
|
|
''',
|
|
qtype=qtype,
|
|
enum_const=c_enum_const(variants.tag_member.type.name,
|
|
var.name))
|
|
|
|
|
|
ret += mcgen('''
|
|
};
|
|
''')
|
|
return ret
|
|
|
|
|
|
def gen_variants(variants):
|
|
ret = mcgen('''
|
|
union { /* union tag is @%(c_name)s */
|
|
''',
|
|
c_name=c_name(variants.tag_member.name))
|
|
|
|
for var in variants.variants:
|
|
# Ugly special case for simple union TODO get rid of it
|
|
simple_union_type = var.simple_union_type()
|
|
if simple_union_type:
|
|
typ = simple_union_type.c_type()
|
|
else:
|
|
typ = var.type.c_unboxed_type()
|
|
ret += mcgen('''
|
|
%(c_type)s %(c_name)s;
|
|
''',
|
|
c_type=typ,
|
|
c_name=c_name(var.name))
|
|
|
|
ret += mcgen('''
|
|
} u;
|
|
''')
|
|
|
|
return ret
|
|
|
|
|
|
def gen_type_cleanup_decl(name):
|
|
ret = mcgen('''
|
|
void qapi_free_%(c_name)s(%(c_name)s *obj);
|
|
''',
|
|
c_name=c_name(name))
|
|
return ret
|
|
|
|
|
|
def gen_type_cleanup(name):
|
|
ret = mcgen('''
|
|
|
|
void qapi_free_%(c_name)s(%(c_name)s *obj)
|
|
{
|
|
QapiDeallocVisitor *qdv;
|
|
Visitor *v;
|
|
|
|
if (!obj) {
|
|
return;
|
|
}
|
|
|
|
qdv = qapi_dealloc_visitor_new();
|
|
v = qapi_dealloc_get_visitor(qdv);
|
|
visit_type_%(c_name)s(v, NULL, &obj, NULL);
|
|
qapi_dealloc_visitor_cleanup(qdv);
|
|
}
|
|
''',
|
|
c_name=c_name(name))
|
|
return ret
|
|
|
|
|
|
class QAPISchemaGenTypeVisitor(QAPISchemaVisitor):
|
|
def __init__(self):
|
|
self.decl = None
|
|
self.defn = None
|
|
self._fwdecl = None
|
|
self._btin = None
|
|
|
|
def visit_begin(self, schema):
|
|
# gen_object() is recursive, ensure it doesn't visit the empty type
|
|
objects_seen.add(schema.the_empty_object_type.name)
|
|
self.decl = ''
|
|
self.defn = ''
|
|
self._fwdecl = ''
|
|
self._fwdefn = ''
|
|
self._btin = guardstart('QAPI_TYPES_BUILTIN')
|
|
|
|
def visit_end(self):
|
|
self.decl = self._fwdecl + self.decl
|
|
self._fwdecl = None
|
|
# To avoid header dependency hell, we always generate
|
|
# declarations for built-in types in our header files and
|
|
# simply guard them. See also do_builtins (command line
|
|
# option -b).
|
|
self._btin += guardend('QAPI_TYPES_BUILTIN')
|
|
self.decl = self._btin + self.decl
|
|
self._btin = None
|
|
|
|
def _gen_type_cleanup(self, name):
|
|
self.decl += gen_type_cleanup_decl(name)
|
|
self.defn += gen_type_cleanup(name)
|
|
|
|
def visit_enum_type(self, name, info, values, prefix):
|
|
# Special case for our lone builtin enum type
|
|
# TODO use something cleaner than existence of info
|
|
if not info:
|
|
self._btin += gen_enum(name, values, prefix)
|
|
if do_builtins:
|
|
self.defn += gen_enum_lookup(name, values, prefix)
|
|
else:
|
|
self._fwdecl += gen_enum(name, values, prefix)
|
|
self.defn += gen_enum_lookup(name, values, prefix)
|
|
|
|
def visit_array_type(self, name, info, element_type):
|
|
if isinstance(element_type, QAPISchemaBuiltinType):
|
|
self._btin += gen_fwd_object_or_array(name)
|
|
self._btin += gen_array(name, element_type)
|
|
self._btin += gen_type_cleanup_decl(name)
|
|
if do_builtins:
|
|
self.defn += gen_type_cleanup(name)
|
|
else:
|
|
self._fwdecl += gen_fwd_object_or_array(name)
|
|
self.decl += gen_array(name, element_type)
|
|
self._gen_type_cleanup(name)
|
|
|
|
def visit_object_type(self, name, info, base, members, variants):
|
|
# Nothing to do for the special empty builtin
|
|
if name == 'q_empty':
|
|
return
|
|
self._fwdecl += gen_fwd_object_or_array(name)
|
|
self.decl += gen_object(name, base, members, variants)
|
|
if base:
|
|
self.decl += gen_upcast(name, base)
|
|
# TODO Worth changing the visitor signature, so we could
|
|
# directly use rather than repeat type.is_implicit()?
|
|
if not name.startswith('q_'):
|
|
# implicit types won't be directly allocated/freed
|
|
self._gen_type_cleanup(name)
|
|
|
|
def visit_alternate_type(self, name, info, variants):
|
|
self._fwdecl += gen_fwd_object_or_array(name)
|
|
self._fwdefn += gen_alternate_qtypes(name, variants)
|
|
self.decl += gen_object(name, None, [variants.tag_member], variants)
|
|
self.decl += gen_alternate_qtypes_decl(name)
|
|
self._gen_type_cleanup(name)
|
|
|
|
# If you link code generated from multiple schemata, you want only one
|
|
# instance of the code for built-in types. Generate it only when
|
|
# do_builtins, enabled by command line option -b. See also
|
|
# QAPISchemaGenTypeVisitor.visit_end().
|
|
do_builtins = False
|
|
|
|
(input_file, output_dir, do_c, do_h, prefix, opts) = \
|
|
parse_command_line("b", ["builtins"])
|
|
|
|
for o, a in opts:
|
|
if o in ("-b", "--builtins"):
|
|
do_builtins = True
|
|
|
|
c_comment = '''
|
|
/*
|
|
* deallocation functions for schema-defined QAPI types
|
|
*
|
|
* Copyright IBM, Corp. 2011
|
|
*
|
|
* Authors:
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
* Michael Roth <mdroth@linux.vnet.ibm.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
*
|
|
*/
|
|
'''
|
|
h_comment = '''
|
|
/*
|
|
* schema-defined QAPI types
|
|
*
|
|
* Copyright IBM, Corp. 2011
|
|
*
|
|
* Authors:
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
*
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
*
|
|
*/
|
|
'''
|
|
(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
|
|
'qapi-types.c', 'qapi-types.h',
|
|
c_comment, h_comment)
|
|
|
|
fdef.write(mcgen('''
|
|
#include "qapi/dealloc-visitor.h"
|
|
#include "%(prefix)sqapi-types.h"
|
|
#include "%(prefix)sqapi-visit.h"
|
|
''',
|
|
prefix=prefix))
|
|
|
|
# To avoid circular headers, use only typedefs.h here, not qobject.h
|
|
fdecl.write(mcgen('''
|
|
#include "qemu/typedefs.h"
|
|
#include "unicorn/platform.h"
|
|
'''))
|
|
|
|
schema = QAPISchema(input_file)
|
|
gen = QAPISchemaGenTypeVisitor()
|
|
schema.visit(gen)
|
|
fdef.write(gen.defn)
|
|
fdecl.write(gen.decl)
|
|
|
|
close_output(fdef, fdecl)
|