1# 2# QAPI event generator 3# 4# Copyright (c) 2014 Wenchao Xia 5# Copyright (c) 2015-2016 Red Hat Inc. 6# 7# Authors: 8# Wenchao Xia <wenchaoqemu@gmail.com> 9# Markus Armbruster <armbru@redhat.com> 10# 11# This work is licensed under the terms of the GNU GPL, version 2. 12# See the COPYING file in the top-level directory. 13 14from qapi import * 15 16 17def gen_event_send_proto(name, arg_type, boxed): 18 return 'void qapi_event_send_%(c_name)s(%(param)s)' % { 19 'c_name': c_name(name.lower()), 20 'param': gen_params(arg_type, boxed, 'Error **errp')} 21 22 23def gen_event_send_decl(name, arg_type, boxed): 24 return mcgen(''' 25 26%(proto)s; 27''', 28 proto=gen_event_send_proto(name, arg_type, boxed)) 29 30 31# Declare and initialize an object 'qapi' using parameters from gen_params() 32def gen_param_var(typ): 33 assert not typ.variants 34 ret = mcgen(''' 35 %(c_name)s param = { 36''', 37 c_name=typ.c_name()) 38 sep = ' ' 39 for memb in typ.members: 40 ret += sep 41 sep = ', ' 42 if memb.optional: 43 ret += 'has_' + c_name(memb.name) + sep 44 if memb.type.name == 'str': 45 # Cast away const added in gen_params() 46 ret += '(char *)' 47 ret += c_name(memb.name) 48 ret += mcgen(''' 49 50 }; 51''') 52 if not typ.is_implicit(): 53 ret += mcgen(''' 54 %(c_name)s *arg = ¶m; 55''', 56 c_name=typ.c_name()) 57 return ret 58 59 60def gen_event_send(name, arg_type, boxed): 61 # FIXME: Our declaration of local variables (and of 'errp' in the 62 # parameter list) can collide with exploded members of the event's 63 # data type passed in as parameters. If this collision ever hits in 64 # practice, we can rename our local variables with a leading _ prefix, 65 # or split the code into a wrapper function that creates a boxed 66 # 'param' object then calls another to do the real work. 67 ret = mcgen(''' 68 69%(proto)s 70{ 71 QDict *qmp; 72 Error *err = NULL; 73 QMPEventFuncEmit emit; 74''', 75 proto=gen_event_send_proto(name, arg_type, boxed)) 76 77 if arg_type and not arg_type.is_empty(): 78 ret += mcgen(''' 79 QObject *obj; 80 Visitor *v; 81''') 82 ret += gen_param_var(arg_type) 83 84 ret += mcgen(''' 85 86 emit = qmp_event_get_func_emit(); 87 if (!emit) { 88 return; 89 } 90 91 qmp = qmp_event_build_dict("%(name)s"); 92 93''', 94 name=name) 95 96 if arg_type and not arg_type.is_empty(): 97 ret += mcgen(''' 98 v = qmp_output_visitor_new(&obj); 99''') 100 if not arg_type.is_implicit(): 101 ret += mcgen(''' 102 visit_type_%(c_name)s(v, "%(name)s", &arg, &err); 103''', 104 name=name, c_name=arg_type.c_name()) 105 else: 106 ret += mcgen(''' 107 108 visit_start_struct(v, "%(name)s", NULL, 0, &err); 109 if (err) { 110 goto out; 111 } 112 visit_type_%(c_name)s_members(v, ¶m, &err); 113 if (!err) { 114 visit_check_struct(v, &err); 115 } 116 visit_end_struct(v, NULL); 117''', 118 name=name, c_name=arg_type.c_name()) 119 ret += mcgen(''' 120 if (err) { 121 goto out; 122 } 123 124 visit_complete(v, &obj); 125 qdict_put_obj(qmp, "data", obj); 126''') 127 128 ret += mcgen(''' 129 emit(%(c_enum)s, qmp, &err); 130 131''', 132 c_enum=c_enum_const(event_enum_name, name)) 133 134 if arg_type and not arg_type.is_empty(): 135 ret += mcgen(''' 136out: 137 visit_free(v); 138''') 139 ret += mcgen(''' 140 error_propagate(errp, err); 141 QDECREF(qmp); 142} 143''') 144 return ret 145 146 147class QAPISchemaGenEventVisitor(QAPISchemaVisitor): 148 def __init__(self): 149 self.decl = None 150 self.defn = None 151 self._event_names = None 152 153 def visit_begin(self, schema): 154 self.decl = '' 155 self.defn = '' 156 self._event_names = [] 157 158 def visit_end(self): 159 self.decl += gen_enum(event_enum_name, self._event_names) 160 self.defn += gen_enum_lookup(event_enum_name, self._event_names) 161 self._event_names = None 162 163 def visit_event(self, name, info, arg_type, boxed): 164 self.decl += gen_event_send_decl(name, arg_type, boxed) 165 self.defn += gen_event_send(name, arg_type, boxed) 166 self._event_names.append(name) 167 168 169(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line() 170 171c_comment = ''' 172/* 173 * schema-defined QAPI event functions 174 * 175 * Copyright (c) 2014 Wenchao Xia 176 * 177 * Authors: 178 * Wenchao Xia <wenchaoqemu@gmail.com> 179 * 180 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 181 * See the COPYING.LIB file in the top-level directory. 182 * 183 */ 184''' 185h_comment = ''' 186/* 187 * schema-defined QAPI event functions 188 * 189 * Copyright (c) 2014 Wenchao Xia 190 * 191 * Authors: 192 * Wenchao Xia <wenchaoqemu@gmail.com> 193 * 194 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. 195 * See the COPYING.LIB file in the top-level directory. 196 * 197 */ 198''' 199 200(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix, 201 'qapi-event.c', 'qapi-event.h', 202 c_comment, h_comment) 203 204fdef.write(mcgen(''' 205#include "qemu/osdep.h" 206#include "qemu-common.h" 207#include "%(prefix)sqapi-event.h" 208#include "%(prefix)sqapi-visit.h" 209#include "qapi/qmp-output-visitor.h" 210#include "qapi/qmp-event.h" 211 212''', 213 prefix=prefix)) 214 215fdecl.write(mcgen(''' 216#include "qapi/error.h" 217#include "qapi/qmp/qdict.h" 218#include "%(prefix)sqapi-types.h" 219 220''', 221 prefix=prefix)) 222 223event_enum_name = c_name(prefix + "QAPIEvent", protect=False) 224 225schema = QAPISchema(input_file) 226gen = QAPISchemaGenEventVisitor() 227schema.visit(gen) 228fdef.write(gen.defn) 229fdecl.write(gen.decl) 230 231close_output(fdef, fdecl) 232