1""" 2QAPI event generator 3 4Copyright (c) 2014 Wenchao Xia 5Copyright (c) 2015-2018 Red Hat Inc. 6 7Authors: 8 Wenchao Xia <wenchaoqemu@gmail.com> 9 Markus Armbruster <armbru@redhat.com> 10 11This work is licensed under the terms of the GNU GPL, version 2. 12See the COPYING file in the top-level directory. 13""" 14 15from qapi.common import * 16 17 18def build_event_send_proto(name, arg_type, boxed): 19 return 'void qapi_event_send_%(c_name)s(%(param)s)' % { 20 'c_name': c_name(name.lower()), 21 'param': build_params(arg_type, boxed)} 22 23 24def gen_event_send_decl(name, arg_type, boxed): 25 return mcgen(''' 26 27%(proto)s; 28''', 29 proto=build_event_send_proto(name, arg_type, boxed)) 30 31 32# Declare and initialize an object 'qapi' using parameters from build_params() 33def gen_param_var(typ): 34 assert not typ.variants 35 ret = mcgen(''' 36 %(c_name)s param = { 37''', 38 c_name=typ.c_name()) 39 sep = ' ' 40 for memb in typ.members: 41 ret += sep 42 sep = ', ' 43 if memb.optional: 44 ret += 'has_' + c_name(memb.name) + sep 45 if memb.type.name == 'str': 46 # Cast away const added in build_params() 47 ret += '(char *)' 48 ret += c_name(memb.name) 49 ret += mcgen(''' 50 51 }; 52''') 53 if not typ.is_implicit(): 54 ret += mcgen(''' 55 %(c_name)s *arg = ¶m; 56''', 57 c_name=typ.c_name()) 58 return ret 59 60 61def gen_event_send(name, arg_type, boxed, event_enum_name, event_emit): 62 # FIXME: Our declaration of local variables (and of 'errp' in the 63 # parameter list) can collide with exploded members of the event's 64 # data type passed in as parameters. If this collision ever hits in 65 # practice, we can rename our local variables with a leading _ prefix, 66 # or split the code into a wrapper function that creates a boxed 67 # 'param' object then calls another to do the real work. 68 ret = mcgen(''' 69 70%(proto)s 71{ 72 QDict *qmp; 73''', 74 proto=build_event_send_proto(name, arg_type, boxed)) 75 76 if arg_type and not arg_type.is_empty(): 77 ret += mcgen(''' 78 QObject *obj; 79 Visitor *v; 80''') 81 if not boxed: 82 ret += gen_param_var(arg_type) 83 else: 84 assert not boxed 85 86 ret += mcgen(''' 87 88 qmp = qmp_event_build_dict("%(name)s"); 89 90''', 91 name=name) 92 93 if arg_type and not arg_type.is_empty(): 94 ret += mcgen(''' 95 v = qobject_output_visitor_new(&obj); 96''') 97 if not arg_type.is_implicit(): 98 ret += mcgen(''' 99 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort); 100''', 101 name=name, c_name=arg_type.c_name()) 102 else: 103 ret += mcgen(''' 104 105 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort); 106 visit_type_%(c_name)s_members(v, ¶m, &error_abort); 107 visit_check_struct(v, &error_abort); 108 visit_end_struct(v, NULL); 109''', 110 name=name, c_name=arg_type.c_name()) 111 ret += mcgen(''' 112 113 visit_complete(v, &obj); 114 qdict_put_obj(qmp, "data", obj); 115''') 116 117 ret += mcgen(''' 118 %(event_emit)s(%(c_enum)s, qmp); 119 120''', 121 event_emit=event_emit, 122 c_enum=c_enum_const(event_enum_name, name)) 123 124 if arg_type and not arg_type.is_empty(): 125 ret += mcgen(''' 126 visit_free(v); 127''') 128 ret += mcgen(''' 129 qobject_unref(qmp); 130} 131''') 132 return ret 133 134 135class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): 136 137 def __init__(self, prefix): 138 QAPISchemaModularCVisitor.__init__( 139 self, prefix, 'qapi-events', 140 ' * Schema-defined QAPI/QMP events', __doc__) 141 self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False) 142 self._event_enum_members = [] 143 self._event_emit_name = c_name(prefix + 'qapi_event_emit') 144 145 def _begin_module(self, name): 146 types = self._module_basename('qapi-types', name) 147 visit = self._module_basename('qapi-visit', name) 148 self._genc.add(mcgen(''' 149#include "qemu/osdep.h" 150#include "qemu-common.h" 151#include "%(prefix)sqapi-events.h" 152#include "%(visit)s.h" 153#include "qapi/error.h" 154#include "qapi/qmp/qdict.h" 155#include "qapi/qobject-output-visitor.h" 156#include "qapi/qmp-event.h" 157 158''', 159 visit=visit, prefix=self._prefix)) 160 self._genh.add(mcgen(''' 161#include "qapi/util.h" 162#include "%(types)s.h" 163 164''', 165 types=types)) 166 167 def visit_end(self): 168 (genc, genh) = self._module[self._main_module] 169 genh.add(gen_enum(self._event_enum_name, 170 self._event_enum_members)) 171 genc.add(gen_enum_lookup(self._event_enum_name, 172 self._event_enum_members)) 173 genh.add(mcgen(''' 174 175void %(event_emit)s(%(event_enum)s event, QDict *qdict); 176''', 177 event_emit=self._event_emit_name, 178 event_enum=self._event_enum_name)) 179 180 def visit_event(self, name, info, ifcond, arg_type, boxed): 181 with ifcontext(ifcond, self._genh, self._genc): 182 self._genh.add(gen_event_send_decl(name, arg_type, boxed)) 183 self._genc.add(gen_event_send(name, arg_type, boxed, 184 self._event_enum_name, 185 self._event_emit_name)) 186 self._event_enum_members.append(QAPISchemaMember(name, ifcond)) 187 188 189def gen_events(schema, output_dir, prefix): 190 vis = QAPISchemaGenEventVisitor(prefix) 191 schema.visit(vis) 192 vis.write(output_dir) 193